Review
Core idea
A NumPy array is a shaped numerical container.
Before computing, inspect:
Arrays versus lists
List addition concatenates lists. Array addition adds element by element.
[1, 2] + [10, 20]
produces:
[1, 2, 10, 20]
np.array([1, 2]) + np.array([10, 20])
produces:
[11 22]
Creating arrays
Useful constructors:
Shape and axis
For a matrix with shape (2, 3):
- axis
0indexes the two rows; - axis
1indexes the three columns; A.sum(axis=0)produces one value per column;A.sum(axis=1)produces one value per row.
Always attach meaning to the axes.
Indexing
: means all values along that axis.
Views and copies
Basic slices share data with the original array.
Use:
part = array_slice.copy()
when you want a separate array before mutation.
Reductions
Common reductions:
Use axis=... to summarize along a specific axis.
Array errors
When an array operation fails, inspect:
- shape;
- dtype;
- the smallest example that still fails.