Understanding NumPy Array Attributes
Once you've created a NumPy array, it's important to understand its internal properties, known as attributes. These attributes help you inspect the structure and metadata of the array — such as its shape, size, data type, and number of dimensions.
NumPy arrays, or ndarray objects, expose several useful attributes that allow you to explore the array's configuration without needing to manipulate the data directly.
Common Attributes of NumPy Arrays
NumPy arrays, also known as ndarray objects, come with several built-in attributes that help you understand the array’s structure, memory usage, and metadata. These attributes are essential when working with numerical or scientific data.
Here are the most commonly used attributes of a NumPy array:
- ndim: Returns the number of dimensions (axes) of the array.
- shape: Returns a tuple showing the size of each dimension.
- size: Returns the total number of elements in the array.
- dtype: Describes the data type of each element in the array.
- itemsize: Shows the size in bytes of each individual element.
- nbytes: Total number of bytes consumed by all elements.
- T: Returns the transpose of the array (rows become columns).
Understanding these attributes helps you analyze array structures, optimize performance, and debug issues effectively in data science and numerical applications.
💡 Tip: The most frequently used attributes are ndim, shape, size, and dtype. Start by learning those first!
Example 1: Exploring Common NumPy Array Attributes
Let’s create a simple 2D array and explore its most useful attributes. This will help you understand how NumPy internally represents the structure and data of an array.
import numpy as np
# Create a 2D NumPy array
arr = np.array([[10, 20, 30], [40, 50, 60]])
# Display basic attributes
print("Number of dimensions:", arr.ndim)
print("Shape of array:", arr.shape)
print("Total number of elements:", arr.size)
print("Data type of elements:", arr.dtype)
print("Bytes per element:", arr.itemsize)
print("Total bytes:", arr.nbytes)
print("Transpose:",)
print(arr.T)
import numpy as np
# Create a 2D NumPy array
arr = np.array([[10, 20, 30], [40, 50, 60]])
# Display basic attributes
print("Number of dimensions:", arr.ndim)
print("Shape of array:", arr.shape)
print("Total number of elements:", arr.size)
print("Data type of elements:", arr.dtype)
print("Bytes per element:", arr.itemsize)
print("Total bytes:", arr.nbytes)
print("Transpose:",)
print(arr.T)
Output
Number of dimensions: 2
Shape of array: (2, 3)
Total number of elements: 6
Data type of elements: int64
Bytes per element: 8
Total bytes: 48
Transpose:
[[10 40]
[20 50]
[30 60]]
Number of dimensions: 2
Shape of array: (2, 3)
Total number of elements: 6
Data type of elements: int64
Bytes per element: 8
Total bytes: 48
Transpose:
[[10 40]
[20 50]
[30 60]]
As you can see:
- ndim is 2 because it’s a 2D array.
- shape is (2, 3), meaning 2 rows and 3 columns.
- size is 6, which is the total number of elements.
- dtype is int64, showing the data type.
- itemsize is 8, indicating each int64 uses 8 bytes.
- nbytes is 48, total memory used (6 × 8).
- T gives the transposed view (rows ↔ columns).
💡 Tip: Try modifying the array or reshaping it and observe how these attributes change!
Frequently Asked Questions
What does the shape attribute of a NumPy array show?
What does the shape attribute of a NumPy array show?
The shape attribute returns a tuple showing the size of the array along each dimension. For example, (2, 3) means 2 rows and 3 columns in a 2D array.
What is the difference between ndim and size in NumPy?
What is the difference between ndim and size in NumPy?
ndim returns the number of dimensions (axes) in the array, while size returns the total number of elements across all dimensions.
How do I find the data type of elements in a NumPy array?
How do I find the data type of elements in a NumPy array?
You can use the dtype attribute to find the data type. For example, arr.dtype may return int64, float32, etc.
What does the T attribute do in NumPy?
What does the T attribute do in NumPy?
The T attribute returns the transpose of the array. It flips the array’s axes, converting rows into columns and vice versa.
How can I check the memory usage of a NumPy array?
How can I check the memory usage of a NumPy array?
Use the nbytes attribute to check total memory usage in bytes, and itemsize to see how much memory each element consumes.
What's Next?
Now that you understand how to inspect a NumPy array's structure using attributes like shape, ndim, and dtype, the next step is to learn how to create arrays efficiently.
In the next section, we'll cover NumPy initialization functions — tools like np.zeros, np.ones, np.eye, and more. These functions help you quickly generate arrays with specific shapes, values, or identity structures, which is essential for numerical computing and prototyping.