0
0
NumPydata~10 mins

Aggregation along specific axes in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Aggregation along specific axes
Start with 2D array
Choose aggregation function
Select axis: 0 (columns) or 1 (rows)
Apply aggregation along axis
Get reduced array with aggregated values
Use or display result
We start with a 2D array, pick an aggregation like sum or mean, choose axis 0 or 1, then apply aggregation along that axis to get a smaller array.
Execution Sample
NumPy
import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
sum_axis0 = arr.sum(axis=0)
sum_axis1 = arr.sum(axis=1)
print(sum_axis0, sum_axis1)
This code sums the 2D array along columns (axis 0) and rows (axis 1) and prints both results.
Execution Table
StepArray StateAxis ChosenAggregation FunctionResultExplanation
1[[1, 2, 3], [4, 5, 6]]NoneNoneOriginal arrayStart with 2D array
2[[1, 2, 3], [4, 5, 6]]0sum[5, 7, 9]Sum along columns: 1+4=5, 2+5=7, 3+6=9
3[[1, 2, 3], [4, 5, 6]]1sum[6, 15]Sum along rows: 1+2+3=6, 4+5+6=15
4[[1, 2, 3], [4, 5, 6]]0mean[2.5, 3.5, 4.5]Mean along columns: (1+4)/2=2.5, etc.
5[[1, 2, 3], [4, 5, 6]]1mean[2.0, 5.0]Mean along rows: (1+2+3)/3=2.0, etc.
6N/AN/AN/AExecution endsAll aggregations done
💡 All aggregation operations completed on the array.
Variable Tracker
VariableStartAfter sum axis=0After sum axis=1After mean axis=0After mean axis=1
arr[[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]]
sum_axis0N/A[5 7 9][5 7 9][5 7 9][5 7 9]
sum_axis1N/AN/A[6 15][6 15][6 15]
mean_axis0N/AN/AN/A[2.5 3.5 4.5][2.5 3.5 4.5]
mean_axis1N/AN/AN/AN/A[2.0 5.0]
Key Moments - 3 Insights
Why does sum with axis=0 reduce the number of rows but keep the number of columns?
Because axis=0 means summing down each column, so each column's values are added together, resulting in one value per column. This reduces rows to 1 but keeps columns the same, as shown in execution_table row 2.
What happens if we use axis=1 for aggregation?
Axis=1 means summing or aggregating across each row, so each row's values are combined into one number per row. This reduces columns to 1 but keeps rows the same, as shown in execution_table row 3.
Why does the shape of the result change after aggregation?
Aggregation along an axis collapses that axis by combining values, so the dimension along that axis becomes 1 (or disappears), reducing the array's shape. For example, sum along axis=0 changes shape (2,3) to (3,), as in rows 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 2. What is the sum of the second column after summing along axis=0?
A15
B7
C5
D3
💡 Hint
Check the 'Result' column in row 2 for sum_axis0 values.
At which step does the aggregation sum along rows (axis=1) happen?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look for 'axis=1' and 'sum' in the execution_table rows.
If we change the aggregation function from sum to mean along axis=0, what is the first value of the result?
A5
B6
C2.5
D1
💡 Hint
Check execution_table row 4 for mean along axis=0 results.
Concept Snapshot
Aggregation along axes in numpy:
- axis=0 aggregates down columns (reduces rows)
- axis=1 aggregates across rows (reduces columns)
- Common functions: sum(), mean(), max(), min()
- Result shape changes by collapsing chosen axis
- Use arr.sum(axis=0) or arr.mean(axis=1) etc.
Full Transcript
This visual execution shows how numpy aggregates data along specific axes in a 2D array. Starting with a 2x3 array, we apply sum and mean functions along axis 0 (columns) and axis 1 (rows). Summing along axis 0 adds values down each column, resulting in a 1D array with length equal to the number of columns. Summing along axis 1 adds values across each row, resulting in a 1D array with length equal to the number of rows. Mean works similarly but calculates averages. The variable tracker shows how each variable changes after each aggregation. Key moments clarify why axis choice affects the shape and result. The quiz tests understanding of these steps by referencing the execution table. This helps beginners see exactly how aggregation along axes works in numpy.