繁体   English   中英

查找一个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 2.14579526e+08 0.66 0.3 . . 

需求

No 1:我想find the indices in Speed arrayvalues greater than 20 find the indices in Speed array

No 2:对于那些索引, what will be values in Brake Array

No 3:现在我想Top N Maximum Value indices in Brake Array找到Top N Maximum Value indices in Brake Array并将其存储在另一个列表/阵列中

所以最后,如果我从前Top N Maximum Indices一个索引,并将其用于“刹车和速度”数组,它必须显示出来。

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

一般总结

简而言之,我需要找到对应的速度值应大于20的最大N个制动点索引。

我尝试了什么

    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,但结果都不符合我的要求

请求

我相信会有最好的方法来做。

(我将上面的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]]

要获得最大的Brak_press:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM