# What is our equivalent of numpy.argpartition?

**URL:** <https://discourse.julialang.org/t/what-is-our-equivalent-of-numpy-argpartition/75626>\
**Category:** General Usage\
**Tags:** sortperm\
**Created:** [February 2, 2022, 7:29am UTC](https://discourse.julialang.org/t/what-is-our-equivalent-of-numpy-argpartition/75626 "2022-02-02T07:29:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rakeshvar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rakeshvar/32/3613_2.png) [@rakeshvar](https://discourse.julialang.org/u/rakeshvar)\
**Post date:** [February 2, 2022, 7:29am UTC](https://discourse.julialang.org/t/what-is-our-equivalent-of-numpy-argpartition/75626/1 "2022-02-02T07:29:56Z")

</div>

What is the julia equivalent of [numpy’s argpartition](https://numpy.org/doc/stable/reference/generated/numpy.argpartition.html)?

Here is how it works:  
Let’s say you want to get the indices of the top five entries of an array.  
As an example, say I have probabilites of `n` events stored in an array. Now I want to find the index of the top five most likely events. I would do so in numpy:

```julia
indices_of_most_likely_5 = np.argpartition(probabilities, -5)[-5:]

```

It is based on the `partition` function:

```julia
least_5_at_the_beginning = np.partition(probabilities, 5)

```

This will partially sort probabilities so that the least five values are at the beginning and the rest later.

It does not sort the whole array, thus saving a lot of time `O(n)` vs `O(n log(n))`.

Thank you.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [February 2, 2022, 7:32am UTC](https://discourse.julialang.org/t/what-is-our-equivalent-of-numpy-argpartition/75626/2 "2022-02-02T07:32:27Z")

</div>

```julia
partialsortperm(probabilities, 1:5, rev=true)

```
