Numpy 将二维数组与一维数组连接起来 [英] Numpy concatenate 2D arrays with 1D array
问题描述
我正在尝试连接 4 个数组,一个一维形状数组 (78427,) 和 3 个二维形状数组 (78427, 375/81/103).基本上这是 4 个具有 78427 张图像特征的数组,其中一维数组每个图像只有 1 个值.
我尝试按如下方式连接数组:
<预><代码>>>>打印 X_Cscores.shape(78427, 375)>>>打印 X_Mscores.shape(78427, 81)>>>打印 X_Tscores.shape(78427, 103)>>>打印 X_Yscores.shape(78427,)>>>np.concatenate((X_Cscores, X_Mscores, X_Tscores, X_Yscores),axis=1)这会导致以下错误:
<块引用>回溯(最近一次调用最后一次):文件",第 1 行,在ValueError:所有输入数组必须具有相同的维数
问题似乎是一维数组,但我真的不明白为什么(它也有 78427 个值).我试图在连接它之前转置一维数组,但这也不起作用.
有关连接这些数组的正确方法的任何帮助将不胜感激!
尝试连接 X_Yscores[:, None]
(或 X_Yscores[:, np.newaxis]
作为imaluengo 建议).这将从一维数组中创建一个二维数组.
示例:
A = np.array([1, 2, 3])打印 A.shape打印 A[:, None].shape
输出:
(3,)(3,1)
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!
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)
这篇关于Numpy 将二维数组与一维数组连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!