0Pricing
Data Science Academy · Lesson

Sum, Mean, and the Axis Trick

Aggregating down rows or across columns.

Sum, Mean, and the Axis Trick is a free Data Science Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Summaries in One Call

NumPy can collapse a whole array into a single number. Methods like sum and mean turn many values into one quick summary.

a = np.array([2, 4, 6])
total = a.sum()   # 12

Mean Is the Average

The mean divides the total by the count. For the array above it returns 4.0, the everyday average you already know.

avg = a.mean()   # 4.0

The Whole Array by Default

With no extra argument, these methods reduce everything to one scalar. Every element joins the calculation, ignoring rows and columns.

m = np.arange(6).reshape(2, 3)
m.sum()   # 15

Meet the axis Argument

The real power is axis. It tells NumPy which direction to collapse, so you can summarize per row or per column instead.

axis=0 Goes Down

Set axis=0 to collapse the rows. NumPy walks down each column and returns one value per column.

m.sum(axis=0)   # [3 5 7]

axis=1 Goes Across

Set axis=1 to collapse the columns. NumPy sweeps across each row and returns one value per row.

m.sum(axis=1)   # [3 12]

A Memory Trick

Think of axis as the dimension that disappears. Collapsing axis 0 removes the row dimension, leaving a result shaped like one row.

Same Idea for mean

The axis rule is identical for mean, min, max, and more. Learn it once and it applies across every reduction NumPy offers.

m.mean(axis=0)   # [1.5 2.5 3.5]

Keep the Dimensions

Pass keepdims=True to keep the collapsed axis as size 1. This makes the result broadcast cleanly back against the original array.

m.sum(axis=1, keepdims=True)   # shape 2 x 1

Counting Along an Axis

Reductions pair with shape to answer real questions: total per category, average per day, or a count of items down each column.

Pick the Axis on Purpose

Choosing the wrong axis is a classic bug. Always ask which dimension you want to keep, then collapse the other one.

Quick Check

You have a 2D array of rows and columns.

Axis Recap

You summarized whole arrays, then steered axis to summarize per row or per column. Remember: the axis you name is the one that vanishes. 👍

Frequently asked questions

Is the “Sum, Mean, and the Axis Trick” lesson free?

Yes — the full text of “Sum, Mean, and the Axis Trick” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.

What will I learn in “Sum, Mean, and the Axis Trick”?

Aggregating down rows or across columns. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Data Science Academy?

No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Sum, Mean, and the Axis Trick” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Data Science Academy lesson?

Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reshape and Flatten Arrays
  2. Sum, Mean, and the Axis Trick
  3. Boolean Masks for Selection
  4. Random Numbers and Seeds
← Back to Data Science Academy