假设你有:
arr = np.array([1,2,1,3,3,4])
Run Code Online (Sandbox Code Playgroud)
是否有内置函数返回最常见的元素?
Ray*_*ger 13
是的,Python的collections.Counter直接支持查找最常见的元素:
>>> from collections import Counter
>>> Counter('abracadbra').most_common(2)
[('a', 4), ('r', 2)]
>>> Counter([1,2,1,3,3,4]).most_common(2)
[(1, 2), (3, 2)]
Run Code Online (Sandbox Code Playgroud)
使用numpy,您可能希望从histogram()函数或bincount()函数开始.
随着SciPy的,你可以搜索与模态元素mstats.mode.
| 归档时间: |
|
| 查看次数: |
619 次 |
| 最近记录: |