After creating a NumPy array, you'll often want to understand its structure without printing the entire array, especially if it's large. NumPy arrays come with several useful attributes that provide metadata about the array itself. These attributes don't require parentheses ()
like methods do; you access them directly using dot notation (e.g., my_array.attribute
). Let's look at the fundamental ones: ndim
, shape
, size
, and dtype
.
Imagine you have created a couple of arrays like these:
import numpy as np
# A 1-dimensional array
arr1d = np.array([1, 2, 3, 4, 5])
# A 2-dimensional array (3 rows, 4 columns)
arr2d = np.array([[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0]])
Now, let's inspect their attributes.
ndim
: Number of Axes (Dimensions)The ndim
attribute tells you the number of axes, or dimensions, of the array. A 1D array has one axis, a 2D array has two axes (like rows and columns in a spreadsheet), a 3D array has three, and so on.
print(arr1d.ndim)
# Expected Output: 1
print(arr2d.ndim)
# Expected Output: 2
Knowing the number of dimensions is fundamental for understanding how to index the array and how operations might behave.
shape
: The Dimensions of the ArrayWhile ndim
tells you how many axes there are, the shape
attribute tells you the size of the array along each of those axes. It returns a tuple of integers indicating the number of elements along each dimension.
For our 1D array arr1d
, the shape reflects its length:
print(arr1d.shape)
# Expected Output: (5,)
Notice the comma inside the tuple (5,)
. This indicates it's a tuple with one element, signifying one dimension of length 5.
For our 2D array arr2d
, the shape reflects its structure of rows and columns:
print(arr2d.shape)
# Expected Output: (3, 4)
This output (3, 4)
tells us the array has 2 dimensions (because there are two numbers in the tuple, which matches arr2d.ndim
). The first axis has a length of 3 (think rows), and the second axis has a length of 4 (think columns).
The shape
is one of the most frequently used attributes, as it provides a quick overview of the array's structure.
size
: Total Number of ElementsThe size
attribute provides the total number of elements in the array. This is simply the product of the elements in the shape
tuple.
print(arr1d.size)
# Expected Output: 5
print(arr2d.size)
# Expected Output: 12
For arr1d
, the size is 5 (matching its shape (5,)
). For arr2d
, the size is 3×4=12 (matching its shape (3, 4)
). This attribute is handy for quickly knowing how much data you're dealing with.
dtype
: Data Type of Array ElementsWe discussed earlier that NumPy arrays contain elements of the same data type. The dtype
attribute reveals what that data type is. NumPy automatically infers a suitable data type when you create an array, but you can also specify it explicitly.
print(arr1d.dtype)
# Expected Output: dtype('int64') (or potentially 'int32' depending on your system)
print(arr2d.dtype)
# Expected Output: dtype('float64')
In arr1d
, NumPy detected integers, so it likely assigned a 64-bit integer type (int64
). In arr2d
, we used numbers with decimal points (like 1.0
), so NumPy assigned a 64-bit floating-point type (float64
).
Knowing the dtype
is important for several reasons:
int64
uses more memory than int8
).float32
, float64
) offer different levels of precision.These attributes (ndim
, shape
, size
, dtype
) are your first tools for inspecting and understanding the structure and nature of your NumPy arrays. Regularly checking them helps ensure your arrays are structured as expected before you proceed with more complex operations.
© 2025 ApX Machine Learning