statistics模块的作用
statistics模块实现了很多常用的统计公式,允许使用Python的各种数值类型(int,float,Decimal和Fraction)来完成高效的计算。
1、求一个列表的平均数
from statistics import *
data = [1, 2, 2, 5, 10, 12]
print('{:0.2f}'.format(mean(data)))
statistics_mean.py
运行效果
5.33
2、显示一个元素在列表中出现次数最多的,如果数次相同则抛出异常statistics.StatisticsError
from statistics import *
data = [1, 2, 2, 5, 10, 12]
print('{}'.format(mode(data)))
statistics_mode.py
运行效果
2
3、计算中间最小,最大值,和中数的平均值
from statistics import *
data = [1, 2, 2, 5, 10, 12]
print('median : {:0.2f}'.format(median(data))) # 取两个中数的平均值
print('low : {:0.2f}'.format(median_low(data))) # 取中数最小值
print('high : {:0.2f}'.format(median_high(data))) # 取中数最大值
statistics_median.py
运行效果
median : 3.50
low : 2.00
high : 5.00
4、取中间值,每次减少0.5的示例
from statistics import *
data = [10, 20, 30, 40]
print('1: {:0.2f}'.format(median_grouped(data, interval=1)))
print('2: {:0.2f}'.format(median_grouped(data, interval=2)))
print('3: {:0.2f}'.format(median_grouped(data, interval=3)))
statistics_median_grouped.py
运行效果
1: 29.50
2: 29.00
3: 28.50
5、方差计算
from statistics import *
import subprocess
def get_line_lengths():
cmd = 'wc -l ../[a-z]*/*.py'
out = subprocess.check_output(
cmd, shell=True).decode('utf-8')
for line in out.splitlines():
parts = line.split()
if parts[1].strip().lower() == 'total':
break
nlines = int(parts[0].strip())
if not nlines:
continue # skip empty files
yield (nlines, parts[1].strip())
data = list(get_line_lengths())
lengths = [d[0] for d in data]
sample = lengths[::2]
print('Basic statistics:')
print(' count : {:3d}'.format(len(lengths)))
print(' min : {:6.2f}'.format(min(lengths)))
print(' max : {:6.2f}'.format(max(lengths)))
print(' mean : {:6.2f}'.format(mean(lengths)))
print('\nPopulation variance:') # 方差
print(' pstdev : {:6.2f}'.format(pstdev(lengths)))
print(' pvariance : {:6.2f}'.format(pvariance(lengths)))
print('\nEstimated variance for sample:') # 标准差
print(' count : {:3d}'.format(len(sample)))
print(' stdev : {:6.2f}'.format(stdev(sample)))
print(' variance : {:6.2f}'.format(variance(sample)))
statistics_variance.py
运行效果
[root@localhost ~]# python
python python2 python2.6 python3
[root@YZS_3WYY_WEB ~]# python3 p.py
Basic statistics:
count : 4
min : 7.00
max : 134.00
mean : 46.75
Population variance:
pstdev : 51.89
pvariance : 2692.69
Estimated variance for sample:
count : 2
stdev : 89.10
variance : 7938.00