Reshape and Transpose
Regroup a fixed number of entries with reshape or exchange existing two-dimensional axes with transpose.
A flat sequence can contain all the values we need without showing what a row
or column means. NumPy can regroup those entries with reshape. When data has
the right groups but reversed axes, transpose can exchange the axes instead.
These operations may produce the same shape while making different claims
about the data.
Regroup Flat Values with Reshape
Start with six readings in observation order. Every two consecutive values belong to one observation:
[[18.5 20. ]
[21.5 22. ]
[19. 24. ]]
(3, 2)
reshape(3, 2) regroups the six entries as three observations by two sensors.
It does not sort or calculate new readings. The entry count is preserved:
6 entries = 3 × 2 entries
A requested shape whose sizes do not multiply to six cannot hold exactly these values, so NumPy raises an error rather than dropping or inventing entries.
Q1. Check whether a reshape can work
Which requested shape can hold all six entries from flat?
Select one choice, then check.
HintPreserve the entry count
The product of the new axis lengths must equal flat.size, which is 6.
SolutionUse a shape whose product is six
Shape (2, 3) contains 2 × 3 = 6 positions. The other choices require
eight or nine entries.
Infer One Axis Length
When one axis length is known, -1 asks NumPy to infer the other:
(3, 2)
Three observation rows must share six entries, so NumPy infers two sensor
positions per row. Use at most one -1 in a reshape: two unknown lengths would
not state enough information to choose one result.
Inference checks arithmetic, not meaning. NumPy can also form shape (2, 3),
but it cannot know whether 2 means sensors, observations, or something else.
The program's data contract must supply those axis names.
Q2. Infer one dimension
Complete the reshape so the twelve values become four rows while NumPy infers the number of columns.
Editable Python
Ready to run.
HintLeave one length to NumPy
Replace values with values.reshape(4, -1).
SolutionState one axis and infer the other
table = values.reshape(4, -1)
Since 12 / 4 = 3, the result has shape (4, 3).
Exchange Two Axes with Transpose
Suppose a source gives one row per sensor and one column per observation:
(2, 3)
The values are already grouped by sensor. To represent observations as rows and sensors as columns, exchange the two axes:
[[18.5 20. ]
[21.5 22. ]
[19. 24. ]]
(3, 2)
For a two-dimensional array, .T turns row positions into column positions
and column positions into row positions. The entry at [sensor, observation]
becomes the entry at [observation, sensor]. The same six values remain.
Reshape and transpose are not interchangeable:
| Operation | What it changes | Question it answers |
|---|---|---|
flat.reshape(3, 2) | groups a flat order into axis lengths | How should this sequence be divided into rows and columns? |
reversed_axes.T | exchanges the existing row and column axes | The groups are correct; should their axes trade places? |
Shape alone cannot prove that either result has the intended meaning. A
transpose repairs the reversed table only because the source contract says its
axes are (sensors, observations). If the source rows meant unrelated groups,
the same operation would preserve the numbers but make a false claim about
them.
Q3. Choose transpose for reversed axes
The supplied array has axes (sensors, observations) and shape (2, 3).
Complete the program so readings has axes (observations, sensors) and shape
(3, 2) without regrouping the source's sensor rows.
Editable Python
Ready to run.
HintPreserve the existing groups
The source rows already mean sensors. Use the two-dimensional transpose
.T to make those sensor groups become columns.
SolutionExchange rows and columns
readings = reversed_axes.T
The result has shape (3, 2). Each row is now one observation, and each
column is one sensor.
Reshape groups a fixed entry order into new axis lengths; transpose exchanges existing axes. Both preserve the entry count, but neither can decide what an axis means. The next lesson uses the corrected layout to summarize values along a named axis.