Index Ai Academy
Phase 0 · Working with data

NumPy in practice: shapes, types and picking values

Reshape, view and copy, where and clip, top-k, stacking and the (N, C, H, W) batch a model expects.

About 35 minutes · Free lesson

What it covers6

Why it matters

Every input to a model is an array with a shape, and most first-week bugs are shape or type bugs: an axis that vanished, a uint8 that wrapped around, a reshape that scrambled pixels, a slice that changed its parent. This lesson names the handful of NumPy moves that the image project, pandas and PyTorch all lean on, so you meet them once, slowly, before they appear mixed into larger code.

What you'll be able to do

  • Read and change an array's shape with reshape (including -1), .T and a new axis
  • Choose between a view and a copy, and say which a slice, a reshape and .copy() give
  • Pick values with np.where, np.clip, a mask and argsort, and join arrays with concatenate and stack
  • Turn a batch of images into the (N, C, H, W) float tensor a PyTorch model expects
Quick check
A guess before you start: how many numbers does an array of shape (3, 4) hold?
  1. 7
  2. 3
  3. 12
  4. 4
Show the answer

12

A shape (3, 4) is 3 rows of 4 numbers: 3 times 4 is 12. The shape says how the numbers are laid out; the count is the product of its parts.

The idea: one block of numbers, a shape and a dtype

This lesson rests on NumPy arrays and the Foundation's Tensors and broadcasting. An array holds its numbers in one block of memory. Two small labels say how to read the block: its shape (how the numbers are laid out in rows and columns) and its dtype (what kind of number each cell holds).

The same twelve numbers can be read as a list of 12, a grid of 3 rows and 4 columns, or two grids of 2 by 3. Changing the shape changes the labels, not the numbers:

import numpy as np

a = np.arange(12)
print(a.shape)
print(a.reshape(3, 4).shape)
print(a.reshape(2, 2, 3).shape)
print(a.reshape(2, -1).shape)
It runs right here in your browser, with nothing to install.

The -1 means "work this one out": 12 numbers in 2 rows leaves 6 columns. Every model input is an array with a shape like this, so most AI bugs are shape bugs.

The dtype matters just as much. Whole numbers are int64 by default on most computers (int32 here in the browser, as the first line below prints), numbers with a decimal point are float64, and pixels are uint8: whole numbers from 0 to 255 in one byte each. PyTorch's default is float32, half the size of float64, which is why a model's weights fit in memory. A mismatch between the two is a common error message: the cure is .astype in NumPy and .float() in PyTorch.

import numpy as np

print(np.array([1, 2]).dtype, np.array([1, 2.5]).dtype)
print(np.array([1, 2.5], dtype=np.float32).dtype)
small = np.array([250, 5], dtype=np.uint8)
print(small.dtype, (small + 10).tolist())

A decimal anywhere in a list makes the whole array float. A uint8 can't go above 255, so a sum that does wraps around instead of growing.

Quick check
Which dtype does np.array([1, 2.5]).dtype report?
import numpy as np

print(np.array([1, 2.5]).dtype)
  1. float64
  2. int64
  3. uint8
  4. float32
Show the answer

float64

One decimal makes the whole array float, and NumPy's default float is float64.

Quick check
From the lesson on NumPy: what does this print?
import numpy as np

grid = np.zeros((4, 6))
print(grid.shape, grid.size)
  1. (4, 6) 10
  2. (6, 4) 24
  3. (24,) 24
  4. (4, 6) 24
Show the answer

(4, 6) 24

np.zeros((4, 6)) has 4 rows and 6 columns, and size counts all 24 cells.

Try it now

A reshape doesn't copy the numbers: b is another way of reading the same block as a.

In your own words
In your own words: what do the shape and the dtype of an array tell you, and which one does reshape change?

Write a sentence or two, then compare it with a good answer in the lesson.

In Python: reshape, pick, stack and copy

Purpose. The seven moves you reach for most, and the one trap behind them: a view is not a copy.

New axes, and keeping them

Many bugs come from a result that has lost an axis. sum(axis=1) collapses the columns and returns a flat list of row totals, which no longer lines up with the grid. keepdims=True keeps the collapsed axis with length 1, so the result still broadcasts against the grid. [:, None] does the same for any flat list, and .T swaps rows and columns:

