Reshape and Flatten Arrays
Changing dimensions without losing data.
Same Data, New Shape
An array is just numbers plus a shape. Reshaping rearranges those numbers into new dimensions without copying or changing a single value.
Calling reshape
Use reshape to lay 12 numbers out as a 3 by 4 grid. The total count must stay the same, or NumPy raises an error.
import numpy as np
a = np.arange(12)
b = a.reshape(3, 4)All lessons in this course
- Reshape and Flatten Arrays
- Sum, Mean, and the Axis Trick
- Boolean Masks for Selection
- Random Numbers and Seeds