Python statistics.mode() 方法
示例
计算给定数据的众数(集中趋势)
# 导入 statistics 库
import statistics
# 计算众数
print(statistics.mode([1, 3, 3, 3, 5, 7, 7 9, 11]))
print(statistics.mode([1, 1, 3, -5, 7, -9, 11]))
print(statistics.mode(['red', 'green', 'blue', 'red']))
自己动手试一试 »
定义和用法
statistics.mode()
方法计算给定数值或名义数据集的众数(集中趋势)。
语法
statistics.mode(data)
参数值
参数 | 描述 |
---|---|
data | 必需。用于数据的值(可以是任何序列、列表或迭代器) |
注意: 如果 data 为空,则返回 StatisticsError。
技术详情
返回值 | 一个 float 或名义值,表示给定数据的众数 |
---|---|
Python 版本 | 3.4 |
更改日志 | 3.8:现在支持多模态数据集(将返回遇到的第一个众数) |