Python ValueError:所有输入数组必须具有相同的维数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38848759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: all the input arrays must have same number of dimensions
提问by odo22
I'm having a problem with np.append
.
我有问题np.append
。
I'm trying to duplicate the last column of 20x361 matrix n_list_converted
by using the code below:
我正在尝试n_list_converted
使用以下代码复制 20x361 矩阵的最后一列:
n_last = []
n_last = n_list_converted[:, -1]
n_lists = np.append(n_list_converted, n_last, axis=1)
But I get error:
但我得到错误:
ValueError: all the input arrays must have same number of dimensions
ValueError:所有输入数组必须具有相同的维数
However, I've checked the matrix dimensions by doing
但是,我通过执行检查了矩阵维度
print(n_last.shape, type(n_last), n_list_converted.shape, type(n_list_converted))
and I get
我得到
(20L,) (20L, 361L)
(20L,) (20L, 361L)
so the dimensions match? Where is the mistake?
所以尺寸匹配?错误在哪里?
采纳答案by hpaulj
If I start with a 3x4 array, and concatenate a 3x1 array, with axis 1, I get a 3x5 array:
如果我从一个 3x4 数组开始,并用轴 1 连接一个 3x1 数组,我会得到一个 3x5 数组:
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))
Note that both inputs to concatenate have 2 dimensions.
请注意,要连接的两个输入都有 2 个维度。
Omit the :
, and x[:,-1]
is (3,) shape - it is 1d, and hence the error:
省略:
, 和x[:,-1]
(3,) 形状 - 它是 1d,因此错误:
In [914]: np.concatenate([x,x[:,-1]], axis=1)
...
ValueError: all the input arrays must have same number of dimensions
The code for np.append
is (in this case where axis is specified)
的代码np.append
是(在这种情况下指定轴)
return concatenate((arr, values), axis=axis)
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.
所以稍微改变语法就行了append
。它需要 2 个参数而不是列表。它模仿 list append
is 语法,但不应与该 list 方法混淆。
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
first makes sure all inputs are atleast_1d
, and then does concatenate:
np.hstack
首先确保所有输入都是atleast_1d
,然后进行连接:
return np.concatenate([np.atleast_1d(a) for a in arrs], 1)
So it requires the same x[:,-1:]
input. Essentially the same action.
所以它需要相同的x[:,-1:]
输入。基本相同的动作。
np.column_stack
also does a concatenate on axis 1. But first it passes 1d inputs through
np.column_stack
也在轴 1 上进行连接。但首先它通过 1d 输入
array(arr, copy=False, subok=True, ndmin=2).T
This is a general way of turning that (3,) array into a (3,1) array.
这是将 (3,) 数组转换为 (3,1) 数组的一般方法。
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]])
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
。还知道如何查找此类函数的代码。我经常使用ipython
??
魔法。
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.
在时间测试中,np.concatenate
速度明显更快——像这样的小数组,额外的函数调用层会产生很大的时间差异。
回答by Aguy
(n,) and (n,1) are not the same shape. Try casting the vector to an array by using the [:, None]
notation:
(n,) 和 (n,1) 不是相同的形状。尝试使用[:, None]
符号将向量转换为数组:
n_lists = np.append(n_list_converted, n_last[:, None], axis=1)
Alternatively, when extracting n_last
you can use
或者,在提取时,n_last
您可以使用
n_last = n_list_converted[:, -1:]
to get a (20, 1)
array.
得到一个(20, 1)
数组。
回答by RuRo
The reason why you get your error is because a "1 by n" matrix is different from an array of length n.
出现错误的原因是“1 x n”矩阵与长度为 n 的数组不同。
I recommend using hstack()
and vstack()
instead.
Like this:
我建议使用hstack()
andvstack()
代替。像这样:
import numpy as np
a = np.arange(32).reshape(4,8) # 4 rows 8 columns matrix.
b = a[:,-1:] # last column of that matrix.
result = np.hstack((a,b)) # stack them horizontally like this:
#array([[ 0, 1, 2, 3, 4, 5, 6, 7, 7],
# [ 8, 9, 10, 11, 12, 13, 14, 15, 15],
# [16, 17, 18, 19, 20, 21, 22, 23, 23],
# [24, 25, 26, 27, 28, 29, 30, 31, 31]])
Notice the repeated "7, 15, 23, 31" column.
Also, notice that I used a[:,-1:]
instead of a[:,-1]
. My version generates a column:
请注意重复的“7、15、23、31”列。另外,请注意我使用的a[:,-1:]
不是a[:,-1]
. 我的版本生成一列:
array([[7],
[15],
[23],
[31]])
Instead of a row array([7,15,23,31])
而不是一行 array([7,15,23,31])
Edit: append()
is muchslower. Read this answer.
编辑:append()
是多慢。阅读这个答案。
回答by ZZZ
You can also cast (n,) to (n,1) by enclosing within brackets [ ].
您还可以通过将 (n,) 括在方括号 [ ] 中来将 (n,) 转换为 (n,1)。
e.g. Instead of np.append(b,a,axis=0)
use np.append(b,[a],axis=0)
例如,而不是np.append(b,a,axis=0)
使用np.append(b,[a],axis=0)
a=[1,2]
b=[[5,6],[7,8]]
np.append(b,[a],axis=0)
returns
返回
array([[5, 6],
[7, 8],
[1, 2]])