NumPy · Chapter 7 of 18

Reshape and axes

By the end of this chapter you can give the same numbers a new shape with reshape (and let NumPy work out one length with -1), say when that is a view and when it is a copy, say what each axis of an array means, move axes with .T, transpose, swapaxes and moveaxis, and add or remove axes of length 1 with np.newaxis, np.expand_dims and np.squeeze. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Shapes and maths
  • 4 tasks
  • NumPy
  • In the all labs plan

The first 2 chapters of NumPy are free. This one comes with the all labs plan.

Why you need this

Rukmini wants the last 30 days side by side: did the plant make less at 10:00 this week than the week before? blocks_30d.csv has one line a block, day after day, so its plant_kw column comes out of np.loadtxt as 2,880 numbers in one long row. To compare days she needs them as 30 rows of 96. Meenal settles the exchange by the hour, four blocks at a time. Imran reports by inverter, and an inverter is a rectangle of 10 rows by 20 columns inside the (40, 60) grid. Bhanwar thinks panel first: "show me (21, 12)'s day", and the day's array puts time first.

With what you know, each of these is slicing in a loop: day d of the 30 is kw[d * 96:(d + 1) * 96], thirty slices kept in a list, and a list of arrays cannot be sliced down a column ("block 40 of every day") the way an array can. The numbers are already in the right order. All that is missing is a different shape over the same block of memory, and NumPy can give an array a new shape without moving a single number.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    by_day.py: thirty days, one row each

    blocks_30d.csv has one line a block: day 1's 96 blocks, then day 2's, and so on, so one of its columns read with np.loadtxt (for example plant_kw, column 4) is 2,880 numbers in a row. Write by_day(column) in by_day.py. column is a 1-D float array of a whole number of days of 96 blocks each. Return the same numbers with one row a day and one column a block, where [d, b] is day d's block b: shape (30, 96) for the 2,880 values, (7, 96) for 7 days, (1, 96) for one day.

    Return a view of column, not a copy. The Check tries plant_kw's 30 days, its last 7 days, and one day of 96 numbers, so let NumPy work out the number of rows.

  2. Task 2

    inverter_view.py: the day, inverter by inverter

    Each plant block is ten rows (rows 0 to 9 are block A, 10 to 19 block B, and so on), and each block's columns are three inverters of 20 columns (columns 0 to 19 of block A are INV01, 20 to 39 INV02, 40 to 59 INV03). Write inverter_view(day) in inverter_view.py. day has shape (n, 40, 60). Return the same readings with shape (n, 4, 10, 3, 20), where [t, k, r, i, c] is time block t, plant block k, row r inside that block, inverter i of that block and column c inside that inverter: for the day it is (96, 4, 10, 3, 20), and [48, 2, 1, 0, 12] is day[48, 21, 12].

    Return a view, not a copy. The Check passes the day and a made-up day of 2 blocks, so take n from the array.

  3. Task 3

    panel_first.py: every panel's day

    Write panel_first(day) in panel_first.py. day has shape (n, 40, 60), time first. Return the same readings with shape (40, 60, n), where [r, c, t] is day[t, r, c], so [r, c] is one panel's n readings in time order: (40, 60, 96) for the day.

    Return a view, not a copy. A reshape to (40, 60, n) gets the shape right and puts the wrong numbers at [r, c]: the axes themselves must move. The Check passes the day and a made-up day of 5 blocks.

  4. Task 4 · interview question

    Interview: drone_scan.py, the drone that flew down columns

    The drone measures a block's panels flying down the columns: column 0 from row 0 to row 39, then column 1 from row 0 to row 39, and so on. Its scan is a 1-D array of 2,400 readings in that order. grid_from_scan(scan) in drone_scan.py must return them as the plant's grid: an array of shape (40, 60) where [r, c] is the panel at row r and column c.

    Bhanwar's version returns shape (40, 60) with the panels in the wrong places: [0, 1] holds the reading of the panel at row 1, column 0. Fix it. The Check tries scans of the noon grid, of block 60 and of numbered panels.

    Imran will ask: reshape(60, 40) and .T both give 60 rows of 40, so why are they not the same, and which did this scan need?

What you will learn

  • reshape: the same elements, in the same C order, read with a new shape of the same size; -1 for "work this length out".
  • A view when it can: a reshape normally copies nothing, so it costs nanoseconds.
  • What an axis is: axis 0, 1 and 2 of the day, and negative axes that count from the end.
  • .T and transpose: the axes swap, and the elements go with them, which reshape never does.
  • swapaxes and moveaxis: move one axis and keep the others in order.
  • np.newaxis, np.expand_dims and np.squeeze: add or remove an axis of length 1.

More in Shapes and maths

Build "Reshape and axes" yourself

DhoopGrid runs a solar plant in the Thar desert with 2,400 panels. You turn its raw readings into answers. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of NumPy are free. This one comes with the all labs plan.