Guided Notes

Reshape, Join & Split

Changing an array's shape, flattening, joining arrays with concatenate and stack, and splitting them.

Reshaping

Slide 1 / 5

reshape() rearranges the same elements into a new shape:

arr = np.arange(1, 13) # 12 elements arr.reshape(4, 3) # 4 rows Γ— 3 columns arr.reshape(2, 3, 2) # 3-D

The total number of elements must stay the same: 12 elements fit 4Γ—3, 2Γ—6, or 2Γ—3Γ—2, but not 5Γ—3.

reshape usually returns a view of the original data, so it's fast even for huge arrays.

← Copy vs View & Iterating