pyodide: loading…

[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]

# example 01 · reshape with -1 for the unknown dimension

If you know one dimension, pass -1 for the other and NumPy fills it in.

1
2
3
4
5
6
🐍
Loading PythonSetting up pandas & numpy...
# example 02 · axis=0 vs axis=1 for aggregates

Same array, two different aggregates, depending on which dimension you collapse.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
🐍
Loading PythonSetting up pandas & numpy...
# example 03 · transpose and stack

Reshaping is for the same data in a new shape. Transpose flips rows and columns. Stack glues separate arrays together.

1
2
3
4
5
6
7
8
9
10
🐍
Loading PythonSetting up pandas & numpy...

# challenges [2]

# challenge 01/02todo
Take np.arange(24) and reshape it into a 4-row matrix without specifying the column count. Print the shape in the format 'shape: (R, C)' (with literal parens).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
🐍
Loading PythonSetting up pandas & numpy...
# challenge 02/02todo
You have a 3x4 sales matrix [[10,20,30,40],[15,25,35,45],[5,15,25,35]]. Compute totals per column and per row. Print 'col totals: [a, b, c, d]' and 'row totals: [e, f, g]' with the actual numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
🐍
Loading PythonSetting up pandas & numpy...