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

yipeiwu_com6年前Python基础

概述

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函数的使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

python字符串替换第一个字符串的方法

Python 截取字符串使用 变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾。 # 例1:字符串截取 str...

Python安装pycurl失败的解决方法

Centos安装pycurl centos 安装pycurl yum install python-devel curl-devel pip3 install pycurl Mac...

python使用matplotlib库生成随机漫步图

python使用matplotlib库生成随机漫步图

本教程使用python来生成随机漫步数据,再使用matplotlib将数据呈现出来 开发环境 操作系统: Windows10 IDE: Pycharm 2017.1.3 Python...

django-allauth入门学习和使用详解

django-allauth是集成的Django应用程序,用于解决网站身份验证,用户的注册登录及账户管理,以及第三方(社交)账户的身份验证。 既然你知道并准备使用django-allau...

Python在OpenCV里实现极坐标变换功能

Python在OpenCV里实现极坐标变换功能

在中学里学习过直角坐标系,也叫做笛卡尔坐标系,它是正交坐标系,不过也学习过极坐标系,这种坐标系比较适合大炮发射的场合。极坐标系的定义如下: 在 平面内取一个定点O, 叫极点,引一条射线O...