import numpy as np

counts = np.array([[2.0, 6.0], [1.0, 3.0]])
totals = counts.sum(axis=1)
print(totals.shape, counts.sum(axis=1, keepdims=True).shape)

share = counts / counts.sum(axis=1, keepdims=True)
print(share.tolist())
print(counts.T.shape)

Each row now sums to 1: row totals as a column divide every row by its own total.

Quick check
Why does a / a.sum(axis=1) go wrong for a (2, 2) array, while a / a.sum(axis=1, keepdims=True) works?
  1. keepdims makes the array twice as big
  2. The flat (2,) result matches the columns, not the rows
  3. sum(axis=1) returns text instead of numbers
  4. Only (2, 1) arrays can be divided
Show the answer

The flat (2,) result matches the columns, not the rows

Without keepdims the row totals are a flat (2,) array, and broadcasting lines it up with the last axis, the columns. With keepdims the totals are (2, 1) and divide row by row.

Choosing values: where, clip and a mask

np.where(condition, a, b) picks from a where the condition holds and from b everywhere else. np.clip(x, low, high) pins values into a range. A boolean mask picks the cells that pass a test:

import numpy as np

scores = np.array([0.2, 0.9, 0.55, 0.7])
print(np.where(scores >= 0.5, "pass", "fail").tolist())
print(np.clip(scores, 0.3, 0.8).tolist())
print(scores[scores >= 0.5].tolist(), (scores >= 0.5).sum())

where keeps the shape and gives a value for every cell. The mask gives back only the cells that pass, so the result is shorter. Adding the booleans counts them: True is 1.

Ranking, joining and multiplying

np.argsort gives the positions that would sort the values; reversed and cut, it gives the top few. np.concatenate joins arrays along an axis that already exists, and np.stack makes a new axis to hold them. a @ b is the matrix product:

import numpy as np

scores = np.array([0.2, 0.9, 0.55, 0.7])
top2 = np.argsort(scores)[::-1][:2]
print(top2.tolist(), scores[top2].tolist())

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.concatenate([a, b]).shape, np.stack([a, b]).shape)
print(np.array([[1, 2], [3, 4]]) @ np.array([[1], [1]]))

Two vectors of length 3 stack into a (2, 3) grid, but concatenate into one vector of length 6.

Drill

Row shares

Write row_shares(a) that divides each row of the 2D array a by its own total, so every row sums to 1. Use keepdims.

import numpy as np

def row_shares(a):
    pass
In the lesson, your code is checked as you write it.
Drill

The top k

Write top_k(scores, k) that returns the positions of the k highest scores, best first, as a list.

import numpy as np

def top_k(scores, k):
    pass
In the lesson, your code is checked as you write it.

A view is not a copy

Slicing and reshaping return views: new labels on the same memory. That is fast, and it is also how one array silently changes another. .copy() makes an independent array, and np.shares_memory tells you which you have:

import numpy as np

a = np.arange(6)
view = a[:3]
safe = a[:3].copy()
view[0] = 100
safe[1] = 200
print(a.tolist())
print(np.shares_memory(a, view), np.shares_memory(a, safe))

rng = np.random.default_rng(0)
first = rng.integers(0, 10, size=3).tolist()
again = np.random.default_rng(0).integers(0, 10, size=3).tolist()
print(first == again)

The last line shows why to pass a seed: default_rng(0) gives the same numbers every run, so an experiment can be repeated.

Quick check
What does this print?
import numpy as np

a = np.arange(3)
view = a[:]
safe = a.copy()
view[0] = 9
print(a.tolist(), safe.tolist())
  1. [0, 1, 2] [9, 1, 2]
  2. [0, 1, 2] [0, 1, 2]
  3. [9, 1, 2] [0, 1, 2]
  4. [9, 1, 2] [9, 1, 2]
Show the answer

[9, 1, 2] [0, 1, 2]

view is a slice of a, so writing through it changes a. safe is a copy, so a change to the original later doesn't reach it: it still holds the original 0, 1, 2.

Worked example

Worked example: the three best answers, masked

A model gave scores to five answers. Keep the three best, set the rest to zero, and rescale what's left so it sums to 1.

