为什么numpy矩阵不相等?

时间:2016-10-26 20:47:52

标签: python numpy

为什么返回False?

temp = np.array([[1,2,3],[4,5,6], [7,8,9]])

filter_indices = range(3)[1:2]
filter_indices2 = range(3)[1:]

print np.array_equal(temp[1:2,1:], temp[filter_indices, filter_indices2])

但这些是平等的:

np.array_equal(temp[1:2,1:], temp[filter_indices, 1:])

看起来他们对我来说是一样的,但看起来第二个数组的过滤方式与第一个不同。

1 个答案:

答案 0 :(得分:2)

最终,它是您第一个切片的产物,因为与slice(1, 2)[1]建立索引之间存在差异。通过使用1:2建立索引,即可生成

> temp[1:2]
array([[4, 5, 6]])

具有形状(1, 3),即行向量(1行,3列)。使用filter_indices,您实际上正在生成

> temp[1] #equivalent to temp[filter_indices]
array([4, 5, 6])

形状(3,)。要获得相同的行为,请使用

进行索引
> temp[[1]] #equivalent to temp[[filter_indices]]
array([[4, 5, 6]])