I have need the N minimum (index) values in a numpy array

后端 未结 5 709
孤独总比滥情好
孤独总比滥情好 2021-02-05 02:57

Hi I have an array with X amount of values in it I would like to locate the indexs of the ten smallest values. In this link they calculated the maximum effectively, How to get i

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 03:08

    Just don't reverse the sort results.

    In [164]: a = numpy.random.random(20)
    
    In [165]: a
    Out[165]: 
    array([ 0.63261763,  0.01718228,  0.42679479,  0.04449562,  0.19160089,
            0.29653725,  0.93946388,  0.39915215,  0.56751034,  0.33210873,
            0.17521395,  0.49573607,  0.84587652,  0.73638224,  0.36303797,
            0.2150837 ,  0.51665416,  0.47111993,  0.79984964,  0.89231776])
    

    Sorted:

    In [166]: a.argsort()
    Out[166]: 
    array([ 1,  3, 10,  4, 15,  5,  9, 14,  7,  2, 17, 11, 16,  8,  0, 13, 18,
           12, 19,  6])
    

    First ten:

    In [168]: a.argsort()[:10]
    Out[168]: array([ 1,  3, 10,  4, 15,  5,  9, 14,  7,  2])
    

提交回复
热议问题