[python] 将一个序列的排序方式扩展到其他序列

问题描述

很多时候一个样本对应多个属性。

我们想要对全体样本一个属性A进行降序或升序排列,并希望其他属性作为整体,跟着属性A的位置变动一起动。

比如下面的例子:

样本1样本2样本3样本4
属性A2143
属性B4321

我们希望对属性A进行降序排列,而属性B跟着属性A一起移动(即以样本为单位排序)。期望的排序结果如下:

样本2样本1样本4样本3
属性A1234
属性B3412

解决方式

使用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]
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值