ValueError:所有输入数组必须具有相同的维数 [英] ValueError: all the input arrays must have same number of dimensions
问题描述
我在使用 np.append
时遇到问题.
I'm having a problem with 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 数组开始,然后将一个 3x1 数组与轴 1 连接起来,我会得到一个 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屋!