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

weylhg0b  于 2023-08-05  发布在  其他
关注(0)|答案(5)|浏览(97)

我必须找到一个numpy数组的最大值,忽略对角线元素。
amax()提供了查找它的方法,忽略了特定的轴。我怎么能忽略所有的对角线元素来达到同样的效果呢?

mv1qrgav

mv1qrgav1#

你可以戴个面具

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是某种浮点格式。

ar7v8xwq

ar7v8xwq2#

另一种可能性是使用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]])

7fhtutme

7fhtutme3#

这应该可以工作:

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

ru9i0ody

ru9i0ody4#

如果你在这里寻找一种方法来找到最小值沿着一个轴而忽略对角线,那么你可以使用numpy.where来替换对角线与数组max并找到min沿着一个轴:

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

字符串
对于沿着轴的max,将max改为min,反之亦然:

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


另一种选择是将数组max与对角线上的值相加,并沿着轴查找min(减去max的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])

myss37ts

myss37ts5#

复制此评论作为答案:

a[~np.eye(*a.shape, dtype=bool)].min()

字符串

相关问题