<分区>
假设你有:
arr = np.array([1,2,1,3,3,4])
是否有返回最频繁元素的内置函数?
<分区>
假设你有:
arr = np.array([1,2,1,3,3,4])
是否有返回最频繁元素的内置函数?
最佳答案
是的,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)]
对于 numpy,您可能希望从 histogram() function 开始或 bincount() function .
使用 scipy,您可以使用 mstats.mode 搜索模态元素.
关于python - numpy/python 中有 "freq"函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15734756/