Reshaping and Joining Arrays
🎧 Focus sounds
Soft beat sounds that many learners like while reading. Pick one, press Play, and keep studying.
Keep the volume low. These sounds are for comfort while studying, not a medical treatment. If you have epilepsy or are sensitive to pulsing sounds, use the background noise only.
In this lesson
Data rarely arrives in the shape you need. A model may want a column instead of a row, or you may need to glue two months of data together. This lesson covers the shape tools.
reshape: same numbers, new shape#
reshape(rows, cols) rearranges the elements without changing them. The total must stay the same: 12 elements can be 3 × 4, 4 × 3, 2 × 6 or 6 × 2, but not 5 × 2.
import numpy as np
a = np.arange(1, 13)
print(a.reshape(3, 4))
print(a.reshape(2, -1))
Output:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
-1 means “work it out for me”: with 12 elements and 2 rows, NumPy computes 6 columns.
A very common case in machine learning: turning a 1-D list of values into one column, because scikit-learn expects a table of features:
import numpy as np
sizes = np.array([650, 800, 1200])
X = sizes.reshape(-1, 1)
print(X)
print(X.shape)
Output:
[[ 650]
[ 800]
[1200]]
(3, 1)
Flatten and transpose#
import numpy as np
m = np.array([[1, 2, 3],
[4, 5, 6]])
print(m.flatten())
print(m.T)
print(m.T.shape)
Output:
[1 2 3 4 5 6]
[[1 4]
[2 5]
[3 6]]
(3, 2)
flatten()turns any shape back into one long row..T(transpose) swaps rows and columns: row 0 becomes column 0.
Joining arrays#
import numpy as np
jan = np.array([[10, 12], [14, 9]])
feb = np.array([[11, 15], [13, 8]])
print(np.vstack([jan, feb]))
print(np.hstack([jan, feb]))
Output:
[[10 12]
[14 9]
[11 15]
[13 8]]
[[10 12 11 15]
[14 9 13 8]]
vstack(vertical) puts arrays on top of each other: more rows.hstack(horizontal) puts them side by side: more columns.np.concatenate([a, b], axis=0)is the general version:axis=0stacks rows,axis=1stacks columns.
Remember
axis=0 goes down the rows, axis=1 goes across the columns. You’ll meet axis again with sums and averages.
Splitting#
import numpy as np
a = np.arange(10)
first, second = np.split(a, 2)
print(first, second)
Output:
[0 1 2 3 4] [5 6 7 8 9]
Practice#
- Reshape
np.arange(24)into 4 rows and let NumPy find the columns. - Stack the arrays
[1, 2, 3]and[4, 5, 6]into a 2 × 3 table. - What is the shape of the transpose of a (5, 2) array?
Summary#
| Task | Code |
|---|---|
| New shape, same data | a.reshape(r, c) (use -1 for one size) |
| One long row | a.flatten() |
| Swap rows and columns | a.T |
| Stack as rows / columns | np.vstack, np.hstack |
| Split into parts | np.split(a, n) |
Quick quiz
3 questions. Check what stuck and earn 2 XP for each right answer.
Progress is saved in this browser. Create a free account to keep it on every device.
Get new lessons in your inbox
One email when we publish a new tutorial or guide. No spam, unsubscribe in one click.