search
Search
Unlock 100+ guides
search toc
close
Outline
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# NumPy | argpartition method

schedule Aug 12, 2023
Last updated
local_offer
PythonNumPy
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

NumPy's `argparition(~)` method is used to partition an array based on ascending order. Explaining how it works in words is rather difficult, so let's go through a quick example.

# Examples

Suppose we have the following unsorted array:

``` x = np.array([8,5,7,10,9]) ```

The sorted version of `x` is:

``` [5,7,8,9,10] ```

The original indices of this sorted version is:

``` [1,2,0,4,3] ```

Instead of returning this sorted indices, the `argpartition()` method asks you to select an index by which to partition, and ensures that all values of indices smaller than the value at this index appear before it, and all values of indices larger than this index value appear after it.

Here's an example:

``` x = np.array([8,5,7,9,6,2])np.argpartition(x,3) array([4, 5, 1, 2, 3, 0]) ```

Here, we set `kth=1`, which means that we want all values up to and including index `3` to be in its rightful place when sorted. In other words, the 3rd index, or the fourth (3+1) smallest value of `x` (7 in this case), will be in its rightful place when sorted, with all values smaller than 7 coming before it, and all those larger than 7 to come after it. Therefore, some of the valid sorted versions are as follows:

``` [4, 5, 1, 2, 3, 0][4, 1, 2, 5, 3, 0][4, 5, 1, 2, 3, 0]... ```
WARNING

The ordering of the values before after the `k`th index is undefined. This is why the value 4 can potentially come before the value 1. The `argpartition` chooses one out of the above, and returns its indices. As such, there is no guarantee that the returned array of indices represents a perfectly sorted array.

In our specific example, the returned indices were:

``` y array([4, 5, 1, 2, 3, 0]) ```

Which represents the following sorted version:

``` x[y] array([6, 2, 5, 7, 9, 8]) ```

Here, note the following:

• the values before the k-th index (index 3 in this case) are smaller than the value at the k-th index, but they are not necessarily sorted.

• the values after the k-th index are larger than the value at the k-th index

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
2
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!