excel - Python Standard Deviation Check -
i have written python code calculate standard deviation of list of numbers. checked answer on excel , appears off. i'm not sure if missed step or if should concerned, if has moment review code , see if notice error, please let me know. thank you.
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 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)
output: the standard deviation 9443.71609738
excel: 9986.83890663
your problem due code within loop calculating total sum. in loop, you're calculating result @ each iteration, , returning function. means 1 iteration of loop runs.
when running code, result 2258.72114877, calculated first value only. changing code following, correct sample standard deviation produced:
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 in range(length): total_sum += pow((city_population[i]-mean),2) # total_sum 698158659.4285713 result = (total_sum/(length-1)) # result 116359776.57142855 # sqrt(result) 10787.01889177119 return sqrt(result) stan_dev = stdev(city_population) print "the standard deviation is",(stan_dev)
the reason new result different value excel excel returning population standard deviation. quick reference, following page may useful you:
https://statistics.laerd.com/statistical-guides/measures-of-spread-standard-deviation.php
if there's no requirement code written scratch, i'd recommend using numpy avoid reinventing wheel here: http://www.numpy.org/ . this, code becomes:
import numpy city_population = [2123,1284,7031,30788,147,2217,10000] numpy.std(city_population, ddof=1)
a couple of additional tips: avoid future confusion , potential issues, try avoid naming function parameters same global variables. , try not rely on set variables within function (as "mean" here).
Comments
Post a Comment