Code for calculating standard deviationin in python
Answers
Answered by
0
Answer:
Suppose your data is in the list variable X.
To avoid problems, you should make sure the values are 'float' type, not 'int'.
>>> X = [ float(x) for x in X ]
The number of values is
>>> n = len(X)
The sum of the values is
>>> sum(X)
The mean is
>>> mean = sum(X) / n
The sum of the squares of the values is
>>> ssq = sum(x**2 for x in X)
The variance is the mean of these minus the square of the mean:
>>> var = ssq / n - mean**2
The standard deviation is the square root of the variance
>>> sd = pow(var, 0.5)
All in one step:
>>> sd = pow(sum(x**2 for x in X)/len(X) - (sum(X)/len(X))**2 , 0.5)
Similar questions