2015-06-18 146 views
0

我写了一个python代码来计算数字列表的标准偏差。我检查了我对Excel的答案,它似乎是关闭的。我不确定我是否错过了一个步骤,或者我应该担心,但是如果有人有时间查看代码并查看他们是否发现错误,请告诉我。谢谢。Python标准偏差检查

city_population = [2123,1284,7031,30788,147,2217,10000] 

mean = sum(city_population,0.0)/len(city_population) 

def stdev(city_population): 
    length = len(city_population) 
    total_sum = 0 
    for i in range(length): 
     total_sum += pow((city_population[i]-mean),2) 
     result = (total_sum/(length-1)) 
     return sqrt(result) 
stan_dev = stdev(city_population) 
print "The standard deviation is",(stan_dev) 

输出: The standard deviation is 9443.71609738

的Excel:9986.83890663

+0

你使用哪个excel函数? –

+0

人口std dev –

回答

3

你的问题多半是由于你的循环内的代码来计算的总和。在这个循环中,你也计算每次迭代的结果,然后从函数返回。这意味着只有一个迭代循环运行。

运行你的代码时,我得到的结果是2258.72114877,它是从第一个值开始计算的。由代码改变为以下,正确的样本的标准偏差产生:

city_population = [2123,1284,7031,30788,147,2217,10000] 

mean = sum(city_population,0.0)/len(city_population) 

def stdev(city_population): 
    length = len(city_population) 
    total_sum = 0 
    for i in range(length): 
     total_sum += pow((city_population[i]-mean),2) 
    # total_sum is 698158659.4285713 
    result = (total_sum/(length-1)) 
    # result is 116359776.57142855 
    # sqrt(result) is 10787.01889177119 
    return sqrt(result) 

stan_dev = stdev(city_population) 
print "The standard deviation is",(stan_dev) 

之所以这样新的结果是从Excel的值不同的是,Excel正在返回的总体标准偏差。如果有用于从头开始编写的代码,我推荐使用numpy的,以避免在这里重新发明轮子没有要求

https://statistics.laerd.com/statistical-guides/measures-of-spread-standard-deviation.php

:作为一个快速参考,下面的页面可能对你有用http://www.numpy.org/ 。有了这个,你的代码就变成了:

import numpy 
city_population = [2123,1284,7031,30788,147,2217,10000] 
numpy.std(city_population, ddof=1) 

一对夫妇的其他提示:为了避免将来出现混乱和潜在的问题,尽量避免命名函数的参数相同的全局变量。并且尽量不要依赖先前在函数中设置的变量(就像你在这里用“mean”所做的那样)。

+0

谢谢你的指导。我将再次评估我的代码并进行适当的更改。 –

1

问题是你在回路中有回报!

下面应该工作:

def stdev(city_population): 
    length = len(city_population) 
    total_sum = 0 
    for i in range(length): 
     total_sum += pow((city_population[i]-mean),2) 
    result = (total_sum/(length)) 
    return sqrt(result) 

,而不是对于标准差,则需要通过长不长-1(如果你有一个样本,而不是整个人口,这将是)来划分。