Skip to content
Learn Netverks

Lesson

Step 8/36 22% through track

shape-reshape

Shape and reshape

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

This lesson

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

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

You will apply Shape and reshape 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.

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

The shape attribute is a tuple of axis lengths. reshape returns a new view (when possible) with different dimensions but the same total size.

Rules of reshape

  • Total elements must match: prod(old_shape) == prod(new_shape)
  • -1 infers one dimension: arr.reshape(2, -1)
  • Row-major (C) order: last index varies fastest

1D ↔ 2D

import numpy as np
a = np.arange(6)
b = a.reshape(2, 3)
print(a.shape, '→', b.shape)
print(b)

ravel and flatten

  • ravel() — 1D view when possible
  • flatten() — always returns a copy

Important interview questions and answers

  1. Q: reshape(3, 4) from size 12?
    A: Valid—12 elements rearranged into 3 rows × 4 columns.
  2. Q: When does reshape fail?
    A: When requested shape product does not equal array size.

Self-check

  1. Reshape range(8) to (2, 4).
  2. What does -1 mean in reshape?

Tip: Use -1 in reshape to infer one dimension quickly.

Interview prep

reshape rule?

Total elements must match; -1 infers one dimension.

ravel vs flatten?

ravel view when possible; flatten always copies.

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

  • reshape vs resize?
  • -1 in reshape?

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