当前位置:   article > 正文

numpy 排序,搜索和计数_numpy排序输出索引和数值

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用法相同

  1. import numpy as np
  2. np.random.seed(20200612)
  3. x = np.random.rand(5, 5) * 10
  4. x = np.around(x, 2)
  5. print(x)
  6. # [[2.32 7.54 9.78 1.73 6.22]
  7. # [6.93 5.17 9.28 9.76 8.25]
  8. # [0.01 4.23 0.19 1.73 9.27]
  9. # [7.99 4.97 0.88 7.32 4.29]
  10. # [9.05 0.07 8.95 7.9 6.99]]
  11. y = np.sort(x) #默认为负1
  12. print(y)
  13. # [[1.73 2.32 6.22 7.54 9.78]
  14. # [5.17 6.93 8.25 9.28 9.76]
  15. # [0.01 0.19 1.73 4.23 9.27]
  16. # [0.88 4.29 4.97 7.32 7.99]
  17. # [0.07 6.99 7.9 8.95 9.05]]
  18. y = np.sort(x, axis=0)
  19. print(y)
  20. # [[0.01 0.07 0.19 1.73 4.29]
  21. # [2.32 4.23 0.88 1.73 6.22]
  22. # [6.93 4.97 8.95 7.32 6.99]
  23. # [7.99 5.17 9.28 7.9 8.25]
  24. # [9.05 7.54 9.78 9.76 9.27]]
  25. y = np.sort(x, axis=1)
  26. print(y)
  27. # [[1.73 2.32 6.22 7.54 9.78]
  28. # [5.17 6.93 8.25 9.28 9.76]
  29. # [0.01 0.19 1.73 4.23 9.27]
  30. # [0.88 4.29 4.97 7.32 7.99]
  31. # [0.07 6.99 7.9 8.95 9.05]]、
  32. >>> y = np.sort(x, None) #全部拿出来进行排序
  33. >>> print(y)
  34. [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
  35. 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() 函数返回的是数组值从小到大的索引值。对数组沿给定轴执行间接排序,并使用指定排序类型返回数据的索引数组。这个索引数组用于构造排序后的数组。

  1. import numpy as np
  2. np.random.seed(20200612)
  3. x = np.random.randint(0, 10, 10)
  4. print(x)
  5. # [6 1 8 5 5 4 1 2 9 1]
  6. y = np.argsort(x) #升序排列索引
  7. print(y)
  8. # [1 6 9 7 5 3 4 0 2 8]
  9. print(x[y]) #打印y索引所对应的X的值
  10. # [1 1 1 2 4 5 5 6 8 9]
  11. y = np.argsort(-x) #降序排列索引
  12. print(y)
  13. # [8 2 0 3 4 5 7 1 6 9]
  14. print(x[y])
  15. # [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.
  1. import numpy as np
  2. np.random.seed(20200612)
  3. x = np.random.rand(5, 5) * 10
  4. x = np.around(x, 2)
  5. print(x)
  6. # [[2.32 7.54 9.78 1.73 6.22]
  7. # [6.93 5.17 9.28 9.76 8.25]
  8. # [0.01 4.23 0.19 1.73 9.27]
  9. # [7.99 4.97 0.88 7.32 4.29]
  10. # [9.05 0.07 8.95 7.9 6.99]]
  11. y = np.argmax(x)
  12. print(y) # 2
  13. y = np.argmax(x, axis=0)
  14. print(y)
  15. # [4 0 0 1 2]
  16. y = np.argmax(x, axis=1)
  17. print(y)
  18. # [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.
  1. import numpy as np
  2. x = np.count_nonzero(np.eye(4))
  3. print(x) # 4
  4. x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]])
  5. print(x) # 5
  6. x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=0)
  7. print(x) # [1 1 1 1 1]
  8. x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=1)
  9. 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/638735
推荐阅读
相关标签
  

闽ICP备14008679号