简体   繁体   中英

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

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. 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.

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

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

From the OP question

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

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

That's all.

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]

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

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

Filter the data for the selected value in column 0, and then calculate the minimum distance:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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