Reputation: 3
fitVec = np.zeros((100, 2))
#Initializing the fitVec, where first column` will be the indices and second column will contain the values
After Initialization, fitVec gets assigned some values by running a 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 all of the 100*4 matrix to make it look less messy. Now I want to select the twenty (20*4) minimum values out of it. I'm trying winner = np.argmin(fitVec[100,1]) but it gives me only one minimum value whereas I want 20 min values. How should I go about it?
Upvotes: 0
Views: 291
Reputation: 58721
First off, I'd separate indices and values; no need to store them both as float
. After that, numpy.argsort
is your friend:
import numpy
idx = numpy.arange(20, 38, dtype=int)
vals = numpy.random.rand(len(idx))
i = 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]
Upvotes: 1