python - How to select a group of minimum values out of a numpy array? -


fitvec = np.zeros((100, 2)) #initializing fitvec, first column` indices , second column contain values

after initialization, fitvec gets assigned values running function. final fitvec values:

fitvec [[  2.00000000e+01   2.42733444e+10]  [  2.10000000e+01   2.53836270e+10]  [  2.20000000e+01   2.65580909e+10]  [  2.30000000e+01   2.76674886e+10]  [  2.40000000e+01   2.88334239e+10]  [  2.50000000e+01   3.00078878e+10]  [  2.60000000e+01   3.11823517e+10]  [  2.70000000e+01   3.22917494e+10]  [  2.80000000e+01   3.34011471e+10]  [  2.90000000e+01   3.45756109e+10]  [  3.00000000e+01   3.57500745e+10]  [  3.10000000e+01   3.68594722e+10]  [  3.20000000e+01   3.79688699e+10]  [  3.30000000e+01   3.90782676e+10]  [  3.40000000e+01   4.02527315e+10]  [  3.50000000e+01   4.14271953e+10]  [  3.60000000e+01   4.25365930e+10]  [  3.70000000e+01   4.36476395e+10]] 

**i haven't shown of 100*4 matrix make less messy. want select twenty (20*4) minimum values out of it. i'm trying winner = np.argmin(fitvec[100,1]) gives me 1 minimum value whereas want 20 min values. how should go it?

first off, i'd separate indices , values; no need store them both float. after that, numpy.argsort friend:

import numpy idx = numpy.arange(20, 38, dtype=int) vals = numpy.random.rand(len(idx))  = numpy.argsort(vals)  sorted_idx = idx[i] sorted_vals = vals[i]  print(idx) print(vals) print print(sorted_idx) print(sorted_vals) 

output:

[20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37] [ 0.00560689  0.73380138  0.53490514  0.1221538   0.45490855  0.39076217   0.39906252  0.59933451  0.7163099   0.393409    0.15854323  0.4631854   0.92469362  0.69999709  0.67664291  0.73184184  0.52893679  0.60365631]  [20 23 30 25 29 26 24 31 36 22 27 37 34 33 28 35 21 32] [ 0.00560689  0.1221538   0.15854323  0.39076217  0.393409    0.39906252   0.45490855  0.4631854   0.52893679  0.53490514  0.59933451  0.60365631   0.67664291  0.69999709  0.7163099   0.73184184  0.73380138  0.92469362] 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -