Lesson 5 of 23
โฎPrev Nextโฏ

PY-401 ยท NumPy foundations

Aggregations and axes

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

5of 23

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Aggregations and axes, running
    1. import numpy as np
    2. # rows = stores A, B, C columns = Mon, Tue, Wed
    3. sales = np.array([[10, 12, 9],
    4. [14, 11, 15],
    5. [8, 13, 10]])
    6. print(sales.sum())
    7. print(sales.sum(axis=0)) # per day
    8. print(sales.sum(axis=1)) # per store
    9. print(sales.mean(axis=1).round(1))
    10. print(sales.argmax(axis=1)) # best day of each store
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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.
    2. The Python ideaa.sum() # everythingAggregations reduce many values to one: sum(), mean(), min(), max() and std().
    3. In real lifeA 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.

    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

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    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.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. a = np.array([[1, 2], [3, 4]]). What is a.sum(axis=0)?
    2. 2. Same a. What is a.sum(axis=1)?
    3. 3. What does argmax return?
    4. 4. Rows are students, columns are subjects. Which gives one average per subject?
    5. 5. What does a.std() measure?

    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=0 squashes the rows away (one answer per column); axis=1 squashes 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