python计算均值标准差_如何用python计算输出的平均值、模式、方差、标准差等?...

你在问如何。最直接可用的是以统计信息库的形式构建到Python中。但是,你似乎想知道怎么做。下面的代码展示了基本的,我已经有50年没有必要这么做了。在

首先,修改您的代码,使其捕获向量中的样本。在我的代码中,它被称为sample。在

代码的第一部分只是练习Python库。没有汗。在

代码的第二部分说明了如何累积样本中的值的和,以及它们与平均值的偏差的平方和。我交给你们来解决如何计算样本方差,样本标准差和在这些统计数据的通常假设下的置信区间。对样本进行排序和重命名后,我计算出最大值和最小值(对于某些分布的估计很有用)。最后,我从分类的样本中计算中位数。我把中位数的计算留给你。在import random

def coin_tossing_game():

random_numbers = [random.randint(0, 1) for x in range(500)] #generate 500 random numbers

for x in random_numbers:

if x == 0: #if we get heads

return 20 #we win $20

elif x == 1: #if we get tails

return -19 #we lose $19

sample = []

for a in range(1, 28): #for each day of the month

#~ print(coin_tossing_game())

sample.append(coin_tossing_game())

## the easy way

import statistics

print (statistics.mean(sample))

print (statistics.median(sample))

print (statistics.mode(sample))

print (statistics.stdev(sample))

print (statistics.variance(sample))

## the hard way

sample.sort()

orderedSample = sample

N = len(sample)

minSample = orderedSample[0]

maxSample = orderedSample[-1]

sumX = 0

for x in sample:

sumX += x

mean = sumX / N

sumDeviates2 = 0

for x in sample:

sumDeviates2 += ( x-mean )**2

k = N//2

if N%2==0:

mode = 0.5* (orderedSample[k]+orderedSample[k-1])

else:

mode = orderedSample[k]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值