ValueError:所有输入数组的维数必须相同 [英] ValueError: all the input arrays must have same number of dimensions

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

问题描述

我遇到np.append问题.

我正在尝试通过使用以下代码来复制20x361矩阵n_list_converted的最后一列:

I'm trying to duplicate the last column of 20x361 matrix n_list_converted by using the code below:

n_last = []
n_last = n_list_converted[:, -1]
n_lists = np.append(n_list_converted, n_last, axis=1)

但是我得到了错误:

ValueError:所有输入数组的维数必须相同

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

但是,我已经检查了矩阵尺寸

However, I've checked the matrix dimensions by doing

 print(n_last.shape, type(n_last), n_list_converted.shape, type(n_list_converted))

我得到

(20L,)(20L,361L)

(20L,) (20L, 361L)

那么尺寸匹配吗?错误在哪里?

so the dimensions match? Where is the mistake?

推荐答案

如果我从3x4数组开始,并以轴1连接3x1数组,我将得到3x5数组:

If I start with a 3x4 array, and concatenate a 3x1 array, with axis 1, I get a 3x5 array:

In [911]: x = np.arange(12).reshape(3,4)
In [912]: np.concatenate([x,x[:,-1:]], axis=1)
Out[912]: 
array([[ 0,  1,  2,  3,  3],
       [ 4,  5,  6,  7,  7],
       [ 8,  9, 10, 11, 11]])
In [913]: x.shape,x[:,-1:].shape
Out[913]: ((3, 4), (3, 1))

请注意,两个要连接的输入都具有2维.

Note that both inputs to concatenate have 2 dimensions.

省略:,并且x[:,-1]是(3,)形状-它是1d,因此出现错误:

Omit the :, and x[:,-1] is (3,) shape - it is 1d, and hence the error:

In [914]: np.concatenate([x,x[:,-1]], axis=1)
...
ValueError: all the input arrays must have same number of dimensions

np.append的代码为(在这种情况下,指定了轴)

The code for np.append is (in this case where axis is specified)

return concatenate((arr, values), axis=axis)

因此,只需稍微更改语法append即可.它使用2个参数代替列表.它模仿列表append是语法,但不应与该列表方法混淆.

So with a slight change of syntax append works. Instead of a list it takes 2 arguments. It imitates the list append is syntax, but should not be confused with that list method.

In [916]: np.append(x, x[:,-1:], axis=1)
Out[916]: 
array([[ 0,  1,  2,  3,  3],
       [ 4,  5,  6,  7,  7],
       [ 8,  9, 10, 11, 11]])

np.hstack首先确保所有输入均为atleast_1d,然后进行串联:

np.hstack first makes sure all inputs are atleast_1d, and then does concatenate:

return np.concatenate([np.atleast_1d(a) for a in arrs], 1)

因此它需要相同的x[:,-1:]输入.本质上是相同的动作.

So it requires the same x[:,-1:] input. Essentially the same action.

np.column_stack还在轴1上进行连接.但是首先,它将1d输入传递给

np.column_stack also does a concatenate on axis 1. But first it passes 1d inputs through

array(arr, copy=False, subok=True, ndmin=2).T

这是将(3,)数组转换为(3,1)数组的一般方法.

This is a general way of turning that (3,) array into a (3,1) array.

In [922]: np.array(x[:,-1], copy=False, subok=True, ndmin=2).T
Out[922]: 
array([[ 3],
       [ 7],
       [11]])
In [923]: np.column_stack([x,x[:,-1]])
Out[923]: 
array([[ 0,  1,  2,  3,  3],
       [ 4,  5,  6,  7,  7],
       [ 8,  9, 10, 11, 11]])

所有这些堆栈"都可以方便使用,但从长远来看,了解尺寸和基本的np.concatenate很重要.还知道如何查找类似这样的函数的代码.我经常使用ipython ??魔术.

All these 'stacks' can be convenient, but in the long run, it's important to understand dimensions and the base np.concatenate. Also know how to look up the code for functions like this. I use the ipython ?? magic a lot.

在时间测试中,np.concatenate明显更快-像这样的小数组,额外的函数调用层会产生很大的时差.

And in time tests, the np.concatenate is noticeably faster - with a small array like this the extra layers of function calls makes a big time difference.

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

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