Array Attributes and Inspection
Explore ndim, shape, size, and dtype attributes, and learn how to reshape an array with reshape().
Why Array Attributes Matter
Before working on an array, you'll want to know its shape, dimensions, and dtype. NumPy hands you these instantly through lightweight attributes.
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print('ndim:', a.ndim) # 2
print('shape:', a.shape) # (2, 3)
print('size:', a.size) # 6
print('dtype:', a.dtype) # int64ndim: Number of Dimensions
ndim tells you how many dimensions an array has: a flat list is 1, a list of lists is 2, and ML tensors often go to 3 or 4.
import numpy as np
v = np.array([1, 2, 3])
print(v.ndim) # 1
m = np.zeros((4, 5))
print(m.ndim) # 2
t = np.ones((2, 3, 4))
print(t.ndim) # 3All lessons in this course
- Creating NumPy Arrays
- Array Attributes and Inspection
- Element-Wise Arithmetic
- Array Slicing and Indexing