如何从numpy数组的一部分列中找到最小值? [英] How to find the minimum value from a part of a column in numpy array?

查看:698
本文介绍了如何从numpy数组的一部分列中找到最小值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的numpy数组,它有三列,col.1是距离,col. 2和3是节点的ID.我想找到距第一列的最小距离,但仅适用于节点ID0.

I have a numpy array like below, which has three columns, col.1 is the distance, col. 2 and 3 are the id of the nodes. I want to find the minimum distance from the 1st column but only for node id 0.

distance   i    j
[[ 1.18801546  0.          1.        ]
 [ 2.30434659  0.          2.        ]
 [ 3.46650731  0.          3.        ]
 [ 0.85449778  0.          4.        ]
 [ 0.84375971  0.          5.        ]
 [ 2.66327706  0.          6.        ]
 [ 1.84376278  0.          7.        ]
 [ 1.29614483  0.          8.        ]
 [ 2.86955783  0.          9.        ]
 [ 1.55222839  1.          2.        ]
 [ 2.56904021  1.          3.        ]
 [ 0.56480212  1.          4.        ]
 [ 0.81877367  1.          5.        ]
 [ 2.87466569  1.          6.        ]
 [ 1.01649384  1.          7.        ]
 [ 1.95662814  1.          8.        ]
 [ 3.15455155  1.          9.        ]
 [ 1.1897445   2.          3.        ]
 [ 1.65880881  2.          4.        ]
 [ 2.21427178  2.          5.        ]
 [ 2.12770111  2.          6.        ]
 [ 0.59811712  2.          7.        ]
 [ 2.15373458  2.          8.        ]
 [ 2.47151944  2.          9.        ]
 [ 2.78849347  3.          4.        ]
 [ 3.29699194  3.          5.        ]
 [ 2.90479808  3.          6.        ]
 [ 1.6405647   3.          7.        ]
 [ 3.2628552   3.          8.        ]
 [ 3.24135083  3.          9.        ]
 [ 0.59483003  4.          5.        ]
 [ 2.55441835  4.          6.        ]
 [ 1.22876339  4.          7.        ]
 [ 1.62616729  4.          8.        ]
 [ 2.7776452   4.          9.        ]
 [ 3.07635954  5.          6.        ]
 [ 1.7483827   5.          7.        ]
 [ 1.993107    5.          8.        ]
 [ 3.26526698  5.          9.        ]
 [ 2.34443787  6.          7.        ]
 [ 1.59405468  6.          8.        ]
 [ 0.46781919  6.          9.        ]
 [ 1.92762241  7.          8.        ]
 [ 2.69818642  7.          9.        ]
 [ 1.85007201  8.          9.        ]]

我尝试使用

print all_data[np.argmax(all_data[:, 0]), 1]

但是它不仅返回我想要的节点0,而且返回整个列的最小值.如何获得仅与节点"0"关联的最小值?另外,argmin值似乎已取整!知道如何解决这些问题吗?顺便说一下,我正在使用numpy数组.

but it returns the lowest value for the whole column not only for node 0 which I want. How to get the minimum associated with only node '0'? Also the argmin value seems to be rounded up! Any idea how to solve these problems? By the way I'm using numpy array.

推荐答案

来自OP问题

如何获取仅与节点"0"相关联的最小值?

How to get the minimum associated with only node '0'?

In [1]: import numpy as np

In [2]: a=np.array([[ 1.18801546, 0., 1., ],
   ...:  [ 2.30434659, 0., 2., ],
   ...:  [ 3.46650731, 0., 3., ],
   ...:  [ 0.85449778, 0., 4., ],
   ...:  ...
   ...:  [ 1.29614483, 0., 8., ],
   ...:  [ 2.86955783, 0., 9., ],])

具有导入的numpy并将数组创建为a,我们使用布尔数组a[:,1]==0.0在其上创建视图,并使用numpy函数min查找第一列的最小值,并带有可选的参数axis=0以限制对列0中的最小值的搜索.

Having imported numpy and created your array as a, we create a view on it using the boolean array a[:,1]==0.0 and find the minimum value of the first column using the numpy function min, with the optional argument axis=0 to limit the search for the minimum in column 0.

In[3]: np.min(a[a[:,1]==0.0],axis=0)
Out[3]: array([ 0.84375971,  0.        ,  1.        ])

就这些.

这将为您提供每一列的最小值,如果您希望在第0列中包含最小值,则可以使用表达式

This gives you the minimum for each column, if you want the minimum value in column 0 then the expression

np.min(a[a[:,1]==0.0],axis=0)[0]

将其提供给您--- OTOH,如果您希望该行具有最小值,则有些不同

gives it to you --- OTOH, if you want the row with the minimum value it's a bit different

a[np.argmin(a[a[:,1]==0],axis=0)[0]]

即使我们在单个表达式中写了三遍a的事实似乎也没有说服力,但这确实可以做到这一点.

even if the fact that we write a three times in a single expression may seem a bit unelegant it does its job.

这篇关于如何从numpy数组的一部分列中找到最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