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

Python ValueError:所有输入数组的维数必须相同,python,arrays,numpy,append,Python,Arrays,Numpy,Append,我对np.append有问题 我试图使用下面的代码复制20x361矩阵的最后一列n\u list\u converted: n_last = [] n_last = n_list_converted[:, -1] n_lists = np.append(n_list_converted, n_last, axis=1) 但我得到了一个错误: ValueError:所有输入数组的维数必须相同 但是,我已经通过以下方式检查了矩阵维度 print(n_last.shape, type(n_last

我对
np.append有问题

我试图使用下面的代码复制20x361矩阵的最后一列
n\u list\u converted

n_last = []
n_last = n_list_converted[:, -1]
n_lists = np.append(n_list_converted, n_last, axis=1)
但我得到了一个错误:

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

但是,我已经通过以下方式检查了矩阵维度

 print(n_last.shape, type(n_last), n_list_converted.shape, type(n_list_converted))
我得到

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

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

(n,)和(n,1)不是相同的形状。尝试使用
[:,None]
符号将向量强制转换为数组:

n_lists = np.append(n_list_converted, n_last[:, None], axis=1)
或者,在提取
n_last
时,您可以使用

n_last = n_list_converted[:, -1:]

获取
(20,1)
数组。

获取错误的原因是“1×n”矩阵与长度为n的数组不同

我建议改用
hstack()
vstack()
。 像这样:

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]])
注意重复出现的“7,15,23,31”列。 另外,请注意,我使用了
a[:,-1:][/code>而不是
a[:,-1]
。我的版本生成一列:

array([[7],
       [15],
       [23],
       [31]])
而不是行
数组([7,15,23,31])



编辑:
append()
要慢得多。阅读。

如果我从一个3x4数组开始,并连接一个3x1数组和轴1,我得到一个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))
请注意,要连接的两个输入都有两个维度

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

In [914]: np.concatenate([x,x[:,-1]], axis=1)
...
ValueError: all the input arrays must have same number of dimensions
np.append
的代码为(在指定轴的情况下)

所以只要稍微改变一下语法,
append
就可以了。它不是一个列表,而是两个参数。它模仿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
首先确保所有输入至少为
1d
,然后连接:

return np.concatenate([np.atleast_1d(a) for a in arrs], 1)
因此它需要相同的
x[:,-1:][/code>输入。基本上是相同的动作

np.column\u堆栈也在轴1上进行连接。但首先,它将1d输入传递到

array(arr, copy=False, subok=True, ndmin=2).T
这是将(3,)数组转换为(3,1)数组的一般方法

所有这些“堆栈”都很方便,但从长远来看,理解维度和基本
np.concatenate
很重要。还知道如何查找像这样的函数的代码。我经常使用
ipython
??
魔术

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

你也可以将(n,)转换为(n,1),方法是用括号括起来[]

e、 g.使用
np.append(b[a],axis=0)代替
np.append(b[a],axis=0)

返回

array([[5, 6],
       [7, 8],
       [1, 2]])

试试
np.column\u stack
。它成功了!但为什么呢?
np.append
比list
.append
慢;但与
堆栈相比
。它使用
np.concatenate
@hpaulj以便。。。正如我刚才所说,使用
append
vs
stack
与2个矩阵相同,并且
stack
对于2个以上的元素更好,因此
stack
总是至少和
append
一样好。反之亦然:
b=[1,2];a=[[5,6],[7,8];np.append([b],a,axis=0)
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]])
a=[1,2]
b=[[5,6],[7,8]]
np.append(b,[a],axis=0)
array([[5, 6],
       [7, 8],
       [1, 2]])