python - 编写一个在python中找到一维数组中出现频率最高的值的方法
问题描述
我在 python 中使用 Counter 和 Union 尝试了一些方法,但我没有得到解决方案。
当我运行代码时,我得到了TypeError: '>=' not supported between instances of 'numpy. ndarray' and 'int'
错误。
我试过了
import numpy as np
from typing import Union
from collections import Counter
def most_frequent_val(array:np.ndarray) -> Union[int,float,str]:
counts = Counter()
counts.most_common(array)
我的输入是一个数组
inps =np.array(["a","b","c","d","a","a","b","c","e","e","a"])
我也用过print(np.bincount(x).argmax())
,但是没有用。
解决方案
试试这个怎么样
import numpy as np
x = np.random.randint(0, 10, 40)
print("Original array:")
print(x)
print("Most frequent value in the above array:")
print(np.bincount(x).argmax())
推荐阅读
- python-3.x - 如何向 Elastic-beanstalk 环境添加大量 ENV 变量?与boto3?
- c++ - 如何动态转换未命名类型的内容
- azure - Azure 函数 - 超出 HTTP 触发请求长度
- jenkins - 詹金斯流水线成一个时间表
- dji-sdk - 在 iOS 中将 DJI 视频直播到 youtube
- javascript - 反应本机中具有变量的图像源
- css - CSS Transform Translate 在 Chrome 和 Firefox 中的定位不同?
- tsql - 分组后的SQL重新编号索引
- google-maps - 如何从应用程序本身启用位置跟踪权限?
- java - @SessionTarget,休眠会话为空