Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 被其他数组修改的numpy数组_Python_Numpy - Fatal编程技术网

Python 被其他数组修改的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[

我很难理解这个非常简单的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[:,:,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]]]