ValueError:所有输入数组必须具有相同的维数。堆叠矢量

时间:2022-10-27 21:25:35

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 :

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

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

l got the following error :

我收到以下错误:

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

However :

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

works, it returns :

工作,它返回:

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

What is wrong with V1 ?

V1有什么问题?

2 个解决方案

#1


1  

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转换为2D,使得三个输入数组沿第一轴的长度相同 -

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

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

作为替代方案,我们可以在2D转换V1和第二轴上使用np.column_stack或np.concatenate以及其他或np.c_ -

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

#2


0  

There's nothing wrong with V1 except that it's 1D while V2 and V3 are 2D. According to the docs for hstack, all the input arrays have to have the same shape on all except the second axis. V1 in your code doesn't have a second axis.

除了它是1D而V2和V3是2D之外,V1没有任何问题。根据hstack的文档,除了第二个轴之外,所有输入数组都必须具有相同的形状。代码中的V1没有第二个轴。

You can easily add an empty second axis to V1 during your call to hstack like this:

您可以在调用hstack时轻松地将空的第二轴添加到V1,如下所示:

new_v = hstack((V1[:, None], V2, V3))

That should achieve your desired output.

那应该达到你想要的输出。

Note: The V1[:, None] bit is one of three ways that NumPy has available to add empty dimensions to arrays. The other two are V1[:, np.newaxis] and the function version np.expand_dims(V1, axis=1).

注意:V1 [:,None]位是NumPy可用于向数组添加空维度的三种方式之一。另外两个是V1 [:,np.newaxis]和函数版本np.expand_dims(V1,axis = 1)。

You could use any of these in place of V1[:, None] in that line of code and it would work. Check out the docs for numpy.expand_dims for more on adding dimensions to arrays.

您可以在该行代码中使用其中任何一个代替V1 [:,None],它可以工作。查看numpy.expand_dims的文档,了解有关向数组添加维度的更多信息。

#1


1  

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转换为2D,使得三个输入数组沿第一轴的长度相同 -

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

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

作为替代方案,我们可以在2D转换V1和第二轴上使用np.column_stack或np.concatenate以及其他或np.c_ -

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

#2


0  

There's nothing wrong with V1 except that it's 1D while V2 and V3 are 2D. According to the docs for hstack, all the input arrays have to have the same shape on all except the second axis. V1 in your code doesn't have a second axis.

除了它是1D而V2和V3是2D之外,V1没有任何问题。根据hstack的文档,除了第二个轴之外,所有输入数组都必须具有相同的形状。代码中的V1没有第二个轴。

You can easily add an empty second axis to V1 during your call to hstack like this:

您可以在调用hstack时轻松地将空的第二轴添加到V1,如下所示:

new_v = hstack((V1[:, None], V2, V3))

That should achieve your desired output.

那应该达到你想要的输出。

Note: The V1[:, None] bit is one of three ways that NumPy has available to add empty dimensions to arrays. The other two are V1[:, np.newaxis] and the function version np.expand_dims(V1, axis=1).

注意:V1 [:,None]位是NumPy可用于向数组添加空维度的三种方式之一。另外两个是V1 [:,np.newaxis]和函数版本np.expand_dims(V1,axis = 1)。

You could use any of these in place of V1[:, None] in that line of code and it would work. Check out the docs for numpy.expand_dims for more on adding dimensions to arrays.

您可以在该行代码中使用其中任何一个代替V1 [:,None],它可以工作。查看numpy.expand_dims的文档,了解有关向数组添加维度的更多信息。