Mari
Mari

Reputation: 698

To find N Maximum indices of a numpy array whose corresponding values should greater than M in another array

I have 3 Numpy arrays each of length 107952899.

Lets Say :

1. Time = [2.14579526e+08 2.14579626e+08 2.14579726e+08 ...1.10098692e+10  1.10098693e+10]
2. Speed = [0.66 0.66 0.66 .............0.06024864 0.06014756]

3. Brak_press = [0.3, 0.3, 0.3 .............. 0.3, 0.3]

What it mean

Each index Value in Time corresponds to same index Value in Speed & Brake array.

Time                Speed         Brake
2.14579526e+08      0.66          0.3
.
.

Requirement

No 1 :I want to find the indices in Speed array whose values inside are greater than 20

No 2 : for those indices, what will be values in Brake Array

No 3 : Now i want to find the Top N Maximum Value indices in Brake Array & Store it another list/array

So finally if I take one indices from Top N Maximum Indices and use it in Brake & Speed array it must show..

Brake[idx] = valid Value & more importantly Speed [idx] = Value > than 20

General Summary

Simply, What i needed is, to find the Maximum N Brake point indices whose corresponding speed Value should be greater than 20

What i tried

    speed_20 = np.where(Speed > 20) # I got indices as tupple 
brake_values = Brake[speed_20]  # Found the Brake Values corresponds to speed_20 indices

After that i tried argsort/argpartition but none of result matches my requirement

Request

I believe there will be a best method to do this..Kindly shed some light

(I converted the above np arrays to pandas df, it works fine, due to memory concern i prefer to do using numpy operations)

Upvotes: 0

Views: 90

Answers (2)

iGian
iGian

Reputation: 11183

Maybe this is an option you can consider, using NumPy.

First create a multidimensional matrix (I changed the values so it's easier to follow):

Time =        [  2,   1,   5,   4,   3]
Speed =       [ 10,  20,  40,  30,  50]
Brak_press =  [0.1, 0.3, 0.5, 0.4, 0.2]

data = np.array([Time, Speed, Brak_press]).transpose()

So data are stored as:

print(data)
# [[ 2.  10.   0.1]
#  [ 1.  20.   0.3]
#  [ 5.  40.   0.5]
#  [ 4.  30.   0.4]
#  [ 3.  50.   0.2]]

To extract speed greater than 20:

data[data[:,1] > 20]
# [[ 5.  40.   0.5]
#  [ 4.  30.   0.4]
#  [ 3.  50.   0.2]]

To get the n greatest Brak_press:

n = 2
data[data[:,2].argsort()[::-1][:n]]
# [[ 5.  40.   0.5]
#  [ 4.  30.   0.4]]

Upvotes: 1

javidcf
javidcf

Reputation: 59691

You are almost there. This should do what you want:

speed_20 = np.where(Speed > 20)[0]
sort = np.argsort(-Brake[speed_20])
result = speed_20[sort[:N]]

Upvotes: 1

Related Questions