This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 5: Array manipulation

Array concatenation

face Josiah Wang

Similar to np.stack(), you can use the np.concat() function to concatenate arrays into a single array along an existing axis.

Note that unlike np.stack(), the arrays can be of different sizes (but only along the axis that you specify). If it does not concatenate to a proper array size, then it is invalid!

Array concatenation

>>> x = np.array([[1, 2], [3, 4]])
>>> y = np.array([[5, 6]])
>>> z = np.concatenate((x, y), axis=0)  # axis=0 is the default
[[1 2]
 [3 4]
 [5 6]]
>>> z = np.concatenate((x, y.transpose()), axis=1)
[[1 2 5]
 [3 4 6]]

How is concatenate different from stack?

Note that np.concatenate() concatenates arrays on an existing axis, while np.stack() that you saw earlier concatenates arrays on a new axis. So np.stack() actually produces an extra axis. Compare the following examples:

>>> x = np.array([[1, 2, 3], [4, 5, 6]])
>>> y = np.array([[7, 8, 9], [10, 11, 12]])
>>> print(x)
[[1 2 3]
 [4 5 6]]
>>> print(y)
[[ 7  8  9]
 [10 11 12]]
>>> z = np.concatenate((x, y), axis=0)
>>> print(z.shape)
(4, 3)
>>> print(z)
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
>>> z = np.stack((x, y), axis=0)
>>> print(z.shape)
(2, 2, 3)
>>> print(z)
[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]

So, concatenate ‘merges’ the rows into the same array, while stack creates a whole new axis for the purpose.

I recommend using np.stack() for stacking 1D arrays (i.e. vectors), as I showed in the previous page. You can also stack a sequence of 2D arrays into 3D. In other cases, it might get really confusing (try stacking along axis=1 or axis=2 🤯🤯)! So for higher dimensions, use np.concatenate()!

You might sometimes struggle a bit getting concatenation to work correctly - just step back and think carefully, and make sure that the array dimensions match up as intended! Draw your arrays on paper if needed!