python——argsort函数

numpy中argsort函数用法,有需要的朋友可以参考下。

在Python中使用help帮助

>>> import numpy

>>> 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

`a` that index data along the given axis in sorted order.

从中可以看出argsort函数返回的是数组值从小到大的索引值

Examples

--------

One dimensional array:一维数组

>>> x = np.array([3, 1, 2])

>>> np.argsort(x)

array([1, 2, 0])

Two-dimensional array:二维数组

>>> x = np.array([[0, 3], [2, 2]])

>>> x

array([[0, 3],

[2, 2]])

>>> np.argsort(x, axis=0) #按列排序

array([[0, 1],

[1, 0]])

>>> np.argsort(x, axis=1) #按行排序

array([[0, 1],

[0, 1]])

#######################################

例1:

>>> x = np.array([3, 1, 2])

>>> np.argsort(x) #按升序排列

array([1, 2, 0])

>>> np.argsort(-x) #按降序排列

array([0, 2, 1])

>>> x[np.argsort(x)] #通过索引值排序后的数组

array([1, 2, 3])

>>> x[np.argsort(-x)]

array([3, 2, 1])

另一种方式实现按降序排序:

>>> a = x[np.argsort(x)]

>>> a

array([1, 2, 3])

>>> a[::-1]

array([3, 2, 1])

另外说到排序,还有两个常用的函数sort和sorted,详细内容请看:http://maoersong.blog.163.com/blog/static/171557351201424105925681/?newFollowBlog

时间: 2024-08-11 05:43:42

python——argsort函数的相关文章

关于python中argsort()函数的使用

在实现<机器学习实战>中kNN代码时遇到需要将计算好的距离进行排序,即可使用argsort()函数,在此依据个人理解对该函数进行简单的介绍. 总的来说,argsort()函数是对数组中的元素进行从小到大排序,并返回相应序列元素的数组下标. 以下通过例子进行详细解释. 1. 先定义一个数组 >>>from numpy import * >>>a = array([7, 8, 5, -3, 10, 9]) 2. 调用argsort()函数,将返回的值赋给y,并查

python之函数用法capitalize()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法capitalize() #capitalize() #说明:将字符串的第一个字母变成大写,其他字母变小写. ''' capitalize(...) S.capitalize() -> string Return a copy of the string S with only its first character capitalized. ''' #案例 str='xiaoden

关于python的函数的*和**参数:

1.将足够的参数解包以后传递给函数:>def f(p1, p2, p3, p4):>     print p1+p2+p3+p4>>li = [1, 2, 3, 4]>f(*li)10>>tu = (1, 2, 3, 4)>f(*tu)10>>di = {'p1':1, 'p2':2, 'p3':3, 'p4':4}>f(**di)>10 2.使用封包的方法访问多余的参数>>> def funct(*para, **

python之函数用法setdefault()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setdefault() #D.get(k,d) #说明:k在D中,则返回 D[K],如果k不在D中,则返回d值 #D.get(k,d), also set D[k]=d if k not in D ''' >>> help(dict.setdefault) Help on built-in function setdefault: setdefault(...) D.set

python之函数用法islower()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法islower() #http://www.runoob.com/python/att-string-islower.html #islower() #说明:检测字符串是否都由小写字母组成 str = "THIS is string example....wow!!!" print str.islower()#False str = "this is string

python之函数用法xrange()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法xrange() #xrange() #说明:返回一个生成器 #xrange做循环的性能比range好,尤其是返回很大的时候.除非要返回一个列表,则用range. ''' class xrange(object) | xrange(stop) -> xrange object | xrange(start, stop[, step]) -> xrange object | | Li

python之函数用法startswith()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法startswith() #http://www.runoob.com/python/att-string-startswith.html #startswith() #说明:返回布尔值,用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False. ''' startswith(...) S.startswith(prefix[, start[, end]]

python之函数用法globals()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法globals() #globals() #说明:在当前作用域下,查看全局变量 ''' globals(...) globals() -> dictionary Return the dictionary containing the current scope's global variables. ''' #案例 b='xiaodeng' print globals#<buil

python之函数用法__setattr__

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__setattr__ #http://www.cnblogs.com/hongfei/p/3858256.html #用__setattr__函数重构方法 class Fruit(): def __init__(self,color,price): self.__color = color self.__price = price def __setattr__(self,name