import numpy as np

scores = np.array([0.1, 0.6, 0.3, 0.8, 0.2])
top = np.argsort(scores)[::-1][:3]
mask = np.zeros(len(scores), dtype=bool)
mask[top] = True
kept = np.where(mask, scores, 0.0)
probs = kept / kept.sum()
print(top.tolist())
print(kept.tolist())
print(round(float(probs.sum()), 6))
In the lesson you can change this and run it.

argsort gives the positions of the best three. A boolean mask marks those positions, and np.where keeps their scores and writes 0.0 elsewhere. Dividing by the total rescales the survivors so they sum to 1. This is the idea behind top-k sampling in language models.

In your own words
A function slices an array, edits the slice, and the caller finds its own array changed. What happened, and what is the fix?

Write a sentence or two, then compare it with a good answer in the lesson.

In AI work: batches, shapes and PyTorch

Purpose. A model takes a batch of inputs as one array. Shape and dtype mistakes at that boundary cause most of the first-week bugs.

An image from a file is (height, width, channels). A PyTorch model wants a batch as (N, channels, height, width), with numbers between 0 and 1 as float32. So the batch goes through the moves above: reorder the axes, change the dtype, scale. The same lines exist in PyTorch with other names.

Check the shape at every boundary

A shape error rarely appears where the mistake was made: it shows up many lines later, as a message about two shapes that can't be multiplied. Two habits catch it early. Print shape and dtype right after any step that changes them. And write the shape you expect as an assert at the entrance of a function, so a wrong batch stops at the door with a message that names the problem:

import numpy as np

def check_batch(x):
    assert x.ndim == 4, f"want 4 axes, got {x.ndim}"
    assert x.shape[1] == 3, f"want channels in axis 1, got shape {x.shape}"
    return x

print(check_batch(np.zeros((2, 3, 4, 6))).shape)

An assert costs nothing when the shapes are right, and when they are wrong it points at the function that was handed the wrong thing, not at a line deep inside the model.

Quick check
A batch of images has shape (8, 32, 32, 3). A model wants (N, C, H, W). Which call fixes it?
  1. reshape(8, 3, 32, 32)
  2. unsqueeze(1)
  3. .T
  4. permute(0, 3, 1, 2)
Show the answer

permute(0, 3, 1, 2)

permute moves the axes into the new order without touching the numbers: channels (axis 3) go to position 1. reshape would keep the numbers in memory order and scramble the pixels.

Goes deeper in

Use these tools on pictures in Project: images are arrays, on tables in DataFrames with pandas, and on tensors with gradients in PyTorch tensors.

In your own words
A model input should be (N, C, H, W) and yours is (N, H, W, C). Name two ways to get the right shape and say which is wrong for pixels.

Write a sentence or two, then compare it with a good answer in the lesson.

Practice what you just read

  • 8 code examples you can edit and run
  • a practice exercise that checks your code
  • a 5-question quiz
  • 9 review cards for spaced repetition

Try a quiz question

What does reshape(2, -1) do to an array of 12 numbers?

  1. Makes it (2, 6)
  2. Makes it (2, 12)
  3. Raises an error
  4. Makes it (12, 2)

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson

Keep learning

Where this lesson leads.

575 lessons in thirteen phases. 176 are free without an account, and a free account opens the rest. See every lesson.

Phase 0 · about 32 hours of study

Tools

