How does numpy's argpartition work on the documentation's example?

后端 未结 3 763
一个人的身影
一个人的身影 2021-02-04 08:11

I am trying to understand numpy\'s argpartition function. I have made the documentation\'s example as basic as possible.

import numpy as np

x = np.array([3, 4,         


        
3条回答
  •  长发绾君心
    2021-02-04 08:44

    i remember having a hard time figuring it out too, maybe the documentation is written badly but this is what it means

    When you do a=np.argpartition(x, 3) then x is sorted in such a way that only the element at the k'th index will be sorted (in our case k=3)

    So when you run this code basically you are asking what would the value of the 3rd index be in a sorted array. Hence the output is ('x[a]:', array([2, 1, 3, 4]))where only element 3 is sorted.

    As the document suggests all numbers smaller than the kth element are before it (in no particular order) hence you get 2 before 1, since its no particular order.

    i hope this clarifies it, if you are still confused then feel free to comment :)

提交回复
热议问题