Review
Reconstruct the path from a numerical question to constructor, structure, selection, layout, reduction, storage evidence, and diagnosis.
Begin with the Numerical Question
A Python list remains useful for mixed values and collections that change
often. A NumPy array is useful when values form a regular numerical structure
and the program needs operations that follow that structure. For equal-length
lists, + joins them. For same-shaped numerical arrays, + adds values at
corresponding positions.
Choose a constructor from the information you have:
| Starting information | Constructor |
|---|---|
| existing values | np.array(...) |
| a required shape filled with zeros or ones | np.zeros(...) or np.ones(...) |
| start, excluded stop, and step | np.arange(...) |
| start, included stop, and number of values | np.linspace(...) |
State Structure and Meaning Together
The chapter's recurring table is:
Its shape is (3, 2), its ndim is 2, and its size is 6. The values
produce a floating-point dtype. Those facts describe storage, not the domain.
The program must also state that axis 0 means observations and axis 1
means sensors.
Predict the Selection and Its Shape
Position and condition answer different questions:
An integer index removes the selected axis. A slice preserves it. A boolean mask must match the positions it is used to select.
Separate Regrouping from Exchanging Axes
reshape(3, 2) groups six flat values into three observations with two sensor
values each. It preserves entry order and count, but the program still owns the
meaning assigned to the new axes. For a two-dimensional table, .T exchanges
the two existing axes. It does not merely choose another grouping.
Before either operation, write the intended source and result meanings. A numerically valid shape can still be wrong for the question.
Reduce the Axis You Intend to Combine
To compute one mean per sensor, combine the observation axis:
sensor_means = readings.mean(axis=0)
The result has shape (2,). Its first value is
(18.5 + 21.5 + 19.0) / 3, approximately 19.67; the second is 22.0.
keepdims=True is useful only when later code needs the reduced axis retained
with length one.
Treat Storage as a Relationship to Check
Basic slices and transposes are views that share storage with their source.
Boolean selection creates independent selected values. A reshape may share or
copy depending on the source layout, so use np.shares_memory when that
relationship matters. Call .copy() when independence is a requirement, then
verify the intended mutation behavior with a small example.
Diagnose from Evidence
When an array operation fails or produces a suspicious result:
- read the final error and locate the failing expression;
- inspect the relevant values,
shape,dtype, andndim; - state what every axis means;
- reproduce the assumption with a small array;
- repair the cause and verify the value, shape, and source data afterward.
Do not change an axis number, reshape, or dtype merely until the code runs. The repair must restore the intended data relationship.