python标准库math中计算平方根的函数是_Python标准库——数学运算

方差

统计使用两个值描述一个值集相对于均值的分散度。方差(variance)是各个值与均值之差平方的平均,标准偏差或标准差(standard deviation)是方差的平方根(这很有用,因为取平方根可以使标准差与输入数据有相同的单位)。如果方差或标准差的值很大,这说明一个数据集是分散的,而如果这个值很小,则说明数据在靠近均值聚集。

新建statistics_variance.py文件。

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

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)))

以上代码输出结果为:

Basic statistics:

count : 959

min : 4.00

max : 228.00

mean : 28.62

Population variance:

pstdev : 18.52

pvariance : 342.95

Estimated variance for sample:

count : 480

stdev : 21.09

variance : 444.61

以上代码,Python包括两组函数来计算方差和标准差,具体取决于数据集是表示总体还是表示总体中的一个样本。这个例子首先使用WC统计所有示例程序输入文件中的行数。然后使用pvariance()和pstdev()计算总体的方差和标准差。最后,它使用variance()和stddev()计算一个子集的样本方差和标准差,这个子集是由每隔一个文件的长度创建的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值