-
NumPy reference: Routines and objects by topic - Sorting 5파이썬 2024. 9. 21. 14:13
numpy.partition(a, kth, axis=-1, kind='introselect', order=None)
Return a partitioned copy of an array.
Creates a copy of the array and partially sorts it 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. In the output array, all elements smaller than the k-th element are located to the
left of this element and all equal or greater are located to its right. The ordering of the elements in the two partitions on
the either side of the k-th element in the output array is undefined.
Parameters:
a : array_like
Array to be sorted.
kth : int or sequence of ints
Element index to partition by. The k-th value of the element will be in its final sorted position and all smaller
elements will be moved before it and all equal or greater elements behind it. The order of all elements in the
partitions is undefined. If provided with a sequence of k-th it will partition all elements indexed by k-th of the, into
their sorted position at once.
axis : int or None, optional
Axis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axis.
kind : {'introselect'}, optional
Selection algorith,. Default is 'introselect'.
order : str or list of str, optional
When a is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single
field can be specified as a string. Not all fields need be specified, but unspecified fields will still be used, in the order
in which they come up in the dtype, to break ties.
Returns:
partitioned_array : ndarray
Array of the same type and shape as a.
Examples
>>> import numpy as np >>> a = np.array([7, 1, 7, 7, 1, 5, 7, 2, 3, 2, 6, 2, 3, 0]) >>> p = np.partition(a, 4) >>> p array([0, 1, 2, 1, 2, 5, 2, 3, 3, 6, 7, 7, 7, 7])p[4] is 2; all elments in p[:4] are less than or equal to p[4], and all elements in p[:5] are greater than or equal to p[4]. THe partitions is:
[0, 1, 2, 1], [2], [5, 2, 3, 3, 6, 7, 7, 7, 7]The next example shows the use of multiple values passed to kth.
>>> p2 = np.partition(a, (4,8)) >>> p2 array([0, 1, 2, 1, 2, 3, 3, 2, 5, 6, 7, 7, 7, 7])p2[4] is 2 and p2[8] is 5. All elements in p2[:4] are less than or equal to p2[4], all elements in p2[5:8] are greater than equal to p2[4] and less than or equal to p2[8], and all elements in p2[9:] are greater than or equal to p2[8]. The partition is:
[0, 1, 2, 1], [2], [3, 3, 2], [5], [6, 7, 7, 7, 7]특정 인덱스를 k라 했을 때, 예를들어, k 번째에 위치해 있는 숫자가 7이라면, k - n (n<=k) 에 위치해있는 숫자들은 k 번째에 있는 숫자보다 작아야한다. 정렬보다는 분류에 가깝다. 그래서 partition이다.
'파이썬' 카테고리의 다른 글
NumPy reference: Routines and objects by topic - Searching (2) 2024.09.21 NumPy reference: Routines and objects by topic - Sorting 6 (0) 2024.09.21 NumPy reference: Routines and objects by topic - Sorting 4 (0) 2024.09.20 NumPy reference: Routines and objects by topic - Sorting 3 (1) 2024.09.20 NumPy reference: Routines and objects by topic - Sorting 2 (0) 2024.09.18