如何计算python输出的均值、众数、方差、标准差等?
How to calculate mean, mode, variance, standard deviation etc. of output in python?
我有一个基于概率的简单游戏,每天我们抛硬币,如果正面朝上,我们就赢,我们得到 20 美元,如果我们抛硬币,反面朝上,我们就输 19 美元,在月底(28 天),我们会看到损失或赚了多少。
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
elif x == 1: #if we get tails
return -19 #we lose
for a in range(1, 28): #for each day of the month
print(coin_tossing_game())
这returns输出20
20
-19
-19
-19
-19
-19
20
-19
20
-19
20
-19
20
20
-19
-19
20
20
-19
-19
-19
20
20
20
-19
-19
-19
20
20
这个输出正是我所期望的。我想找到输出和其他描述性统计数据的总和,如均值、众数、中位数、标准差、置信区间等。我不得不将这些数据复制并粘贴到 excel 以进行此数据分析。我希望有一种方法可以在 python 中快速轻松地做到这一点。
使用 scipy 统计模块并使用 modal
作为众数,使用 scipy.stats.mstats.median_cihs
作为中位数,使用 trim_mean
作为均值。您还可以使用统计模块并使用 mean()
、median()
和 mode()
函数。
是的,有:安装 numpy 和 scipy。使用函数 numpy.mean
、numpy.std
、numpy.median
、scipy.stats.mode
.
Scipy 还包含 scipy.stats
模块,它提供各种常见的重要性测试。
你在问如何。最直接可用的是以 statistics 库的形式构建到 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
elif x == 1: #if we get tails
return -19 #we lose
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]
我有一个基于概率的简单游戏,每天我们抛硬币,如果正面朝上,我们就赢,我们得到 20 美元,如果我们抛硬币,反面朝上,我们就输 19 美元,在月底(28 天),我们会看到损失或赚了多少。
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
elif x == 1: #if we get tails
return -19 #we lose
for a in range(1, 28): #for each day of the month
print(coin_tossing_game())
这returns输出20 20 -19 -19 -19 -19 -19 20 -19 20 -19 20 -19 20 20 -19 -19 20 20 -19 -19 -19 20 20 20 -19 -19 -19 20 20
这个输出正是我所期望的。我想找到输出和其他描述性统计数据的总和,如均值、众数、中位数、标准差、置信区间等。我不得不将这些数据复制并粘贴到 excel 以进行此数据分析。我希望有一种方法可以在 python 中快速轻松地做到这一点。
使用 scipy 统计模块并使用 modal
作为众数,使用 scipy.stats.mstats.median_cihs
作为中位数,使用 trim_mean
作为均值。您还可以使用统计模块并使用 mean()
、median()
和 mode()
函数。
是的,有:安装 numpy 和 scipy。使用函数 numpy.mean
、numpy.std
、numpy.median
、scipy.stats.mode
.
Scipy 还包含 scipy.stats
模块,它提供各种常见的重要性测试。
你在问如何。最直接可用的是以 statistics 库的形式构建到 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
elif x == 1: #if we get tails
return -19 #we lose
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]