Yandex

Course IndexCourse Index0

    ProgramGuru

    NumPy Array Attributes
    shape, size, ndim, itemsize


    Once you've created an array with NumPy, your next step is to understand how to inspect it. Just like a carpenter knows their tools inside and out, a data scientist must understand what they're working with—before diving into slicing, reshaping, or computing. That's where shape, size, ndim, itemsize, and other attributes come into play.

    Why Learn Array Attributes?

    When working with numerical data, it's important to know the structure and nature of the data stored in your array. These attributes tell you:

    • How many elements are present?
    • How many dimensions are involved?
    • How much memory is being used?

    Whether you're debugging shape mismatches or optimizing for performance, these attributes are your first checkpoint.

    Let’s Start with a NumPy Array

    import numpy as np
    
    arr = np.array([[1, 2, 3], [4, 5, 6]])
    print(arr)

    This creates a 2D array with 2 rows and 3 columns. Now let’s inspect it.

    1. shape – The Dimensions of the Array

    arr.shape returns a tuple representing the dimensions of the array.

    print(arr.shape)  # Output: (2, 3)

    That means 2 rows and 3 columns. You can also change the shape using this attribute (with caution):

    arr.shape = (3, 2)
    print(arr)

    Tip: Ensure total elements remain the same, or you'll get a ValueError.

    2. size – Total Number of Elements

    arr.size gives you the total count of elements in the array.

    print(arr.size)  # Output: 6

    3. ndim – Number of Dimensions

    arr.ndim tells you how many axes (dimensions) the array has.

    print(arr.ndim)  # Output: 2

    A 1D array has ndim = 1, a 2D array has ndim = 2, and so on.

    4. itemsize – Memory Used by Each Element (in Bytes)

    Every data type in NumPy has a size. itemsize shows how many bytes one element takes.

    print(arr.itemsize)  # Output: 8 (for int64 on most machines)

    If memory optimization matters to you (and it will at scale), this is key.

    5. dtype – Data Type of Elements

    arr.dtype tells you the type of elements inside the array.

    print(arr.dtype)  # Output: int64

    You can change the data type using arr.astype().

    6. nbytes – Total Memory Consumption (in Bytes)

    Multiply the number of elements by the item size and you get nbytes.

    print(arr.nbytes)  # Output: 48

    That's 6 elements × 8 bytes per element = 48 bytes.

    Verifying Your Understanding

    Here’s a simple checklist to reinforce what you’ve learned:

    • Can you predict the shape of an array before printing it?
    • Does the size match the product of its shape?
    • Is the memory usage reasonable for the data type?
    • What happens when you reshape a 1D array to 2D?

    Best Practices

    • Always check shape before reshaping arrays.
    • Use dtype to ensure compatibility during numerical computations.
    • Track memory using nbytes when working with large datasets.

    Final Thoughts

    Array attributes in NumPy are more than just metadata—they guide your approach to reshaping, computing, and optimizing performance. The more you read them, the better you understand what you're building. Every array tells a story; you just need to learn how to listen to it.



    Welcome to ProgramGuru

    Sign up to start your journey with us

    Support ProgramGuru.org

    You can support this website with a contribution of your choice.

    When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

    PayPal

    UPI

    PhonePe QR

    MALLIKARJUNA M