当前位置: 移动技术网 > IT编程>脚本编程>Python > 浅析python中numpy包中的argsort函数的使用

浅析python中numpy包中的argsort函数的使用

2018年09月23日  | 移动技术网IT编程  | 我要评论

春菜 qvod,丑娘娘视频,貂皮大衣天价

概述

argsort()函数在模块numpy.core.fromnumeric中。

在python中排序数组,或者获取排序顺序的时候,我们常常使用numpy包的argsort函数来完成。

如下图所示,是使用python获取到数组中的排序的顺序。

data=numpy.array([1,2,3,4,5])
datasort=numpy.argsort(data)
datasort
out[39]: array([0, 1, 2, 3, 4], dtype=int64)
data
out[40]: array([1, 2, 3, 4, 5])
datasort1=data.argsort()
datasort1
out[42]: array([0, 1, 2, 3, 4], dtype=int64)

我们也可以通过help(numpy.argsort)来查看使用方法

help(numpy.argsort)
help on function argsort in module numpy.core.fromnumeric:
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

如果想要通过argsort实现排序可以使用切片实现

data1=numpy.array([1,3,4,56,2,0])
datasort=data1[data1.argsort()]
datasort
out[48]: array([ 0, 1, 2, 3, 4, 56])

ps:numpy 中argsort函数

排序函数,返回array类型

argsort函数返回的是数组值从小到大的元素的索引值

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
inx = np.array([1,2,-1,3,4,7,8])
print inx
print inx.argsort()

总结

以上所述是小编给大家介绍的python中numpy包中的argsort函数的使用,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网