文章目录
1. numpy.sort
numpy.sort(a, axis=-1, kind='quicksort', order=None) Return a sorted copy of an array. Parameters: a : array_like Array to be sorted. 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 : {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optional Sorting algorithm. Default is ‘quicksort’. 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, and not all fields need be specified, but unspecified fields will still beused, in the order in which they come up in the dtype, to break ties. Returns: sorted_array : ndarray Array of the same type and shape as a. |
函数功能:返回沿指定轴排序的数组副本。
参数 | 功能 |
---|---|
a | 待排序的数组或类似数组的对象 |
axis | 待排序的轴 axis=None,将所有元素排序,返回一维数组; axis为其它整数值,则沿着指定轴排序,默认值-1,沿最后一个轴排序 |
kind | 排序方法,可选,{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’} |
order | 用于指定字段类型的结构化数组排序的参数 |
>>> np.sort([[1,3,2],[9,7,8],[6,5,4]], axis=0)
array([[1, 3, 2],
[6, 5, 4],
[9, 7, 8]])
>>> np.sort([[1,3,2],[9,7,8],[6,5,4]], axis=1)
array([[1, 2, 3],
[7, 8, 9],
[4, 5, 6]])
>>> np.sort([[1,3,2],[9,7,8],[6,5,4]])
array([[1, 2, 3],
[7, 8, 9],
[4, 5, 6]])
>>> np.sort([[1,3,2],[9,7,8],[6,5,4]], axis=None)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
2. numpy.argsort
numpy.argsort(a, axis=-1, kind='quicksort', order=None) Returns the indices that would sort an array. Perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in sorted order. Parameters: a : array_like Array to sort. axis : int or None, optional Axis along which to sort. The default is -1 (the last axis). If None, the flattened array is used. kind : {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optional Sorting algorithm. 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, and 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: index_array : ndarray, int Array of indices that sort a along the specified axis. If a is one-dimensional, a[index_array] yields a sorted a. More generally, np.take_along_axis(a, index_array, axis=a) always yields the sorted a, irrespective of dimensionality. |
函数功能:返回沿指定轴对数组进行排序的索引,即各元素在原数组中的位置。
参数 | 功能 |
---|---|
a | 待排序的数组或类似数组的对象 |
axis | 待排序的轴 axis=None,将所有元素排序,返回一维数组; axis为其它整数值,则沿着指定轴排序,默认值-1,沿最后一个轴排序 |
kind | 排序方法,可选,{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’} |
order | 用于指定字段类型的结构化数组排序的参数 |
>>> np.argsort([[1,3,2],[9,7,8],[6,5,4]], axis=0)
array([[0, 0, 0],
[2, 2, 2],
[1, 1, 1]], dtype=int64)
>>> np.argsort([[1,3,2],[9,7,8],[6,5,4]], axis=1)
array([[0, 2, 1],
[1, 2, 0],
[2, 1, 0]], dtype=int64)
>>> np.argsort([[1,3,2],[9,7,8],[6,5,4]])
array([[0, 2, 1],
[1, 2, 0],
[2, 1, 0]], dtype=int64)
>>> np.argsort([[1,3,2],[9,7,8],[6,5,4]], axis=None)
array([0, 2, 1, 8, 7, 6, 4, 5, 3], dtype=int64)
3. numpy.partition
numpy.partition(a, kth, axis=-1, kind='introselect', order=None) Return a partitioned copy of an array. 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. The ordering of the elements in the two partitions is undefined. New in version 1.8.0. 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 them 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 algorithm. 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. |
函数功能:返回数组沿指定轴分区/部分排序的副本。排序后的数组副本第k个位置的元素的值不小于其左边元素的值,不大于其右边元素的值。也就是说,返回的数组副本的第k个位置的元素位于正确的有序位置。
参数 | 功能 |
---|---|
a | 待排序的数组或类似数组的对象 |
kth | 第k个位置,可指定多个位置 |
axis | 待排序的轴 axis=None,将所有元素排序,返回一维数组; axis为其它整数值,则沿着指定轴排序,默认值-1,沿最后一个轴排序 |
kind | 排序方法,可选,{‘introselect’} |
order | 用于指定字段的结构化数组排序的参数 |
>>> a = np.array([ 8, 10, 3, 2, 14, 9, 7, 11, 0, 13, 6, 1, 5, 4, 12, 15])
>>> np.partition(a, 4) # 第4个位置的元素位于正确有序元素,即元素4位于有序位置
array([ 2, 0, 1, 3, 4, 5, 6, 10, 7, 8, 9, 11, 13, 14, 12, 15])
>>> np.partition(a, (4,12)) # 第4和12个位置的元素位于正确有序位置
array([ 2, 0, 1, 3, 4, 5, 6, 10, 7, 8, 9, 11, 12, 14, 13, 15])
a = np.asarray([np.random.randint(0,20, 10) for _ in range(10)])
>>> a = np.asarray([np.random.randint(0,10, 10) for _ in range(5)])
>>> a
array([[1, 0, 2, 2, 0, 1, 7, 3, 6, 9],
[9, 8, 0, 2, 8, 2, 0, 2, 6, 9],
[2, 6, 3, 5, 5, 7, 7, 3, 2, 2],
[3, 2, 7, 8, 5, 1, 2, 1, 3, 1],
[4, 0, 1, 4, 3, 2, 2, 3, 9, 7]])
>>> np.partition(a, 2, axis=0) # 对各列分区排序,结果第2行为分界线
array([[1, 0, 0, 2, 0, 1, 0, 1, 2, 1],
[2, 0, 1, 2, 3, 1, 2, 2, 3, 2],
[3, 2, 2, 4, 5, 2, 2, 3, 6, 7],
[9, 6, 7, 8, 5, 7, 7, 3, 6, 9],
[4, 8, 3, 5, 8, 2, 7, 3, 9, 9]])
>>> np.partition(a, 5, axis=1) # 对各行分区排序,结果第5列为分界线
array([[0, 0, 1, 1, 2, 2, 3, 6, 7, 9],
[2, 2, 0, 0, 2, 6, 8, 8, 9, 9],
[2, 2, 2, 3, 3, 5, 5, 6, 7, 7],
[1, 1, 1, 2, 2, 3, 3, 8, 7, 5],
[0, 1, 2, 2, 3, 3, 4, 4, 9, 7]])
4. numpy.argpartition
numpy.argpartition(a, kth, axis=-1, kind='introselect', order=None) Perform an indirect partition along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in partitioned order. New in version 1.8.0. Parameters: a : array_like Array to sort. kth : int or sequence of ints Element index to partition by. The k-th element will be in its final sorted position and all smaller elements will be moved before it and all larger elements behind it. The order all elements in the partitions is undefined. If provided with a sequence of k-th it will partition all of them into their sorted position at once. axis : int or None, optiona l Axis along which to sort. The default is -1 (the last axis). If None, the flattened array is used. kind : {‘introselect’}, optional Selection algorithm. 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, and 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: index_array : ndarray, int Array of indices that partition a along the specified axis. If a is one-dimensional, a[index_array] yields a partitioned a. More generally, np.take_along_axis(a, index_array, axis=a) always yields the partitioned a, irrespective of dimensionality. |
函数功能:返回数组沿指定轴分区/部分排序的索引数组。索引数组中位置k的索引对应的元素不小于位置k之前索引对应的元素,不大于位置k之后索引对应的元素。
参数 | 功能 |
---|---|
a | 待排序的数组或类似数组的对象 |
kth | 第k个位置,可指定多个位置 |
axis | 待排序的轴 axis=None,将所有元素排序,返回一维数组; axis为其它整数值,则沿着指定轴排序,默认值-1,沿最后一个轴排序 |
kind | 排序方法,可选,{‘introselect’} |
order | 用于指定字段的结构化数组排序的参数 |
>>> a = np.array([ 8, 10, 3, 2, 14, 9, 7, 11, 0, 13, 6, 1, 5, 4, 12, 15])
>>> np.argpartition(a, 4)
array([ 3, 8, 11, 2, 13, 12, 10, 1, 6, 0, 5, 7, 9, 4, 14, 15],
dtype=int64)
>>> np.argpartition(a, (4,12))
array([ 3, 8, 11, 2, 13, 12, 10, 1, 6, 0, 5, 7, 14, 4, 9, 15],
dtype=int64)
>>> a[np.argpartition(a, (4,12))] # 等价于np.partition(a, (4,12))
array([ 2, 0, 1, 3, 4, 5, 6, 10, 7, 8, 9, 11, 12, 14, 13, 15])
5. numpy.searchsorted
numpy.searchsorted(a, v, side='left', sorter=None) Find indices where elements should be inserted to maintain order. Find the indices into a sorted array a such that, if the corresponding elements in v were inserted before the indices, the order of a would be preserved. Assuming that a is sorted: side returned index i satisfies left a[i-1] < v <= a[i] right a[i-1] <= v < a[i] Parameters: a : 1-D array_like Input array. If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it. v : array_like Values to insert into a. side : {‘left’, ‘right’}, optional If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index. If there is no suitable index, return either 0 or N (where N is the length of a). sorter : 1-D array_like, optional Optional array of integer indices that sort array a into ascending order. They are typically the result of argsort. New in version 1.7.0. Returns: indices : array of ints Array of insertion points with the same shape as v. |
函数功能:给定升序数组以及待插入元素,返回保持序列有序的插入位置。
参数 | 功能 |
---|---|
a | 升序的数组或类似数组的对象 |
v | 待插入的值,可以插入多个值 |
side | 插入值的优先位置 ‘left’:a[i-1] < v <= a[i];‘right’:a[i-1] <= v < a[i] |
sorter | 将数组a按升序排序的索引数组,通常是argsort的结果数组,可选 |
>>> np.searchsorted([1,2,3,4,5], 3)
2
>>> np.searchsorted([1,2,3,4,5], 3, side='right')
3
>>> np.searchsorted([1,2,3,4,5], [-10, 10, 2, 3])
array([0, 5, 1, 2])
6. numpy.lexsort
numpy.lexsort(keys, axis=-1) Perform an indirect stable sort using a sequence of keys. Given multiple sorting keys, which can be interpreted as columns in a spreadsheet, lexsort returns an array of integer indices that describes the sort order by multiple columns. The last key in the sequence is used for the primary sort order, the second-to-last key for the secondary sort order, and so on. The keys argument must be a sequence of objects that can be converted to arrays of the same shape. If a 2D array is provided for the keys argument, it’s rows are interpreted as the sorting keys and sorting is according to the last row, second last row etc. Parameters: keys : (k, N) array or tuple containing k (N,)-shaped sequences The k different “columns” to be sorted. The last column (or row if keys is a 2D array) is the primary sort key. axis : int, optional Axis to be indirectly sorted. By default, sort over the last axis. Returns: indices : (N,) ndarray of ints Array of indices that sort the keys along the specified axis. |
函数功能:使用给定多个关键字进行间接稳定排序,最后一个键用于主排序键,返回最后一个关键字排序后的索引。
参数 | 功能 |
---|---|
keys | 要排序的不同的列 |
axis | 待排序的轴 axis=None,将所有元素排序,返回一维数组; axis为其它整数值,则沿着指定轴排序,默认值-1,沿最后一个轴排序 |
>>> a = np.array([1,5,1,4,3,4,4])
>>> b = np.array([9,4,0,4,0,2,1])
>>> np.argsort(a)
array([0, 2, 4, 3, 5, 6, 1], dtype=int64)
# 以a为主排序键,b为次排序键
# a[0]=a[2],但b[0]>b[2],因此对于数组a,0位置的元素在前,1位置元素在后
>>> np.lexsort((b,a))
array([2, 0, 4, 6, 5, 3, 1], dtype=int64)
>>> a[np.lexsort((b,a))]
array([1, 1, 3, 4, 4, 4, 5])