[concept]NumPy Foundations
Reshape and axis
# theory
reshape
Reshape rearranges how a 1D buffer of numbers is interpreted. It doesn't move data; it changes the strides used to walk it.
import numpy as np
a = np.arange(12) # shape (12,)
b = a.reshape(3, 4) # shape (3, 4); same 12 numbers
c = a.reshape(4, 3) # shape (4, 3); same 12 numbers
a.reshape(-1, 4) lets NumPy compute the missing dimension. Useful when you know the column count but not the row count.
axis = the dimension being collapsed
axis=0 collapses rows (you get one value per column). axis=1 collapses columns (one value per row).
m = np.array([
[1, 2, 3],
[4, 5, 6],
])
m.sum() # 21 (whole thing)
m.sum(axis=0) # [5, 7, 9] <- columnwise totals
m.sum(axis=1) # [6, 15] <- rowwise totals
The trick that makes this stick: axis names the dimension that disappears. axis=0 collapses dimension 0 (rows), so the row dimension goes away and you're left with one number per column.
transpose & stacking
m.T # transpose, shape (3, 2)
np.vstack([m, m]) # stack vertically, shape (4, 3)
np.hstack([m, m]) # stack horizontally, shape (2, 6)
np.concatenate([m, m], axis=0) # same as vstack# examples [3]
If you know one dimension, pass -1 for the other and NumPy fills it in.
Same array, two different aggregates, depending on which dimension you collapse.
Reshaping is for the same data in a new shape. Transpose flips rows and columns. Stack glues separate arrays together.
# challenges [2]