Array attributes (shape, dtype, ndim, size) in NumPy - Time & Space Complexity
We want to understand how fast we can get information about a NumPy array's structure and type.
How does the time to access attributes like shape or size change as the array grows?
Analyze the time complexity of accessing array attributes.
import numpy as np
arr = np.arange(1000).reshape(100, 10)
shape = arr.shape
ndim = arr.ndim
size = arr.size
dtype = arr.dtype
This code creates a 2D array and reads its shape, number of dimensions, total size, and data type.
Look for any repeated work when accessing these attributes.
- Primary operation: Accessing stored metadata of the array.
- How many times: Each attribute is accessed once, no loops or traversals.
Accessing these attributes does not depend on array size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same no matter how big the array is.
Time Complexity: O(1)
This means accessing these attributes takes the same short time regardless of array size.
[X] Wrong: "Getting the size or shape takes longer for bigger arrays because it counts elements."
[OK] Correct: NumPy stores this information when the array is created, so it just reads it instantly without counting.
Knowing that attribute access is fast helps you write efficient code and explain performance clearly in interviews.
"What if we tried to compute the sum of all elements instead of just accessing size? How would the time complexity change?"
Practice
numpy attribute tells you the total number of elements in an array?Solution
Step 1: Understand the meaning of each attribute
shapeshows dimensions,dtypeshows data type,ndimshows number of dimensions, andsizeshows total elements.Step 2: Identify the attribute for total elements
The attributesizegives the total number of elements in the array.Final Answer:
size -> Option AQuick Check:
Total elements = size [OK]
size attribute [OK]- Confusing
shapewith total elements - Thinking
ndimgives total elements - Mixing
dtypewith size
arr?Solution
Step 1: Recall attribute vs method syntax in numpy
Attributes likendimanddtypeare accessed without parentheses, methods likesize()do not exist.Step 2: Identify correct attribute for number of dimensions
arr.ndimgives the number of dimensions directly.Final Answer:
arr.ndim -> Option BQuick Check:
Number of dimensions = arr.ndim [OK]
- Using parentheses with attributes
- Confusing
shapewithndim - Trying to call
size()as method
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.shape, arr.dtype, arr.ndim, arr.size)
What is the output?
Solution
Step 1: Analyze the array shape and data type
The array has 2 rows and 3 columns, so shape is (2, 3). The default integer type on many systems is int64.Step 2: Check number of dimensions and total size
It is a 2D array, so ndim = 2. Total elements = 2*3 = 6.Final Answer:
(2, 3) int64 2 6 -> Option CQuick Check:
shape=(2,3), dtype=int64, ndim=2, size=6 [OK]
- Mixing rows and columns in shape
- Assuming dtype is int32 by default
- Confusing ndim with size
import numpy as np arr = np.array([1, 2, 3]) print(arr.shape())
Solution
Step 1: Identify usage of
shapeshapeis an attribute, so it should be accessed without parentheses.Step 2: Correct the code
Changearr.shape()toarr.shapeto fix the error.Final Answer:
shapeis an attribute, not a method; remove parentheses -> Option DQuick Check:
Attributes have no () [OK]
- Using parentheses with attributes
- Thinking shape returns a single number
- Assuming print can't show tuples
arr with shape (4, 5, 6). Which attribute would you use to find the total number of elements, and what is that number?Solution
Step 1: Understand the attributes
sizegives total elements,ndimgives number of dimensions,shapegives dimensions,dtypegives data type.Step 2: Calculate total elements from shape
Total elements = 4 * 5 * 6 = 120, soarr.size= 120.Final Answer:
arr.size, 120 -> Option AQuick Check:
Total elements = product of shape = 120 [OK]
size [OK]- Confusing
ndimwith total elements - Using
shapeinstead ofsize - Ignoring that
dtypeis unrelated to size
Ready to prove it?
Pass the test and this pattern moves unknown
