返回numpy中出现最多的数字
可以使用numpy中的bincount函数,统计数组中每个元素出现的次数,然后找到出现次数最多的数字。
示例代码:
import numpy as np
a = np.array([1, 2, 3, 2, 1, 2, 3, 3, 4, 5, 4, 4])
counts = np.bincount(a)
max_count = np.max(counts)
most_frequent = np.where(counts == max_count)[0][0]
print("出现最多的数字是:", most_frequent)
输出结果:
出现最多的数字是: 2
原文地址: https://www.cveoy.top/t/topic/ckQP 著作权归作者所有。请勿转载和采集!