忽略对角线的Numpy数组最小值

16

我需要找到一个numpy数组中忽略对角元素后的最大值。

np.amax() 提供了忽略特定轴的方法。如何忽略所有对角线元素并达到同样的效果?

5个回答

31

你可以使用口罩

mask = np.ones(a.shape, dtype=bool)
np.fill_diagonal(mask, 0)
max_value = a[mask].max()

其中a是您要查找最大值的矩阵。掩码选择了非对角线元素,因此a[mask]将是所有非对角线元素的长向量。然后,您只需取最大值。

或者,如果您不介意修改原始数组

np.fill_diagonal(a, -np.inf)
max_value = a.max()
当然,您始终可以复制原始数据,然后在不修改原始数据的情况下执行上述操作。此外,这是建立在a为某种浮点格式的假设之上。

a[mask].max() 和 np.max(a[mask]) 有什么区别? - Gerald
@Gerald 我没有意识到有任何区别。 - hunse
5
你可以直接使用 a[~np.eye(*a.shape, dtype=bool)].max() - user76284

9
另一种可能性是使用NumPy的as_strided将对角线推到第一列,然后再将其切片:
import numpy as np
from numpy.lib.stride_tricks import as_strided
b = np.arange(0,25,1).reshape((5,5))
n = b.shape[0]
out = np.max(as_strided(b, (n-1,n+1), (b.itemsize*(n+1), b.itemsize))[:,1:])

对于类似于下面的 b

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

上述代码生成的结果是:
23

np.max函数的参数是针对b的移位视图:

In [7]: as_strided(b, (n-1,n+1), (b.itemsize*(n+1), b.itemsize))
Out[7]: array([[ 0,  1,  2,  3,  4,  5],
               [ 6,  7,  8,  9, 10, 11],
               [12, 13, 14, 15, 16, 17],
               [18, 19, 20, 21, 22, 23]])

这样做的目的是:

In [8]: as_strided(b, (n-1,n+1), (b.itemsize*(n+1), b.itemsize))[:,1:]
Out[8]: array([[ 1,  2,  3,  4,  5],
               [ 7,  8,  9, 10, 11],
               [13, 14, 15, 16, 17],
               [19, 20, 21, 22, 23]])

2
哇,这很酷。对于大型数组,可能比使用掩码快。 - hunse
这个能扩展到批量矩阵吗(即存在一些前置维度的情况)? - user76284
@user76284 这可能适用于一个 mnxn 数组的数组:np.max(as_strided(c, (m,n-1,n+1), (c.itemsize*n*n, c.itemsize*(n+1), c.itemsize))[:,:,1:], axis=(1,2)):为了得到更好的答案,我认为你应该提出一个新问题。 - xnx

4
这应该可以工作:
import numpy as np
import numpy.random

# create sample matrix
a = numpy.random.randint(10,size=(8,8))
a[0,0] = 100

看起来像是:
array([[100, 8, 6, 5, 5, 7, 4, 5],
   [4, 6, 1, 7, 4, 5, 8, 5],
   [0, 2, 0, 7, 4, 2, 7, 9],
   [5, 7, 5, 9, 8, 3, 2, 8],
   [2, 1, 3, 4, 0, 7, 8, 1],
   [6, 6, 7, 6, 0, 6, 6, 8],
   [6, 0, 1, 9, 7, 7, 9, 3],
   [0, 5, 5, 5, 1, 5, 4, 4]])

# create mask
mask = np.ones((8,8)) 
mask = (mask - np.diag(np.ones(8))).astype(np.bool)

看起来像这样:

array([[False,  True,  True,  True,  True,  True,  True,  True],
   [ True, False,  True,  True,  True,  True,  True,  True],
   [ True,  True, False,  True,  True,  True,  True,  True],
   [ True,  True,  True, False,  True,  True,  True,  True],
   [ True,  True,  True,  True, False,  True,  True,  True],
   [ True,  True,  True,  True,  True, False,  True,  True],
   [ True,  True,  True,  True,  True,  True, False,  True],
   [ True,  True,  True,  True,  True,  True,  True, False]], dtype=bool)

那么

# calculate the maximum
out = np.amax(a[mask])

输出:

9

0
如果您在这里寻找一种方法来查找沿轴的最小值,同时忽略对角线,则可以使用numpy.where将对角线替换为数组最大值,并沿轴查找min。
row_mins = np.where(np.eye(*a.shape, dtype=bool), a.max(), a).min(axis=1)

对于沿轴的max,将其改为min,反之亦然:

row_maxs = np.where(np.eye(*a.shape, dtype=bool), a.min(), a).max(axis=1)

另一种选择是将数组最大值添加到对角线上的值中,并沿着一个轴找到min(将max减去):

row_mins = (a+np.diag([a.max()]*len(a))).min(axis=1)

row_maxs = (a-np.diag([a.max()]*len(a))).max(axis=1)

示例:

对于数组:

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

的输出

out = np.where(np.eye(*a.shape, dtype=bool), a.max(), a).min(axis=1)

array([ 1,  5, 10, 15, 20])

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接