I didn't understand how numpy.partition
works. The documentation says:
Creates a copy of the array with its elements rearranged in such a way that the value of the element in k-th position is in the position it would be in a sorted array. All elements smaller than the k-th element are moved before this element and all equal or greater are moved behind it.
I have tried to run this code
import numpy as np
a = np.array([2, 4, 1, 5, 0])
np.partition(a,0)
The 0-th element of the array is 2, so I would expect the resulting array to be made as following: all elements smaller than 2 to the left of it, and all elements greater than 2 to the right of it.
Instead I get the following result:
array([0, 4, 1, 5, 2])
where 2 is in the last position.
How is this result justified given the documentation?
Can you provide a step-by-step example of what this np.partition
does?