โ† Back to Lessons
Open in Colab

Mean and Std with Python

Mean and Standard Deviationยถ

mean=ฮผ=1nโˆ‘i=1nxi\text{mean} = \mu = \frac{1}{n}\sum_{i = 1}^n x_i

standardย deviation=ฯƒ=1nโˆ‘i=1n(xiโˆ’ฮผ)2\text{standard deviation} = \sigma = \sqrt{\frac{1}{n}\sum_{i = 1}^n (x_i - \mu)^2}

Inย [ย ]:
#mean of a list
x = [3, 5, 7, 9, 11]
np.mean(x)
Out[ย ]:
7.0
Inย [ย ]:
#standard deviation of list
np.std(x)
Out[ย ]:
2.8284271247461903
Inย [ย ]:
#Binomial distribution with n = 20, p = 0.5
b = stats.binom(20, 0.5)
Inย [ย ]:
#mean
b.mean()
Out[ย ]:
10.0
Inย [ย ]:
#plot
plt.bar(range(21), b.pmf(range(21)))
Out[ย ]:
<BarContainer object of 21 artists>
No description has been provided for this image
Inย [ย ]:
#another example
#Binomial n = 50, p = 0.3
ex2 = stats.binom(50, 0.3)
ex3 = stats.binom(20, 0.3)
Inย [ย ]:
#mean
ex2.mean()
Out[ย ]:
15.0
Inย [ย ]:
ex3.mean()
Out[ย ]:
6.0
Inย [ย ]:
ex3.std()
Out[ย ]:
2.0493901531919194
Inย [ย ]:
#deviation
ex2.std()
Out[ย ]:
3.24037034920393
Inย [ย ]:
#plot
plt.bar(range(51), ex2.pmf(range(51)))
plt.bar(range(21), ex3.pmf(range(21)))
Out[ย ]:
<BarContainer object of 21 artists>
No description has been provided for this image