download

What does mean(axis=0) return, and why isn't reshape a transpose?

Python~8 minReviewed 29 Oct 2026Free · No sign-in

Question

times holds how long two stages of a job took, in milliseconds, over three runs. Each row is a run. Each column is a stage.

  1. What do times.mean(axis=0) and times.mean(axis=1) return? Give the values and the shape.
  2. You need a table with one row per stage. A teammate writes times.reshape(2, 3). The shape is right. Is the table right? If not, what should they write?
"CODE · PYTHON"
import numpy as np

times = np.array([
    [12, 18],  # run 1: stage 1, stage 2
    [20, 10],  # run 2
    [16, 14],  # run 3
])
Show hints (3)
  1. "WHICH AXIS GOES"

    The axis you pass is the one that disappears. Shape (3, 2) means 3 runs and 2 stages. Remove axis 0. What is left?

  2. "FOLLOW THE ORDER"

    reshape reads the numbers row by row: 12, 18, 20, 10, 16, 14. Then it fills the new shape in that same order. Where does 18 end up?

  3. "TRACK ONE CELL"

    Run 1, stage 2 is 18, at times[0, 1]. In a table with one row per stage, it must sit at [1, 0]. Check that cell in both results.

Show checked explanation

axis=0 averages over the runs and leaves one mean per stage: [16. 14.], shape (2,). axis=1 leaves one mean per run: [15. 15. 15.], shape (3,). reshape(2, 3) only regroups the numbers in row order. It doesn't swap rows and columns. Use times.T.

"SOLUTION · PYTHON"
import numpy as np

# rows: runs, columns: stages (ms)
times = np.array([[12, 18], [20, 10], [16, 14]])

per_stage = times.mean(axis=0)
per_run = times.mean(axis=1)
print("per stage:", per_stage, per_stage.shape)
print("per run:", per_run, per_run.shape)
print("overall:", times.mean())

by_stage = times.T               # swaps the axes
regrouped = times.reshape(2, 3)  # same order, new grouping
print("transpose:", by_stage.tolist())
print("reshape:", regrouped.tolist())
print("run 1, stage 2:", times[0, 1], by_stage[1, 0], regrouped[1, 0])
"OUTPUT"
per stage: [16. 14.] (2,)
per run: [15. 15. 15.] (3,)
overall: 15.0
transpose: [[12, 20, 16], [18, 10, 14]]
reshape: [[12, 18, 20], [10, 16, 14]]
run 1, stage 2: 18 18 10

Code run on CPython 3.12 with NumPy 2.5 against the example above. Checked by an editor other than the author.

Walkthrough

Shape (3, 2) gives the sizes: 3 runs, 2 stages. It doesn't say what each axis means, so write that next to the array.

Averaging along an axis. The axis you pass is the one that disappears. axis=0 averages down each column, over the 3 runs:

stagevaluesmean
112, 20, 1616
218, 10, 1414

So you get [16. 14.], one value per stage, shape (2,). axis=1 averages across each row instead. Every run averages to 15, so you get [15. 15. 15.], shape (3,). With no axis, NumPy averages all six numbers into one: 15.0.

Reshape versus transpose. reshape keeps the numbers in the same order and only regroups them. By default it reads row by row (C order): 12, 18, 20, 10, 16, 14. Then it fills the new shape in that order:

  • times.reshape(2, 3) gives [[12, 18, 20], [10, 16, 14]]
  • times.T gives [[12, 20, 16], [18, 10, 14]]

Both have shape (2, 3). Only the transpose puts stage 1 in row 0 and stage 2 in row 1. The reshaped row 0 mixes 12 and 20 from stage 1 with 18 from stage 2.

Check one cell. Pick a value you know. Run 1, stage 2 is 18, at times[0, 1]. In a table with one row per stage, it must be at [1, 0]. times.T[1, 0] is 18. times.reshape(2, 3)[1, 0] is 10, so the reshape is wrong. Use small, distinct numbers for this test. With repeated values, a wrong table can pass by luck.

Common mistakes

  • Thinking axis=0 means "one result per row". The axis you pass is the one that disappears. Here axis=0 gives one value per stage, not one per run.
  • Using reshape to turn rows into columns. The shape comes out right, but the numbers stay in row order. Use .T or np.transpose.
  • Checking only .shape. Both results here are (2, 3). On a square table, axis=0 and axis=1 even return the same shape, so a wrong axis raises no error.
  • Calling times.mean() with no axis when you wanted one value per stage. It averages all six numbers into 15.0. If the stages measure different things, that number means little.

Follow-up questions

  • "A fourth run takes [24, 8]. What changes?" The shape becomes (4, 2). The stage means are now 18 and 12.5, the run means 15, 15, 15 and 16, and the overall mean 15.25. reshape(3, 3) now fails: it needs 9 values and there are 8. times.T has shape (2, 4) and keeps every value with its stage.
  • "If you change the result of reshape, does times change too?" Here, yes. times is stored row by row, so reshape(2, 3) returns a view: a new array that shares memory with times. Writing to it changes times. When a view is impossible, as in times.T.reshape(6), you get a copy. Call .copy() when you need a separate array. Since NumPy 2.1, copy=False raises an error instead of copying. .T is a view too.
  • "How do you stop people mixing up the axes?" Write the meaning where the array is made, for example # rows: runs, columns: stages (ms). Test with a small table of distinct values and check a few known cells. In larger code, a table with named columns, such as a pandas DataFrame, keeps the names on the data.

What a strong answer covers

Says what each axis means before computing. Explains that the axis you pass is the one that disappears. Shows that reshape keeps row order while .T swaps axes, and proves it by tracking one known cell, not by comparing shapes.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from The Python Interview Handbook (Nezir Zahirovic, 2023); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.