同一 np 数组中的元素明智比较 Python

Element wise comparison within same np array Python

我正在尝试比较 np 数组中的值,按顺序附加它们并创建一个向量,其中包含 np 数组的最小值(如果矩阵,向量的长度应为 N*2 - 1尺寸为 NxN)。我附上了一张图片来更好地说明问题。基本上,我想从位置 [0,0] 开始,然后依次比较 [0,1] 和 [1,0] 等中的值。当将 nan 与数字进行比较时,它会自动 select是号码。对于两个数字的比较,即[0,2]和[1,1]中的值,它应该select较小的那个。

之所以矩阵是一个上三角矩阵,列出的是 nan 值而不是 0,是因为矩阵是对称的,我认为这会使计算其中最小值的向量更容易。

下面是我写的一些代码,试图解决这个问题。我意识到索引 i 和 j 在矩阵中移动不正确,但是,我完全不知道如何正确地移动它。

注意:我没有包括生成矩阵的代码,只是因为它是更大脚本的一部分,我想避免混淆。在此示例中,我将大小为 5x5 的矩阵 (mat) 表示为 dim = 5。此外,这只是一个示例,其中最小值恰好是 [mat[0,0],mat[0,1],mat[1,1],mat[1,2]...].

x=[]
dim = 5
for i in range(dim-1):
    for j in range(dim-1):

    if mat[i,j+1] > mat[i+1,j]:
        x.append(mat[i+1,j])

    elif mat[i,j+1] < mat[i+1,j]:
        x.append(mat[i,j+1])

    elif m.isnan(mat[i,j+1]):
        x.append(mat[i+1,j])

    elif m.isnan(mat[i+1,j]):
        x.append(mat[i,j+1])

我将不胜感激help/tips。

编辑:

感谢您的所有意见。我想我知道怎么做了。我认为可能有一种更 pythonic 的方式来做到这一点,但是,我现在缺乏技能。代码 blow 应该能够遍历上三角矩阵,选择最小值并将它们附加到向量。在这个例子中,我的意思是 vector = [2.8, 4.7, 7.6, 5.5, 5.3, 0.18, -3.9, -11.1, -20.1].

import numpy as np


dim = 5
mat = np.array([[2.802, 4.713, 9.581, 15.339, 22.273],
     [np.nan, 7.647, 5.559, 7.317, 10.250],
     [np.nan, np.nan, 5.383, 0.184, -0.882],
     [np.nan, np.nan, np.nan, -3.992, -11.124],
     [np.nan, np.nan, np.nan, np.nan, -20.192]])
mat_to_vect =  np.reshape(mat, (dim**2))
min_vect = [mat_to_vect[0]]

i = 1
j = dim

while i and j < dim**2-1:

    if mat_to_vect[i] > mat_to_vect[j]:
        min_vect.append(mat_to_vect[j])
        j += dim
        i += dim

    elif mat_to_vect[i] < mat_to_vect[j]:
        min_vect.append(mat_to_vect[i])
        j += 1
        i += 1

    elif m.isnan(mat_to_vect[i]):
        min_vect.append(mat_to_vect[j])
        j += dim
        i += dim

    elif m.isnan(mat_to_vect[j]):
        min_vect.append(mat_to_vect[i])
        j += 1
        i += 1

min_vect.append(mat_to_vect[dim**2-1])

这对于评论来说太大了,但可以为您的最终解决方案提供一些选择。

a = [[2.802, 4.713, 9.581, 15.339, 22.273],
     [np.nan, 7.647, 5.559, 7.317, 10.250],
     [np.nan, np.nan, 5.383, 0.184, -0.882],
     [np.nan, np.nan, np.nan, -3.992, -11.124],
     [np.nan, np.nan, np.nan, np.nan, -20.192]]
b = np.nanmin(a, axis=0)
b
array([  2.802,   4.713,   5.383,  -3.992, -20.192])

现在如果-20.192是想要的结果,那么最终的解决方案就很简单了。 实际上,这种使用 np.triu 且下部设置为 np.nan 的设置允许使用 nan 函数来获得最小值。在你的情况下,如果 -20.192 是所需的结果,则按列获取最小值,然后按行获取最小值。 如果这不是最终情况,则需要进一步定义从单元格 (0,0) 开始依次通过数组的步骤。简而言之,np.nanmin 似乎就是您所追求的。

附录 或者您可以按顺序进行,我认为您在评论中添加的内容是指

a
array([[  2.802,   4.713,   9.581,  15.339,  22.273],
       [    nan,   7.647,   5.559,   7.317,  10.25 ],
       [    nan,     nan,   5.383,   0.184,  -0.882],
       [    nan,     nan,     nan,  -3.992, -11.124],
       [    nan,     nan,     nan,     nan, -20.192]])

b = [np.nanmin(a[:i], axis=0) for i in range(1, a.shape[0]+1)]
b
[array([  2.802,   4.713,   9.581,  15.339,  22.273]),
 array([  2.802,   4.713,   5.559,   7.317,  10.25 ]),
 array([ 2.802,  4.713,  5.383,  0.184, -0.882]),
 array([  2.802,   4.713,   5.383,  -3.992, -11.124])
 array([  2.802,   4.713,   5.383,  -3.992, -20.192])]]
import numpy as np


dim = 5
mat = np.array([[2.802, 4.713, 9.581, 15.339, 22.273],
     [np.nan, 7.647, 5.559, 7.317, 10.250],
     [np.nan, np.nan, 5.383, 0.184, -0.882],
     [np.nan, np.nan, np.nan, -3.992, -11.124],
     [np.nan, np.nan, np.nan, np.nan, -20.192]])
mat_to_vect =  np.reshape(mat, (dim**2))
min_vect = [mat_to_vect[0]]

i = 1
j = dim

while i and j < dim**2-1:

    if mat_to_vect[i] > mat_to_vect[j]:
        min_vect.append(mat_to_vect[j])
        j += dim
        i += dim

    elif mat_to_vect[i] < mat_to_vect[j]:
        min_vect.append(mat_to_vect[i])
        j += 1
        i += 1

    elif m.isnan(mat_to_vect[i]):
        min_vect.append(mat_to_vect[j])
        j += dim
        i += dim

    elif m.isnan(mat_to_vect[j]):
        min_vect.append(mat_to_vect[i])
        j += 1
        i += 1

min_vect.append(mat_to_vect[dim**2-1])