问题描述
很多时候一个样本对应多个属性。
我们想要对全体样本一个属性A进行降序或升序排列,并希望其他属性作为整体,跟着属性A的位置变动一起动。
比如下面的例子:
样本1 | 样本2 | 样本3 | 样本4 | |
---|---|---|---|---|
属性A | 2 | 1 | 4 | 3 |
属性B | 4 | 3 | 2 | 1 |
我们希望对属性A进行降序排列,而属性B跟着属性A一起移动(即以样本为单位排序)。期望的排序结果如下:
样本2 | 样本1 | 样本4 | 样本3 | |
---|---|---|---|---|
属性A | 1 | 2 | 3 | 4 |
属性B | 3 | 4 | 1 | 2 |
解决方式
使用np.argsort()
函数。
语法
其官网文档:numpy.argsort
numpy.argsort(a, axis=-1, kind=None, order=None)
Returns the indices that would sort an array.(返回可用于排序array的索引,通过该索引访问其他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.
The default is ‘quicksort’. Note that both ‘stable’ and ‘mergesort’ use timsort under the covers and, in general,
the actual implementation will vary with data type.
The ‘mergesort’ option is retained for backwards compatibility.
Changed in version 1.15.0.: The ‘stable’ option was added.
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=axis) always yields the sorted a, irrespective of dimensionality.
示例
import numpy as np
# 对一个行或列升序排列并扩展到其他行或列(降序排列则取负即可)
a=np.array([2,1,4,3])
b=np.array([4,3,2,1])
a_idx=np.argsort(a)
print(a_idx)
# [1 0 3 2]
a_sort=a[a_idx]
print(a_sort)
# [1 2 3 4]
b_sort_like_a = b[a_idx]
print(b_sort_like_a)
# [3 4 1 2]
a_sort)
# [1 2 3 4]
b_sort_like_a = b[a_idx]
print(b_sort_like_a)
# [3 4 1 2]