浅析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 RC4加密操作示例【测试可用】

python RC4加密操作示例【测试可用】

本文实例讲述了python RC4加密操作。分享给大家供大家参考,具体如下: # -*- conding:utf-8 -*- from Crypto.Cipher import AR...

Python imutils 填充图片周边为黑色的实现

Python imutils 填充图片周边为黑色的实现

代码 import imutils import cv2 image = cv2.imread('') # translate the image x=25 pixels to t...

django模板加载静态文件的方法步骤

加载静态文件 在一个网页中,不仅仅只有一个 html 骨架,还需要 css 样式文件, js 执行文件以及一些图片等。因此在 DTL 中加载静态文件是一个必须要解决的问题。在 DTL 中...

Python字符串、元组、列表、字典互相转换的方法

废话不多说了,直接给大家贴代码了,代码写的不好还去各位大侠见谅。 #-*-coding:utf-8-*- #1、字典 dict = {'name': 'Zara', 'age':...

Python 网络编程说明第1/2页

一、网络知识的一些介绍 socket 是网络连接端点。例如当你的Web浏览器请求www.jb51.net上的主页时,你的Web浏览器创建一个socket并命令它去连接 www.jb51....