Numpy将二维数组与一维数组连接起来

时间:2020-11-27 21:19:27

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.

我正在尝试连接4个阵列,一个一维阵列(78427,)和3个二维阵列(78427,375/81/103)。基本上这是4个具有78427图像特性的数组,其中1D数组对每个图像只有1个值。

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

回溯(最近一次调用):File“”,第1行,在ValueError中:所有输入数组必须具有相同的维数

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.

问题似乎是1D数组,但是我不知道为什么(它也有78427值)。我试着在连接1D数组之前先把它转置,但那也不管用。

Any help on what's the right method to concatenate these arrays would be appreciated!

任何关于连接这些数组的正确方法的帮助都将受到感激!

3 个解决方案

#1


11  

Try concatenating X_Yscores[:, None] (or A[:, np.newaxis] as imaluengo suggests). This creates a 2D array out of a 1D array.

尝试连接x_yscore [:, None](或A[:, np)。newaxis]imaluengo表明)。这将从一维数组中创建一个2D数组。

Example:

例子:

A = np.array([1, 2, 3])
print A.shape
print A[:, None].shape

Output:

输出:

(3,)
(3,1)

#2


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).

这里的“秘密”是使用一个轴中已知的公共维度和另一个轴中的-1进行重构,并自动匹配大小(如果需要,创建一个新的轴)。

#3


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]])

#1


11  

Try concatenating X_Yscores[:, None] (or A[:, np.newaxis] as imaluengo suggests). This creates a 2D array out of a 1D array.

尝试连接x_yscore [:, None](或A[:, np)。newaxis]imaluengo表明)。这将从一维数组中创建一个2D数组。

Example:

例子:

A = np.array([1, 2, 3])
print A.shape
print A[:, None].shape

Output:

输出:

(3,)
(3,1)

#2


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).

这里的“秘密”是使用一个轴中已知的公共维度和另一个轴中的-1进行重构,并自动匹配大小(如果需要,创建一个新的轴)。

#3


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]])