简体   繁体   English

如何从numpy数组的一部分列中找到最小值?

[英]How to find the minimum value from a part of a column in numpy array?

I have a numpy array like below, which has three columns, col.1 is the distance, col. 我有一个如下的numpy数组,其中有三列,col.1是距离,col。 2 and 3 are the id of the nodes. 2和3是节点的ID。 I want to find the minimum distance from the 1st column but only for node id 0. 我想找到距第一列的最小距离,但仅适用于节点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.        ]]

I have tried using 我尝试使用

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

but it returns the lowest value for the whole column not only for node 0 which I want. 但是它不仅返回我想要的节点0,而且返回整个列的最小值。 How to get the minimum associated with only node '0'? 如何获得仅与节点“ 0”关联的最小值? Also the argmin value seems to be rounded up! 另外,argmin值似乎已取整! Any idea how to solve these problems? 任何想法如何解决这些问题? By the way I'm using numpy array. 顺便说一下,我正在使用numpy数组。

您可以找到带有0作为第二个索引的元素,其中all_data[all_data[:, 1]==0]

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

From the OP question 从OP问题

How to get the minimum associated with only node '0'? 如何获得仅与节点“ 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., ],])

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 . 具有进口和numpy的创建阵列作为a ,我们创建使用布尔阵列上它的视图a[:,1]==0.0 ,并找到使用的第一列的最小值numpy功能min ,用可选参数axis=0以限制在第0列中搜索最小值。

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

That's all. 就这样。

This gives you the minimum for each column, if you want the minimum value in column 0 then the expression 这将为您提供每一列的最小值,如果您希望在列0中获得最小值,则表达式

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

gives it to you --- OTOH, if you want the row with the minimum value it's a bit different 给您--- OTOH,如果您要使行具有最小值,则有点不同

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

even if the fact that we write a three times in a single expression may seem a bit unelegant it does its job. 即使我们在一个表达式中写a三遍这个事实看起来似乎并不那么容易理解,但这确实可以做到。

Filter the data for the selected value in column 0, and then calculate the minimum distance: 为列0中的选定值过滤数据,然后计算最小距离:

selected_value = 0
value_col = 0  # The column containing the selected value.
dist_col = 1  # The column containing the distance value.
min_val = all_data[all_data[:, value_col] == selected_value, dist_col].min()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM