np.sum() and axis parameter in NumPy - Time & Space Complexity
We want to understand how the time it takes to add numbers using np.sum() changes as the size of the data grows.
Specifically, how does choosing different axes affect the work done?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.random.rand(1000, 500)
sum_all = np.sum(arr)
sum_axis0 = np.sum(arr, axis=0)
sum_axis1 = np.sum(arr, axis=1)
This code creates a 2D array and sums all elements, or sums along rows or columns.
Look at what repeats when summing.
- Primary operation: Adding each number in the array.
- How many times: Once for each element in the array, no matter which axis is chosen.
As the array gets bigger, the number of additions grows in direct proportion.
| Input Size (n x m) | Approx. Operations |
|---|---|
| 10 x 10 | 100 additions |
| 100 x 100 | 10,000 additions |
| 1000 x 1000 | 1,000,000 additions |
Pattern observation: Doubling the size in each dimension roughly quadruples the work because total elements increase.
Time Complexity: O(n * m)
This means the time to sum grows directly with the total number of elements in the array.
[X] Wrong: "Summing along an axis is faster because it looks like fewer sums happen."
[OK] Correct: Even when summing along one axis, every element is still added once, so the total work stays the same.
Knowing how array operations scale helps you write efficient code and explain your choices clearly in real projects or interviews.
What if we used np.sum() on a 3D array with the axis parameter? How would the time complexity change?
Practice
axis parameter in np.sum() control?Solution
Step 1: Understand the role of axis in np.sum()
The axis parameter tells numpy whether to sum across rows (axis=1) or columns (axis=0).Step 2: Clarify what axis does not affect
It does not change data type, size, or repetition of sums.Final Answer:
The direction along which the sum is calculated (rows or columns). -> Option BQuick Check:
axis controls sum direction [OK]
- Thinking axis changes data type
- Confusing axis with array size
- Assuming axis repeats sums
arr?Solution
Step 1: Recall default behavior of np.sum()
Without axis, np.sum(arr) sums all elements in the array.Step 2: Check invalid axis values
axis=2 is invalid for 2D arrays, axis=1,0 is incorrect syntax, axis='all' is invalid.Final Answer:
np.sum(arr) -> Option AQuick Check:
Default np.sum sums all elements [OK]
- Using invalid axis values
- Passing multiple axis values incorrectly
- Using string instead of integer for axis
arr = np.array([[1, 2, 3], [4, 5, 6]]), what is the output of np.sum(arr, axis=0)?Solution
Step 1: Understand axis=0 sums columns
Summing along axis=0 adds elements vertically: (1+4), (2+5), (3+6).Step 2: Calculate sums for each column
1+4=5, 2+5=7, 3+6=9, so result is [5 7 9].Final Answer:
[5 7 9] -> Option AQuick Check:
Sum columns = [5 7 9] [OK]
- Confusing axis=0 with axis=1
- Adding rows instead of columns
- Expecting a scalar output
import numpy as np arr = np.array([[1, 2], [3, 4]]) result = np.sum(arr, axis=2)
Solution
Step 1: Check array dimensions
The array has shape (2, 2), so valid axis values are 0 or 1.Step 2: Validate axis parameter
axis=2 is out of bounds for this 2D array, causing an error.Final Answer:
The axis value 2 is invalid for a 2D array. -> Option CQuick Check:
Axis must be less than array dimensions [OK]
- Using axis larger than array dimensions
- Thinking np.sum can't handle 2D arrays
- Ignoring error messages
arr = np.array([[[1,2],[3,4]], [[5,6],[7,8]]]). What is the shape of the result when you run np.sum(arr, axis=1)?Solution
Step 1: Understand the shape of arr
arr has shape (2, 2, 2): 2 blocks, each with 2 rows and 2 columns.Step 2: Summing along axis=1
axis=1 sums over the second dimension (rows), reducing it from 2 to 1, so shape changes from (2, 2, 2) to (2, 2).Final Answer:
(2, 2) -> Option DQuick Check:
Sum axis=1 reduces dimension 2 to 1 [OK]
- Confusing which axis is summed
- Expecting shape to stay same
- Mixing up dimensions in 3D arrays
Ready to prove it?
Pass the test and this pattern moves unknown
