当前位置:   article > 正文

python求数组最大值_如何在python中获取数组中多个最大值的值

python如何判断数组里的多个高点

您可以使用

np.unique获取计数和一组唯一元素,然后拉出计数等于max的元素:

import numpy as np

a = np.array([0, 0, 15, 17, 16, 17, 16, 12, 18, 18])

un, cnt = np.unique(a, return_counts=True)

print(un[cnt == cnt.max()])

[ 0 16 17 18]

un是唯一的元素,cnt是每个元素的频率/数量:

In [11]: a = np.array([0, 0, 15, 17, 16, 17, 16, 12, 18, 18])

In [12]: un, cnt = np.unique(a, return_counts=True)

In [13]: un, cnt

Out[13]: (array([ 0, 12, 15, 16, 17, 18]), array([2, 1, 1, 2, 2, 2]))

cnt == cnt.max()会给我们提供一个掩码来拉取等于max的元素:

In [14]: cnt == cnt.max()

Out[14]: array([ True, False, False, True, True, True], dtype=bool)

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

闽ICP备14008679号