Concept Flow - Array attributes (shape, dtype, ndim, size)
Create numpy array
Access .shape
Access .dtype
Access .ndim
Access .size
Done
Create a numpy array, then check its shape, data type, number of dimensions, and total size step-by-step.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.shape) print(arr.dtype) print(arr.ndim) print(arr.size)
| Step | Action | Expression Evaluated | Result |
|---|---|---|---|
| 1 | Create array | arr = np.array([[1, 2, 3], [4, 5, 6]]) | [[1 2 3] [4 5 6]] |
| 2 | Access shape | arr.shape | (2, 3) |
| 3 | Access dtype | arr.dtype | int32 |
| 4 | Access ndim | arr.ndim | 2 |
| 5 | Access size | arr.size | 6 |
| 6 | End | No more attributes to access | Execution stops |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | After Step 5 |
|---|---|---|---|---|---|---|
| arr | undefined | [[1 2 3] [4 5 6]] | [[1 2 3] [4 5 6]] | [[1 2 3] [4 5 6]] | [[1 2 3] [4 5 6]] | [[1 2 3] [4 5 6]] |
| arr.shape | undefined | undefined | (2, 3) | (2, 3) | (2, 3) | (2, 3) |
| arr.dtype | undefined | undefined | undefined | int32 | int32 | int32 |
| arr.ndim | undefined | undefined | undefined | undefined | 2 | 2 |
| arr.size | undefined | undefined | undefined | undefined | undefined | 6 |
Array attributes in numpy: - shape: tuple showing size in each dimension - dtype: data type of elements - ndim: number of dimensions - size: total number of elements Use these to understand array structure quickly.
numpy attribute tells you the total number of elements in an array?shape shows dimensions, dtype shows data type, ndim shows number of dimensions, and size shows total elements.size gives the total number of elements in the array.size attribute [OK]shape with total elementsndim gives total elementsdtype with sizearr?ndim and dtype are accessed without parentheses, methods like size() do not exist.arr.ndim gives the number of dimensions directly.shape with ndimsize() as methodimport numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.shape, arr.dtype, arr.ndim, arr.size)
import numpy as np arr = np.array([1, 2, 3]) print(arr.shape())
shapeshape is an attribute, so it should be accessed without parentheses.arr.shape() to arr.shape to fix the error.shape is an attribute, not a method; remove parentheses -> Option Darr with shape (4, 5, 6). Which attribute would you use to find the total number of elements, and what is that number?size gives total elements, ndim gives number of dimensions, shape gives dimensions, dtype gives data type.arr.size = 120.arr.size, 120 -> Option Asize [OK]ndim with total elementsshape instead of sizedtype is unrelated to sizePass the test and this pattern moves unknown