假设我有一个以下形式的张量:
import numpy as np
a = np.array([ [[1,2],
[3,4]],
[[5,6],
[7,3]]
])
# a.shape : (2,2,2) is a tensor containing 2x2 matrices
indices = np.argmax(a, axis=2)
#print indices
for mat in a:
max_i = np.argmax(mat,axis=1)
# Not really working I would like to
# change 4 in the first matrix to -1
# and 3 in the last to -1
mat[max_i] = -1
print a
现在我想做的是使用索引作为掩码,在一个数组中将每个最大元素替换为-1。有没有numpy的方法可以做到这一点?到目前为止,我已经想出了使用for循环的方法。