在同一个np数组Python中进行元素明智的比较

时间:2017-04-15 21:49:42

标签: python arrays numpy comparison element

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

5x5nparray

矩阵是一个上三角矩阵,其中列出的是纳米值,而不是0,是矩阵是对称的,我认为这样可以更容易地计算出值最小的矢量。

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

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifierHere")
    let article = bookmarks[indexPath.row]
    let title = article.title
    cell.titleLabel.text = title
    return cell
}

我很感激任何帮助/提示。

编辑:

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

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])

2 个答案:

答案 0 :(得分:0)

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

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])]]

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

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

1 < 2 < 3
true

3 > 2 > 1
false

答案 1 :(得分:0)

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])
相关问题