Toma Tomov
Toma Tomov

Reputation: 1654

Which is the right standard deviation formula Python

I am confused. I found few formulas for finding the SD (standard deviation). This is the NumPy library std method:

>>> nums = np.array([65, 36, 52, 91, 63, 79])
>>> np.std(nums)
17.716909687891082

But I found another formula here:Standard deviation

By this formula with the same dataset my result is 323,1666666666667‬. Now which one is right? Or they are used for two different things?

EDIT: Seems I forgot about the square root

Upvotes: 0

Views: 278

Answers (2)

balderman
balderman

Reputation: 23815

Core python. see pstdev

import statistics
print(statistics.pstdev([65, 36, 52, 91, 63, 79]))

output

17.716909687891082

Upvotes: 0

hiro protagonist
hiro protagonist

Reputation: 46859

numpy is correct, of course. here the plain python version:

from math import sqrt

data = [65, 36, 52, 91, 63, 79]

mean = sum(data) / len(data)
std = sqrt(sum((d - mean) ** 2 for d in data) / len(data))
print(std)   # 17.716909687891082

Upvotes: 4

Related Questions