python替换2d numpy数组中的值

python替换2d numpy数组中的值,python,multidimensional-array,max,Python,Multidimensional Array,Max,我想将2d numpy数组中每列的最大值替换为-1: b = numpy.array([[1,2,3,4],[5,6,7,8], [9,10,11,12]]) #get the max value of each column maxposcol = b.argmax(axis = 0) maxvalcol = b.max(axis = 0) #replace max values with -1 for i in numpy.arange(b.shape[1]): b[maxposc

我想将2d numpy数组中每列的最大值替换为-1:

b = numpy.array([[1,2,3,4],[5,6,7,8], [9,10,11,12]])
#get the max value of each column
maxposcol = b.argmax(axis = 0)
maxvalcol = b.max(axis = 0)
#replace max values with -1 
for i in numpy.arange(b.shape[1]):
    b[maxposcol[i]][i] = -1
是否有其他方法替换位置由maxposcol[i]给出的最大值


如果我想找到矩阵每列的n个最大值,你会建议我怎么做?使用排序?重复搜索最大值并在每一步替换它们

您可以这样做:

>>> a=np.argmax(b, axis=0)
>>> b[a] = -1
>>> b
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [-1, -1, -1, -1]])

谢谢,您建议如何查找每列的n个最大值?@lizzie-[此链接应该对您有所帮助](),如果您不知道:
b[b>6]=-1
它可能对您有用:)