numpy将2D数组与1D数组连接 [英] Numpy concatenate 2D arrays with 1D array

查看:128
本文介绍了numpy将2D数组与1D数组连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接4个数组,一个1D形状的数组(78427,)和3个2D形状的数组(78427,375/81/103).基本上,这是4个具有78427个图像特征的阵列,其中1D阵列每个图像只有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)

这将导致以下错误:

回溯(最近通话最近): 文件",第1行,在 ValueError:所有输入数组的维数必须相同

Traceback (most recent call last): File "", line 1, in ValueError: all the input arrays must have same number of dimensions

问题似乎是一维数组,但是我真的看不出为什么(它也有78427个值).我试图在连接它之前转置1D数组,但是那也不起作用.

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!

推荐答案

尝试并置X_Yscores[:, None](或imaluengo建议使用X_Yscores[:, np.newaxis]).这会从1D数组中创建2D数组.

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

示例:

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

输出:

(3,)
(3,1)

这篇关于numpy将2D数组与1D数组连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