a = np.array([1, 2, 3])
aa = np.array([1], [2], [3])
b = np.array([1, 2, 3])
bb = np.array([1], [2], [3])
np.concatenate((a, b), axis = 1)
array([1, 2, 3, 1, 2, 3]) # It's ok, that's what I was expecting
np.concatenate((a, b), axis = 0)
array([1, 2, 3, 1, 2, 3]) # It's not ok, that's not what I was expecting
I was expecting:
array([[1, 2, 3],
[1, 2, 3]])
even with aa and bb I get the same inconsistency. so is there a simple solution to concatenate along axis 0 two one-dimensional arrays?
Join a sequence of arrays along an existing axis. The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default). The axis along which the arrays will be joined.
Joining Arrays Using Stack Functions We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking. We pass a sequence of arrays that we want to join to the stack() method along with the axis. If axis is not explicitly passed it is taken as 0.
concat() The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
Note that a
and b
are both one-dimensional; there's no axis 1 to concatenate along. You want vstack
:
>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = a.copy()
>>> np.vstack([a,b])
array([[1, 2, 3],
[1, 2, 3]])
Alternatively, you could reshape a
and b
first:
>>> np.concatenate([a[np.newaxis,:],b[np.newaxis,:]],axis = 0)
array([[1, 2, 3],
[1, 2, 3]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With