Published on

Understanding Numpys Concatenate Axis Field

Authors

I have been getting a feel for the Numpy API by working through the Numpy exercises on HackerRank.

One thing that stumped me initially is trying to understand what the axis parameter means with the numpy.concatenate function.

In the official documentation, it is described as:

The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.

To better understand this I started by modifying the HackerRank concatenate exercise examples a bit (to make it clear what elements come from where) and outputing these examples:

array_1 = numpy.array([[1, 2, 3], [4, 5, 6]])
array_2 = numpy.array([[0, 0, 0], [7, 8, 9]])

print('array_1\n', array_1, '\n\narray_2\n', array_2)

This outputs:

array_1
 [[1 2 3]
 [4 5 6]]

array_2
 [[0 0 0]
 [7 8 9]]

For clarity the first array as a table would be:

123
456

The second array would be:

000
789

I then ran the following code:

print('axis = 0\n', numpy.concatenate((array_1, array_2), axis=0))
print('\n\naxis = 1\n', numpy.concatenate((array_1, array_2), axis=1))

This outpus:

axis = 0
 [[1 2 3]
 [4 5 6]
 [0 0 0]
 [7 8 9]]


axis = 1
 [[1 2 3 0 0 0]
 [4 5 6 7 8 9]]

I adjusted the example a bit:

array_1 = numpy.array([[1, 2, 3], [4, 5, 6], [7,8,9]])
array_2 = numpy.array([[0, 0, 0], [10, 11, 12], [13,14,15]])

print('array_1\n', array_1, '\n\narray_2\n', array_2, '\n')
print('axis = 0\n', numpy.concatenate((array_1, array_2), axis=0))
print('\n\naxis = 1\n', numpy.concatenate((array_1, array_2), axis=1))

Which outputs:

array_1
 [[1 2 3]
 [4 5 6]
 [7 8 9]]

array_2
 [[ 0  0  0]
 [10 11 12]
 [13 14 15]]

axis = 0
 [[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [ 0  0  0]
 [10 11 12]
 [13 14 15]]


axis = 1
 [[ 1  2  3  0  0  0]
 [ 4  5  6 10 11 12]
 [ 7  8  9 13 14 15]]

For a 2D array there are only 2 axes: 0 and 1. For an ND array there are n axes the first is 0 the last is n-1.

An easier way to think of the axes is as labels for different parts of the table. For example for a 2D array:

  • 0 refers to the row side of the table i.e. where the rows start
    • When we use this in concatenate it says: "take the 2 tables and combine them along row side of the table."
  • 1 refers to the column side of the table i.e. where the columns start
    • When we use this in concatenate it says: "take the 2 tables and combine them along column side of the table."

To illustrate this I have created a simple diagram below:

numpy-axes

This article goes into Numpy axes in much more detail.