Numpy concatenate 2D arrays with 1D array
Solution 1
Try concatenating X_Yscores[:, None]
(or X_Yscores[:, np.newaxis]
as imaluengo suggests). This creates a 2D array out of a 1D array.
Example:
A = np.array([1, 2, 3])
print A.shape
print A[:, None].shape
Output:
(3,)
(3,1)
Solution 2
I am not sure if you want something like:
a = np.array( [ [1,2],[3,4] ] )
b = np.array( [ 5,6 ] )
c = a.ravel()
con = np.concatenate( (c,b ) )
array([1, 2, 3, 4, 5, 6])
OR
np.column_stack( (a,b) )
array([[1, 2, 5],
[3, 4, 6]])
np.row_stack( (a,b) )
array([[1, 2],
[3, 4],
[5, 6]])
Solution 3
You can try this one-liner:
concat = numpy.hstack([a.reshape(dim,-1) for a in [Cscores, Mscores, Tscores, Yscores]])
The "secret" here is to reshape using the known, common dimension in one axis, and -1 for the other, and it automatically matches the size (creating a new axis if needed).
KCDC
Updated on July 09, 2022Comments
-
KCDC almost 2 years
I am trying to concatenate 4 arrays, one 1D array of shape (78427,) and 3 2D array of shape (78427, 375/81/103). Basically this are 4 arrays with features for 78427 images, in which the 1D array only has 1 value for each image.
I tried concatenating the arrays as follows:
>>> print X_Cscores.shape (78427, 375) >>> print X_Mscores.shape (78427, 81) >>> print X_Tscores.shape (78427, 103) >>> print X_Yscores.shape (78427,) >>> np.concatenate((X_Cscores, X_Mscores, X_Tscores, X_Yscores), axis=1)
This results in the following error:
Traceback (most recent call last): File "", line 1, in ValueError: all the input arrays must have same number of dimensions
The problem seems to be the 1D array, but I can't really see why (it also has 78427 values). I tried to transpose the 1D array before concatenating it, but that also didn't work.
Any help on what's the right method to concatenate these arrays would be appreciated!
-
Imanol Luengo almost 9 yearsJust to point out that
A[:, np.newaxis]
has the same behaviour thanA[:, None]
and can sometimes be more intuitive (actuallynp.newaxis == None
). -
kRazzy R about 6 yearsCould help here : stackoverflow.com/questions/48676461/…
-
kRazzy R about 6 yearsany thoughts on this : stackoverflow.com/questions/48676461/…
-
kRazzy R about 6 yearshowever this is true only if both have same dimension. In most cases I am ending up with Array A having shape (8400,) and Array B having shape (8399, 21). How do I truncate/delete the last few rows of A so that both A and B have same shapes like (8399,) and (8399, 21) . Please advise.
-
Ben Farmer about 6 yearsA generalisation: np.concatenate([a.reshape(*shape,-1) for a in my_arrays],axis=-1), where 'shape' is the shape of known dimensions except the last.
-
deadcode about 5 years
np.newaxis
is intuitive but I still can't understand whyA[:, None]
works. Can anyone help me understand this? -
Falko about 5 yearsIt works because "
newaxis
is an alias forNone
" and usingNone
for indexing tells NumPy to add a dimension. So the 1D array is converted into a 2D array, which has axes 0 and 1.