Skip to content
Learn Netverks

Lesson

Step 14/36 39% through track

axis-operations

Axis operations

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Axis operations: NumPy ndarray operations, vectorization, and numerical patterns used across the Python scientific stack.

Teams apply Axis operations in every serious NumPy project—skipping it leaves blind spots in analysis and reviews.

You will apply Axis operations in contexts like: Notebooks, feature engineering pipelines, and custom numerical code.

Read the narrative, run `import numpy as np` snippets in the playground (install NumPy with pip if the runner lacks it), tweak shapes and dtypes, and complete MCQs. Also reshape inputs until `np.broadcast_shapes` succeeds before debugging ufuncs.

When you can explain the previous lesson's ideas in your own words.

Many NumPy functions accept axis= to reduce or aggregate along rows, columns, or higher dimensions. Axis numbering starts at 0 for the outermost dimension.

2D axis cheat sheet

  • axis=0 — down columns (result per column)
  • axis=1 — across rows (result per row)
  • axis=None — flatten entire array first

Examples

import numpy as np
m = np.arange(12).reshape(3, 4)
print('col sums:', m.sum(axis=0))
print('row sums:', m.sum(axis=1))

keepdims

keepdims=True preserves reduced axes as length 1—helps broadcasting the result back against the original shape.

Important interview questions and answers

  1. Q: mean(axis=0) on 100×5 matrix?
    A: Five values—one mean per column.
  2. Q: Negative axis?
    A: Counts from last dimension: axis=-1 is last axis.

Self-check

  1. Compute per-row max of a 2D array.
  2. What does keepdims=True do?

Tip: For 2D data: axis=0 down columns, axis=1 across rows.

Interview prep

axis=0 on 2D?

Aggregate down columns—one result per column.

keepdims?

Preserves reduced axes as length 1 for broadcasting back.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • axis=0 vs 1?
  • keepdims why?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump