Finding Standard Deviation of sample data

Duplicate

import collections

sample = [100, 200, 300, 200, 400, 300, 400, 600, 300, 700, 200, 800]
n = (sum(collections.Counter(sample).values()))
print('Number of samples =', n)
s = math.fsum(sample)

print('Total of sample =', s)
mean = s / n
print('The mean  =', mean)
d1 = (sample[0] - mean) ** 2
d2 = (sample[1] - mean) ** 2
d3 = (sample[2] - mean) ** 2
d4 = (sample[3] - mean) ** 2
d5 = (sample[4] - mean) ** 2
d6 = (sample[5] - mean) ** 2
d7 = (sample[6] - mean) ** 2
d8 = (sample[7] - mean) ** 2
d9 = (sample[8] - mean) ** 2
d10 = (sample[9] - mean) ** 2
d11 = (sample[10] - mean) ** 2
d12 = (sample[11] - mean) ** 2
q = (d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12)
Tot = math.fsum(q)
print('Sum of varience =', Tot)
sd = math.sqrt(Tot / (n - 1))
print('Standard Deviation = ', sd)```
# This can be done in a more easy way by importing #statistic module.(statistic.stdev). But if you want to learn #coding and use your brain , use this conventional method.
# Any one ,using   more easy method (with out using #statistic module) are always welcome.

How about using a for-loop, turning q into an array and just .append() directly?

I tried your suggestion. But it is not working. Thanks for your suggestion. Looking forward for more suggestions.

What do you mean it’s not working?
I could from the top of my head write a for-loop for that problem.

Also here is a quick tip: The length of an array is len(sample) and the sum is sum(sample).
The way you calculate n is… creative but just so extremly complicated.

Got it. I’ll work on it and post the result.Thank you

Hai jagaya, Now it is working ! Earlier it was a simple typo error. Look forward to you for further guidance.
Thank you.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.