Saturday, January 31, 2015

py2. Normal Distribution in Python

We can get samples based on the normal distribution from numpy.random.normal:

# ex2.py
from __future__ import division, print_function
from numpy.random import normal
import matplotlib.pyplot as plt
sig = normal(size = 10000)
num_pos = sig>=0
a = sum(num_pos)/len(num_pos)*100 
print('The percentage of positive numbers in sig is %.2f.' % a)
print('sig has %.5f mean.' % sig.mean())
print('sig has %.5f std away from 1' % abs(sig.std()-1))
plt.hist(sig,bins=45,color='gray')
plt.title('Normal Distribution')
plt.xlabel('x')
plt.ylabel('Frequency')
plt.show()
## The percentage of positive numbers in sig is 49.40.
## sig has -0.00742 mean.
## sig has 0.00592 std away from 1

Output

No comments:

Post a Comment