gpt4 book ai didi

python - 为什么 numpy 矩阵不相等?

转载 作者:行者123 更新时间:2023-12-01 03:36:39 25 4
gpt4 key购买 nike

为什么返回 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:])

对我来说它们似乎是相同的,但似乎第二个数组的过滤方式与第一个数组不同。

最佳答案

最终它是第一个切片的乘积,因为使用 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]])

关于python - 为什么 numpy 矩阵不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40271848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com