Python 被其他数组修改的numpy数组
我很难理解这个非常简单的numpy代码的输出出了什么问题Python 被其他数组修改的numpy数组,python,numpy,Python,Numpy,我很难理解这个非常简单的numpy代码的输出出了什么问题 fake=np.arange(0,24).reshape(4,2,3) print("The original array") print(fake) out=np.copy(fake) print("the copy") print(out) print("what part we want to modify ") print(fake[:,:,2]) print(fake[
fake=np.arange(0,24).reshape(4,2,3)
print("The original array")
print(fake)
out=np.copy(fake)
print("the copy")
print(out)
print("what part we want to modify ")
print(fake[:,:,2])
print(fake[:,:,2]<5)
print("after the modification")
out[fake[:,:,2]<5]=0
print(out)
我是说为什么?为什么第一个元素变成[0,0,0]?是不是应该变成[0,1,0]?毕竟,这种情况只会影响你
[[ 2 5]
[ 8 11]
[14 17]
[20 23]]
有人能帮我理解吗?注意,
out
的形状是(4,2,3)
,而fake[:,:,2]<5
的形状是(4,2)
所以调用行out[fake[:,2]<5]=0
将0
放在最后一个维度的每个单元格中。你想要的是:
out[fake[:, :, 2] < 5, 2] = 0
Output:
[[[ 0 1 0]
[ 3 4 5]]
[[ 6 7 8]
[ 9 10 11]]
[[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]]]
out[fake[:,:,2]<5,2]=0
输出:
[[[ 0 1 0]
[ 3 4 5]]
[[ 6 7 8]
[ 9 10 11]]
[[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]]]
out[fake[:, :, 2] < 5, 2] = 0
Output:
[[[ 0 1 0]
[ 3 4 5]]
[[ 6 7 8]
[ 9 10 11]]
[[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]]]