2015-06-18 148 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(如果你有一個樣本,而不是整個人口,這將是)來劃分。