浅析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中logging实例讲解

Python中logging实例讲解

logging 的基本用法网上很多,这里就不介绍了。在引入正文之前,先来看一个需求: 假设需要将某功能封装成类库供他人使用,如何处理类库中的日志? 数年前在一个 C# 开发的项目中,我用...

Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法

使用Python写文件的时候,或者将网络数据流写入到本地文件的时候,大部分情况下会遇到:UnicodeEncodeError: 'gbk' codec can't encode char...

Python函数和模块的使用总结

函数和模块的使用 在讲解本章节的内容之前,我们先来研究一道数学题,请说出下面的方程有多少组正整数解。 $$x_1 + x_2 + x_3 + x_4 = 8$$ 事实上,上面的问题等同...

Python之inspect模块实现获取加载模块路径的方法

该文主要介绍如何获取模块的路径,需要申明的是这里所说的模块可以是功能实现的该模块,也可以是别的模块。 使用到的是 inspect 模块的 .getsourcefile(需要获取的模块名)...

对python实时得到鼠标位置的示例讲解

对python实时得到鼠标位置的示例讲解

如下所示: #先下载pyautogui库,pip install pyautogui import os,time import pyautogui as pag try: wh...