Concat两个不同维度的数组numpy [英] Concat two arrays of different dimensions numpy

查看:380
本文介绍了Concat两个不同维度的数组numpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接两个numpy数组以添加额外的列:array_1(569, 30),而array_2(569, )

I am trying to concatenate two numpy arrays to add an extra column: array_1 is (569, 30) and array_2 is is (569, )

combined = np.concatenate((array_1, array_2), axis=1)

我认为如果设置axis=2可以正常工作,因此它将垂直连接.末尾应为569 x 31阵列.

I thought this would work if I set axis=2 so it will concatenate vertically. The end should should be a 569 x 31 array.

我得到的错误是ValueError: all the input arrays must have same number of dimensions

有人可以帮忙吗?

谢谢!

推荐答案

您可以使用numpy.column_stack:

np.column_stack((array_1, array_2))

这会将1-d数组隐式转换为2-d,因此等同于@umutto注释的np.concatenate((array_1, array_2[:,None]), axis=1).

Which converts the 1-d array to 2-d implicitly, and thus equivalent to np.concatenate((array_1, array_2[:,None]), axis=1) as commented by @umutto.

a = np.arange(6).reshape(2,3)
b = np.arange(2)

a
#array([[0, 1, 2],
#       [3, 4, 5]])

b
#array([0, 1])

np.column_stack((a, b))
#array([[0, 1, 2, 0],
#       [3, 4, 5, 1]])

这篇关于Concat两个不同维度的数组numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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