Numpy concatenate()
is not a database join. It is basically stacking Numpy arrays either vertically or horizontally.
np.concatenate((a1, a2, ...), axis=0)
(a1, a2, ...)
parameter is used to pass more than one Numpy arrays. Here you pass arrays in the form of Python tuple or Python list.
axis
parameter is used to specify the axis along which you want to perform concatenation. Its default value is 1.
import numpy as np a = np.array([1, 2, 3, 4, 5]) b = np.array([16, 17, 18, 19, 20]) print('Concatenate 1-D array:\n', np.concatenate([a, b]))
Output
Concatenate 1-D array: [ 1 2 3 4 5 16 17 18 19 20]
As you can see in the above output, np.concatenate()
has concatenated two Numpy arrays. In 1-dimensional array, it is optional to provide axis parameter to concatenate() function.
import numpy as np a = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) b = np.array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]) print('Concatenate along axis=0:\n', np.concatenate((a,b), axis=0)) print('Concatenate along axis=1:\n', np.concatenate([a,b], axis=1))
Output
Concatenate along axis=0: [[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15] [16 17 18 19 20]] Concatenate along axis=1: [[ 1 2 3 4 5 11 12 13 14 15] [ 6 7 8 9 10 16 17 18 19 20]]
When axis=0 is passed to concatenate() function then it performs vertical stacking. In other words, it is called concatenating Numpy arrays vertically.
When axis=1 is passed to concatenate() function then it performs horizontal stacking. In other words, it is called concatenating Numpy arrays horizontally.