ValueError: 所有输入数组必须具有相同的维数.堆叠向量 [英] ValueError: all the input arrays must have same number of dimensions . Stacking vectors

查看:29
本文介绍了ValueError: 所有输入数组必须具有相同的维数.堆叠向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个要追加的数组.这是我的向量示例:

l have three arrays to append. Here a sample of my vectors :

V1=array([ 0.03317591, -0.01624349, -0.01151019])
V2=array([[ 0.06865846, -0.00223798],
       [-0.02872752, -0.00369226],
       [-0.02063454, -0.00231726]])
V3=
array([[ 0.01160267,  0.12610824, -0.01634712,  0.01217519],
       [-0.00727594, -0.0501376 , -0.01641992,  0.00933081],
       [-0.05305551,  0.01195211,  0.04031831, -0.04476306]])

为了附加三个向量并得到一个向量,我做了以下事情:

in order to append the three vectors and get one vector l did the following :

new_v=np.hstack((V1,V2,V3))

我收到以下错误:

ValueError: all the input arrays must have same number of dimensions

然而:

 V2_V3=np.hstack((V2,V3))

有效,它返回:

array([[ 0.06865846, -0.00223798,  0.01160267,  0.12610824, -0.01634712,
         0.01217519],
       [-0.02872752, -0.00369226, -0.00727594, -0.0501376 , -0.01641992,
         0.00933081],
       [-0.02063454, -0.00231726, -0.05305551,  0.01195211,  0.04031831,
        -0.04476306]])

我想得到的是以下内容:

What l would like to get is the following :

array([[0.03317591, 0.06865846, -0.00223798,  0.01160267,  0.12610824, -0.01634712,
         0.01217519],
       [-0.01624349, -0.02872752, -0.00369226, -0.00727594, -0.0501376 , -0.01641992,
         0.00933081],
       [-0.01151019, -0.02063454, -0.00231726, -0.05305551,  0.01195211,  0.04031831,
        -0.04476306]])

V1 有什么问题?

推荐答案

使用np.hstack,需要将V1转为2D> 使得三个输入数组沿第一个轴的长度相同 -

To use np.hstack, we need to convert V1 to 2D such that the lengths along the first axis for the three input arrays are the same -

np.hstack((V1[:,None],V2,V3))

作为替代方案,我们可以使用 <代码>np.column_stacknp.concatenate 沿 2D 转换的 V1 与其他或 np.c_ ->

As alternatives, we can use np.column_stack or np.concatenate along the second axis on 2D converted V1 alongwith others or np.c_ -

np.column_stack((V1,V2,V3))
np.concatenate([V1[:,None],V2,V3],axis=1)
np.c_[V1,V2,V3]

这篇关于ValueError: 所有输入数组必须具有相同的维数.堆叠向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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