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

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

问题描述

l有三个要附加的数组. 这是我的矢量样本:

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

为了附加三个向量并得到一个向量l,请执行以下操作:

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

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

l出现以下错误:

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_stack np.concatenate 沿2D上的第二个轴与其他轴或V1. html"rel =" nofollow noreferrer> 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天全站免登陆