NumPy Array Join
Joining puts the contents of two or more arrays into one. In SQL you join tables by a key; in NumPy you join arrays along an axis: glue them end to end, one under the other, or side by side.
np.concatenate
np.concatenate takes a sequence (list or tuple) of arrays and an axis (default 0):
import numpy as np
morning = np.array([12, 15, 11])
evening = np.array([20, 18])
print(np.concatenate((morning, evening)))
jan = np.array([[1, 2],
[3, 4]])
feb = np.array([[5, 6],
[7, 8]])
print(np.concatenate((jan, feb))) # axis=0: stack rows below
print(np.concatenate((jan, feb), axis=1)) # axis=1: add columns to the right[12 15 11 20 18]
[[1 2]
[3 4]
[5 6]
[7 8]]
[[1 2 5 6]
[3 4 7 8]]
The rule: all arrays must have the same shape except along the joining axis. Joining a 2×2 with a 3×2 along axis=0 is fine (result 5×2), but along axis=1 it fails:
import numpy as np
a = np.ones((2, 2))
b = np.zeros((3, 2))
print(np.concatenate((a, b)).shape)
try:
np.concatenate((a, b), axis=1)
except ValueError as err:
print("ValueError:", err)(5, 2)
ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 2 and the array at index 1 has size 3
np.stack: join along a new axis
concatenate keeps the number of dimensions. np.stack adds a new dimension, so the arrays must all have exactly the same shape:
import numpy as np
x = np.array([1, 2, 3])
y = np.array([10, 20, 30])
print(np.stack((x, y))) # new axis 0 -> shape (2, 3)
print(np.stack((x, y), axis=1)) # new axis 1 -> shape (3, 2): pairs
print(np.stack((x, y), axis=1).shape)[[ 1 2 3]
[10 20 30]]
[[ 1 10]
[ 2 20]
[ 3 30]]
(3, 2)
np.stack((x, y), axis=1) is a neat way to turn two lists of coordinates into a list of (x, y) points.
Helper functions: hstack, vstack, dstack, column_stack
These are shortcuts with intuitive names:
| Function | For 1-D inputs | For 2-D inputs |
|---|---|---|
np.hstack |
joins end to end | joins side by side (axis 1) |
np.vstack |
each array becomes a row | joins one under the other (axis 0) |
np.dstack |
depth: shape (1, n, k) |
along a third axis (axis 2) |
np.column_stack |
each array becomes a column | like hstack |
import numpy as np
names_id = np.array([101, 102, 103])
scores = np.array([88, 92, 79])
print(np.hstack((names_id, scores)))
print(np.vstack((names_id, scores)))
print(np.column_stack((names_id, scores)))
print(np.dstack((names_id, scores)).shape)[101 102 103 88 92 79]
[[101 102 103]
[ 88 92 79]]
[[101 88]
[102 92]
[103 79]]
(1, 3, 2)
A practical example: building a table
Suppose three measurement series arrive separately and you want a table with one row per sample and a header row of sensor ids:
import numpy as np
temp = np.array([21.5, 22.0, 22.4])
humidity = np.array([40.0, 42.5, 41.0])
pressure = np.array([1012.0, 1011.5, 1013.2])
table = np.column_stack((temp, humidity, pressure))
print(table)
print("shape:", table.shape)
extra_row = np.array([[23.1, 39.5, 1012.8]]) # note: 2-D, one row
table = np.vstack((table, extra_row))
print(table[-1], table.shape)[[ 21.5 40. 1012. ]
[ 22. 42.5 1011.5]
[ 22.4 41. 1013.2]]
shape: (3, 3)
[ 23.1 39.5 1012.8] (4, 3)
Tip: Every join creates a brand-new array and copies all data. Appending to an array inside a loop is therefore slow. Collect pieces in a Python list and join once at the end, or create the full array with
np.zerosup front and fill it.
Test yourself
What shape does np.stack((np.zeros(4), np.ones(4))) have?
- (8,)
- (2, 4)
- (4, 2)
- (4,)
Two arrays of shape (2, 3) and (2, 5) can be concatenated along which axis?
- axis=0
- axis=1
- Both
- Neither
Which function turns 1-D arrays into the columns of a 2-D table?
- np.vstack
- np.column_stack
- np.concatenate
- np.dstack