为什么返回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:])
看起来他们对我来说是一样的,但看起来第二个数组的过滤方式与第一个不同。
答案 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]])