日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

python的argsort函数_python——argsort函数

發(fā)布時(shí)間:2023/12/4 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python的argsort函数_python——argsort函数 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

numpy中argsort函數(shù)用法,有需要的朋友可以參考下。

在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函數(shù)返回的是數(shù)組值從小到大的索引值

Examples

--------

One dimensional array:一維數(shù)組

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

>>> np.argsort(x)

array([1, 2, 0])

Two-dimensional array:二維數(shù)組

>>> 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)] #通過(guò)索引值排序后的數(shù)組

array([1, 2, 3])

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

array([3, 2, 1])

另一種方式實(shí)現(xiàn)按降序排序:

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

>>> a

array([1, 2, 3])

>>> a[::-1]

array([3, 2, 1])

總結(jié)

以上是生活随笔為你收集整理的python的argsort函数_python——argsort函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。