查找 numpy 数组的 N 个最大索引,其对应值应大于另一个数组中的 M
To find N Maximum indices of a numpy array whose corresponding values should greater than M in another array
我有3 Numpy arrays each of length 107952899
。
让我们说:
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]
什么意思
Time 中的每个索引值对应于 Speed & Brake 数组中的相同索引值。
Time Speed Brake
2.14579526e+08 0.66 0.3
.
.
要求
No 1 :我要find the indices in Speed array
其values
里面是greater than 20
No 2:对于这些指数,what will be values in Brake Array
否 3:现在我想找到 Top N Maximum Value indices in Brake Array
并将其存储在另一个 list/array
所以最后,如果我从 Top N Maximum Indices
中获取一个索引并将其用于 Brake & Speed 数组,它必须显示..
Brake[idx] = valid Value & more importantly Speed [idx] = Value > than 20
一般摘要
简单来说,我需要的是,找到对应速度值应该大于20的Maximum N Brake point indices
我试过的
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
之后我尝试了 argsort/argpartition 但结果 none 符合我的要求
请求
我相信会有一个最好的方法来做到这一点..请说明一下
(我将上面的 np arrays
转换为 pandas df
,它工作正常,由于内存问题我更喜欢使用 numpy 操作)
你快到了。这应该可以满足您的要求:
speed_20 = np.where(Speed > 20)[0]
sort = np.argsort(-Brake[speed_20])
result = speed_20[sort[:N]]
也许这是您可以考虑的一个选项,使用 NumPy。
首先创建一个多维矩阵(我更改了值以便更容易理解):
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()
因此数据存储为:
print(data)
# [[ 2. 10. 0.1]
# [ 1. 20. 0.3]
# [ 5. 40. 0.5]
# [ 4. 30. 0.4]
# [ 3. 50. 0.2]]
提取速度大于20:
data[data[:,1] > 20]
# [[ 5. 40. 0.5]
# [ 4. 30. 0.4]
# [ 3. 50. 0.2]]
取n大Brak_press:
n = 2
data[data[:,2].argsort()[::-1][:n]]
# [[ 5. 40. 0.5]
# [ 4. 30. 0.4]]
我有3 Numpy arrays each of length 107952899
。
让我们说:
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]
什么意思
Time 中的每个索引值对应于 Speed & Brake 数组中的相同索引值。
Time Speed Brake
2.14579526e+08 0.66 0.3
.
.
要求
No 1 :我要find the indices in Speed array
其values
里面是greater than 20
No 2:对于这些指数,what will be values in Brake Array
否 3:现在我想找到 Top N Maximum Value indices in Brake Array
并将其存储在另一个 list/array
所以最后,如果我从 Top N Maximum Indices
中获取一个索引并将其用于 Brake & Speed 数组,它必须显示..
Brake[idx] = valid Value & more importantly Speed [idx] = Value > than 20
一般摘要
简单来说,我需要的是,找到对应速度值应该大于20的Maximum N Brake point indices
我试过的
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
之后我尝试了 argsort/argpartition 但结果 none 符合我的要求
请求
我相信会有一个最好的方法来做到这一点..请说明一下
(我将上面的 np arrays
转换为 pandas df
,它工作正常,由于内存问题我更喜欢使用 numpy 操作)
你快到了。这应该可以满足您的要求:
speed_20 = np.where(Speed > 20)[0]
sort = np.argsort(-Brake[speed_20])
result = speed_20[sort[:N]]
也许这是您可以考虑的一个选项,使用 NumPy。
首先创建一个多维矩阵(我更改了值以便更容易理解):
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()
因此数据存储为:
print(data)
# [[ 2. 10. 0.1]
# [ 1. 20. 0.3]
# [ 5. 40. 0.5]
# [ 4. 30. 0.4]
# [ 3. 50. 0.2]]
提取速度大于20:
data[data[:,1] > 20]
# [[ 5. 40. 0.5]
# [ 4. 30. 0.4]
# [ 3. 50. 0.2]]
取n大Brak_press:
n = 2
data[data[:,2].argsort()[::-1][:n]]
# [[ 5. 40. 0.5]
# [ 4. 30. 0.4]]