0
0
NumPydata~10 mins

np.sum() and axis parameter in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.sum() and axis parameter
Start with array
Choose axis parameter
Sum elements along axis
Return summed array or scalar
End
np.sum() adds numbers in an array. The axis parameter decides which direction to add: rows, columns, or all.
Execution Sample
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
sum_all = np.sum(arr)
sum_axis0 = np.sum(arr, axis=0)
sum_axis1 = np.sum(arr, axis=1)
This code sums all elements, then sums by columns (axis=0), then by rows (axis=1).
Execution Table
StepOperationInput ArrayAxis ParameterResult
1Sum all elements[[1, 2], [3, 4]]None10
2Sum along axis=0 (columns)[[1, 2], [3, 4]]0[4, 6]
3Sum along axis=1 (rows)[[1, 2], [3, 4]]1[3, 7]
💡 All sums computed; axis parameter controls direction of summation.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
arr[[1, 2], [3, 4]][[1, 2], [3, 4]][[1, 2], [3, 4]][[1, 2], [3, 4]]
sum_allundefined101010
sum_axis0undefinedundefined[4, 6][4, 6]
sum_axis1undefinedundefinedundefined[3, 7]
Key Moments - 3 Insights
Why does np.sum(arr) return a single number but np.sum(arr, axis=0) returns an array?
np.sum(arr) adds all numbers in the whole array into one number (see step 1). When axis=0, it sums down each column separately, so the result is an array with one sum per column (step 2).
What does axis=1 mean in np.sum(arr, axis=1)?
Axis=1 means sum across each row. So it adds numbers in each row separately, giving one sum per row (step 3).
What happens if axis is set to a value not in the array's dimensions?
Numpy will raise an error because the axis does not exist. The axis must be 0 or 1 for a 2D array like this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of np.sum(arr, axis=0)?
A[3, 7]
B[4, 6]
C10
D[1, 2]
💡 Hint
Check row 2 in the execution table under Result.
At which step does np.sum() add all elements into one number?
AStep 2
BStep 3
CStep 1
DNone
💡 Hint
Look at the Operation column for 'Sum all elements'.
If we change axis=1 to axis=0, how does the result change compared to step 3?
AResult sums columns instead of rows
BResult sums rows instead of columns
CResult becomes a single number
DResult stays the same
💡 Hint
Compare results in step 2 and step 3 in the execution table.
Concept Snapshot
np.sum(array, axis=None)
- Sums all elements if axis=None
- axis=0 sums down columns
- axis=1 sums across rows
- Result shape depends on axis
- Useful for quick totals in arrays
Full Transcript
This lesson shows how np.sum() adds numbers in arrays. Without axis, it adds all numbers into one total. With axis=0, it adds numbers down each column separately. With axis=1, it adds numbers across each row separately. The axis parameter controls the direction of summation. We traced a 2x2 array example step-by-step, watching how results change. This helps understand how to get totals by rows, columns, or entire array.