Analyze Data/Python Libraries

numpy-concatenate

Naranjito 2023. 6. 7. 16:27

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

 

  • axis=0

It concatenates along the row axis.

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])

np.concatenate((a, b), axis=0)
>>>
array([[1, 2],
       [3, 4],
       [5, 6]])

 

  • axis=1

It concatenates along the column axis.

np.concatenate((a, b), axis=1)
>>>
ValueError: all the input array dimensions 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 1

b.T
>>>
array([[5],
       [6]])
       
np.concatenate((a, b.T), axis=1)
>>>
array([[1, 2, 5],
       [3, 4, 6]])

 

  • axis=None
np.concatenate((a, b), axis=None)
>>>
array([1, 2, 3, 4, 5, 6])