numpy 排序,搜索和计数

排序numpy.sort()

numpy.sort(a[, axis=-1, kind='quicksort', order=None]) Return a sorted copy of an array.

axis:排序沿数组的(轴)方向,0表示按列1表示按行,None表示展开来排序,默认为-1,表示沿最后的轴排序(二维行,列,二维中最后一轴就是列轴。三维,x,y,z轴),将指定轴上的每一个元素都按照从小到大的顺序排列。=None 都拿出来排列 

默认为-1表示根据最大的维度进行排序,例如该数组为二维数组时,axis=-1与axis=1用法相同

import numpy as np

np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
# [[2.32 7.54 9.78 1.73 6.22]
#  [6.93 5.17 9.28 9.76 8.25]
#  [0.01 4.23 0.19 1.73 9.27]
#  [7.99 4.97 0.88 7.32 4.29]
#  [9.05 0.07 8.95 7.9  6.99]]

y = np.sort(x)  #默认为负1
print(y)
# [[1.73 2.32 6.22 7.54 9.78]
#  [5.17 6.93 8.25 9.28 9.76]
#  [0.01 0.19 1.73 4.23 9.27]
#  [0.88 4.29 4.97 7.32 7.99]
#  [0.07 6.99 7.9  8.95 9.05]]

y = np.sort(x, axis=0)
print(y)
# [[0.01 0.07 0.19 1.73 4.29]
#  [2.32 4.23 0.88 1.73 6.22]
#  [6.93 4.97 8.95 7.32 6.99]
#  [7.99 5.17 9.28 7.9  8.25]
#  [9.05 7.54 9.78 9.76 9.27]]

y = np.sort(x, axis=1)
print(y)
# [[1.73 2.32 6.22 7.54 9.78]
#  [5.17 6.93 8.25 9.28 9.76]
#  [0.01 0.19 1.73 4.23 9.27]
#  [0.88 4.29 4.97 7.32 7.99]
#  [0.07 6.99 7.9  8.95 9.05]]、

>>> y = np.sort(x, None)   #全部拿出来进行排序
>>> print(y)
[0.01 0.07 0.19 0.88 1.73 1.73 2.32 4.23 4.29 4.97 5.17 6.22 6.93 6.99
 7.32 7.54 7.9  7.99 8.25 8.95 9.05 9.27 9.28 9.76 9.78]

numpy.argsort()

numpy.argsort() 函数返回的是数组值从小到大的索引值。对数组沿给定轴执行间接排序,并使用指定排序类型返回数据的索引数组。这个索引数组用于构造排序后的数组。

import numpy as np

np.random.seed(20200612)
x = np.random.randint(0, 10, 10)
print(x)
# [6 1 8 5 5 4 1 2 9 1]

y = np.argsort(x)       #升序排列索引
print(y)
# [1 6 9 7 5 3 4 0 2 8]

print(x[y])             #打印y索引所对应的X的值
# [1 1 1 2 4 5 5 6 8 9]

y = np.argsort(-x)    #降序排列索引
print(y)
# [8 2 0 3 4 5 7 1 6 9]

print(x[y])
# [9 8 6 5 5 4 2 1 1 1]

搜索:

numpy.argmax()

  • numpy.argmax(a[, axis=None, out=None])Returns the indices of the maximum values along an axis.
  • numpy.argmin(a[, axis=None, out=None])Returns the indices of the minimum values along an axis.
import numpy as np

np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
# [[2.32 7.54 9.78 1.73 6.22]
#  [6.93 5.17 9.28 9.76 8.25]
#  [0.01 4.23 0.19 1.73 9.27]
#  [7.99 4.97 0.88 7.32 4.29]
#  [9.05 0.07 8.95 7.9  6.99]]

y = np.argmax(x)
print(y)  # 2

y = np.argmax(x, axis=0)
print(y)
# [4 0 0 1 2]

y = np.argmax(x, axis=1)
print(y)
# [2 3 4 0 0]

计数:

numpy.count_nonzero()返回数组中的非0元素个数

  • numpy.count_nonzero(a, axis=None) Counts the number of non-zero values in the array a.
import numpy as np

x = np.count_nonzero(np.eye(4))
print(x)  # 4

x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]])
print(x)  # 5

x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=0)
print(x)  # [1 1 1 1 1]

x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=1)
print(x)  # [2 3]

numpy 数组排序np.sort()、np.argsort()_大Py的博客-CSDN博客

对Numpy Matrix 轴Axis和 排序sort的理解_noetic_wxb的博客-CSDN博客

numpy中的sort()参数axis=-1是什么意思?_CDA答疑社区

阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

numpy.sort的使用方法及参数分析自学总结_weixin_44776220的博客-CSDN博客_numpy sort

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值