Python, the terminal and calling APIs 46 lessons All free
  1. Your first program FreeRun Python, print output, and read an error message without panicking.24 min
  2. Typing code exactly FreeBrackets, quotes, indentation and the look-alike characters that stop a program.26 min
  3. Variables FreeGive values names so your program can remember and reuse them.24 min
  4. Numbers and arithmetic FreeIntegers, decimals, rounding, and the math behind every metric.30 min
  5. Strings and f-strings FreeWork with text: join it, slice it, clean it, and format it.30 min
  6. Decisions with if/else FreeCompare values and choose what happens next.35 min
  7. Fractions, ratios and rates FreeShares, rates, percentage points and ratios, with Python doing the arithmetic.35 min
  8. Ranges and edges in code FreeAt least, under and between as symbols; intervals with each end in or out; bands with no gaps.32 min
  9. Lists FreeKeep many values in order, then index, slice and change them.28 min
  10. Files, folders and paths FreeWhat a file's name tells you, paths as directions through folders, and where a relative path starts.30 min
  11. Loops FreeRepeat work over every item without copy-pasting code.35 min
  12. Formulas: order, powers and rearranging FreeWork a calculation in the right order, fill in a formula, and turn it around to find what's missing.24 min
  13. Loops that add and count: sequences, sums and combinations FreeSequences that add or multiply each step, a formula for any term, and adding a sequence up.34 min
  14. Dictionaries FreeLook values up by name: the shape of almost all real-world data.40 min
  15. Functions FreePackage steps into reusable, testable pieces with inputs and outputs.35 min
  16. While loops FreeRepeat until something changes: goals reached, answers valid, tasks done.38 min
  17. Functions as values FreeStore functions, pass them in with key=, and accept any number of arguments.40 min
  18. Comprehensions FreeFilter and transform a whole collection in one readable line.38 min
  19. Errors and exceptions FreeRead tracebacks, and handle the failures you expect.38 min
  20. Finding bugs when nothing crashes FreeA method for wrong answers: expected against actual, reproduce, look inside, fix one thing.36 min
  21. Sets: unique items and fast lookups FreeDuplicates, fast membership, set operations and comparing two lists.34 min
  22. Modules and imports FreeUse Python's standard library instead of reinventing it.38 min
  23. Classes and objects FreeBundle data with the functions that work on it.38 min
  24. Inheritance and class attributes FreeBuild classes on other classes, and share values across every object.35 min
  25. Review: find the slip (Python core) FreeTen slips that run without an error, found by working a tiny input by hand, and the construct sheet for NumPy and PyTorch code.30 min
  26. JSON FreeThe text format APIs and LLMs use to exchange structured data.34 min
  27. Files and CSV data FreeRead and write files, and turn a spreadsheet export into Python data.33 min
  28. NumPy arrays FreeDo math on whole columns at once, the foundation of every ML library.30 min
  29. NumPy in practice: shapes, types and picking values FreeReshape, view and copy, where and clip, top-k, stacking and the (N, C, H, W) batch a model expects.35 min
  30. DataFrames with pandas FreeLoad a table, pick rows and columns, and summarize groups in a few lines.40 min
  31. Charts with Matplotlib FreeLine, bar, scatter, histogram and box plots, labeled and honest.42 min
  32. Project: images are arrays FreeGray, crop, brighten, watermark and palette tools built on NumPy and Pillow.45 min
  33. The terminal FreeNavigate folders and run programs by typing commands.30 min
  34. Version control with git FreeSave snapshots of your work, undo mistakes, and share code.30 min
  35. Packages and environments FreeInstall libraries with pip and keep projects isolated with venv.25 min
  36. Python on your own computer FreeInstall Python and VS Code, make a virtual environment, then run, debug and test a project.45 min
  37. How APIs work FreeRequests, responses, methods, status codes and headers.30 min
  38. Calling APIs from Python FreeSend requests, read JSON responses, and handle failures.35 min
  39. API keys and secrets FreeAuthenticate safely and keep keys out of your code.25 min
  40. Your first LLM call FreeSend messages to a model and read its reply, tokens and stop reason.30 min
  41. Capstone: ad-copy reviewer FreeCombine files, functions, JSON and an LLM call into one small tool.40 min
  42. Dates, times and time zones Freedatetime, timedelta, formats, time zones and the traps when clocks change.42 min
  43. Regular expressions FreeFind, check, extract and clean text patterns with the re module.42 min
  44. Sending email and text messages FreeEmailMessage, smtplib, mail merge and an SMS API, with secrets kept out of code.42 min
  45. Web pages: HTML and CSS FreeRead and write HTML, what forms send, CSS selectors and escaping.42 min
  46. Scraping web pages FreeBeautiful Soup, one record per item, pagination and robots.txt.45 min
Part of a free course 32 lessons

Free Python for AI course

Learn Python from your first line of code to your first call to a language model, with nothing to install.

Part of a free course 127 lessons

Free Python developer course

Go from your first line of code to working as a Python developer: projects, testing, data, databases, web apps and APIs, all in your browser.

Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write