PY-401 ยท NumPy foundations
Aggregations and axes
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Aggregations reduce many values to one: sum(), mean(), min(), max() and std(). On a 2-D array, axis=0 collapses the rows and gives one result per column, while axis=1 collapses the columns and gives one result per row. argmax() tells you where the biggest value is instead of what it is.
- Total, average and spread with one call
- Choose axis=0 or axis=1
- Find the position of the best value with argmax
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
import numpy as np# rows = stores A, B, C columns = Mon, Tue, Wedsales = np.array([[10, 12, 9],[14, 11, 15],[8, 13, 10]])print(sales.sum())print(sales.sum(axis=0)) # per dayprint(sales.sum(axis=1)) # per storeprint(sales.mean(axis=1).round(1))print(sales.argmax(axis=1)) # best day of each store
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
102
[32 36 34]
[31 40 31]
[10.3 13.3 10.3]
[1 2 1] ๐ฏ Each row of marks is a student (Asha, Ben, Chen). Set avg to each student's average mark and best to the row number (as an int) of the student with the highest average.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
One average per row is axis=1. Then best = int(avg.argmax())
- The problemMonday morning report: total sales, sales per day, sales per store, and which day was each store's best. Four questions, all answered by the same table with different axes.
- The Python idea
a.sum() # everythingAggregations reduce many values to one:sum(),mean(),min(),max()andstd(). - In real lifeA school report card system stores marks as students ร subjects.
marks.mean(axis=1)gives each student's average andmarks.mean(axis=0)shows which subject the class found hardest.
Start with a question
Monday morning report: total sales, sales per day, sales per store, and which day was each store's best. Four questions, all answered by the same table with different axes.
Reports, dashboards and machine-learning features are built from these summaries. pandas groupby uses the same idea.
Explain it like I'm 10
Picture a sticker chart for three friends over three days. Adding down each column tells you how many stickers were given each day. Adding across a row tells you how many one friend got all week.
The technical explanation
With no axis, a.sum() adds every value in the whole array.
a.sum(axis=0) adds down each column, so the result has one value per column. a.sum(axis=1) adds across each row.
mean, max, min and std take the same axis argument.
argmax returns the index of the largest value. With axis=1 you get, for each row, the column where its best value sits.
Syntax
a.sum() # everything
a.sum(axis=0) # one per column
a.mean(axis=1) # one per row
a.argmax(axis=1) # where each row's max is Real-world scenario
A school report card system stores marks as students ร subjects. marks.mean(axis=1) gives each student's average and marks.mean(axis=0) shows which subject the class found hardest.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: pandas Series โQuestions people ask
What does axis=0 mean in NumPy?
axis=0 is the rows direction. An aggregation with axis=0 collapses the rows and returns one result per column. axis=1 collapses the columns and returns one result per row.
What is the difference between max and argmax in NumPy?
max returns the largest value itself. argmax returns its index, which you use to look up something else, such as the name of the best store or the date of the busiest day.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What does axis=0 mean in NumPy?
axis=0 is the rows direction. An aggregation with axis=0 collapses the rows and returns one result per column. axis=1 collapses the columns and returns one result per row.
beginner What is the difference between max and argmax in NumPy?
max returns the largest value itself. argmax returns its index, which you use to look up something else, such as the name of the best store or the date of the busiest day.
intermediate a = np.array([[1, 2], [3, 4]]). What is a.sum(axis=0)?
[4 6]. axis=0 adds down each column: 1+3 and 2+4.
intermediate Same a. What is a.sum(axis=1)?
[3 7]. axis=1 adds across each row: 1+2 and 3+4.
Quick revision
Remember
- Total, average and spread with one call
- Choose axis=0 or axis=1
- Find the position of the best value with argmax
- axis is the direction that disappears.
axis=0squashes the rows away (one answer per column);axis=1squashes the columns away (one answer per row).
Syntax
a.sum() # everything
a.sum(axis=0) # one per column
a.mean(axis=1) # one per row
a.argmax(axis=1) # where each row's max is Official documentation
- numpy.sum (docs.python.org)
- numpy.argmax (docs.python.org)