how numpy partition work

后端 未结 2 1202
庸人自扰
庸人自扰 2021-01-17 20:13

I am trying to figure out how np.partition function works. For example, consider

arr = np.array([ 5, 4, 1, 0, -1, -3, -4, 0])

2条回答
  •  醉话见心
    2021-01-17 20:42

    The docs talk of 'a sorted array'.

    np.partition starts by sorting the elements in the array provided. In this case the original array is:

    arr = [ 5,  4,  1,  0, -1, -3, -4,  0]
    

    When sorted, we have:

    arr_sorted = [-4 -3 -1  0  0  1  4  5]
    

    Hence the call, np.partition(arr, kth=2), will actually have the kth as the the element in position 2 of the arr_sorted, not arr. The element is correctly picked as -1.

提交回复
热议问题