Skip to content
Advertisement

Find line number of a value which matches upto 2 decimal in python numpy

I tried to find the line number of value which is 60 % of the maximum value in file.

if max_value=1
   v_60 = 0.6

My file looks like this,

2.140672782874617625e-02
9.785932721712538307e-02
1.651376146788990640e-01
2.140672782874617763e-01
2.568807339449541427e-01
2.996941896024464813e-01
3.363914373088685017e-01
3.639143730886849615e-01
3.975535168195718505e-01
4.097859327217125425e-01
4.250764525993884213e-01
4.525993883792048256e-01
4.678899082568807044e-01
4.740061162079510226e-01
4.954128440366972197e-01
5.076452599388379117e-01
5.259938837920489219e-01
5.351681957186543714e-01
5.443425076452599320e-01
5.626911314984709422e-01
5.871559633027523262e-01
6.055045871559633364e-01
6.055045871559633364e-01
6.116207951070335991e-01
6.238532110091742355e-01
8.715596330275228176e-01
8.623853211009173680e-01
8.715596330275228176e-01
8.807339449541283782e-01
8.837920489296635651e-01
8.807339449541283782e-01
8.899082568807338278e-01
8.868501529051986410e-01
9.663608562691131665e-01
9.816513761467889898e-01
9.755351681957186161e-01
9.724770642201834292e-01
9.785932721712538029e-01
9.877675840978593635e-01
1.000000000000000000e+00
1.000000000000000000e+00

Now I print the line number of the max value by

l=numpy.where(file == np.amax(file))

now how to find the line number which has 0.60 just comparing up to two decimal value only in python.

Advertisement

Answer

This will find the element closest to the 60% mark:

findit = data.max() * 0.6
print(np.argmin(np.abs((data-findit))))

Output:

21
2 People found this is helpful
Advertisement