Skip to content
Learn Netverks

Lesson

Step 13/36 36% through track

broadcasting

Broadcasting

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

This lesson

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

Broadcasting mistakes silently reshape arrays—debugging shape tuples is daily NumPy hygiene.

You will apply Broadcasting 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.

Broadcasting lets NumPy operate on arrays of different shapes by virtually expanding smaller arrays along length-1 axes—without copying full-size temp arrays when possible.

Rules (simplified)

  1. Align shapes from the trailing dimension
  2. Dimensions must be equal or one of them is 1
  3. Missing dimensions are treated as size 1

Examples

import numpy as np
a = np.array([[1], [2], [3]])  # (3, 1)
b = np.array([10, 20, 30])     # (3,)
print(a + b)  # (3, 3) after broadcast

Common patterns

  • Subtract column mean: X - X.mean(axis=0)
  • Scale rows: X * row_weights[:, np.newaxis]
  • Compare to scalar threshold: arr > 0.5

Important interview questions and answers

  1. Q: Broadcasting always copy?
    A: Often uses virtual stride tricks—no full materialization of expanded array.
  2. Q: Incompatible shapes?
    A: ValueError when neither side is 1 and sizes differ.

Self-check

  1. Add shape (3,1) array to shape (3,) array—result shape?
  2. Why does broadcasting matter for performance?

Pitfall: Draw trailing axes on paper before combining shapes.

Interview prep

Rule?

Align trailing dims; equal or one is 1.

Failure?

ValueError when dimensions incompatible and neither is 1.

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

  • Broadcast rule?
  • Silent shape bug?

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