一、正态分布
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from scipy.stats import normaltest
data = np.loadtxt('http://jse.amstat.org/datasets/normtemp.dat.txt')
Temperature = []
for i in range(len(data)):
Temperature.append(float(str(data[i]).split()[0].split('[')[1]))
print(Temperature)
mu = np.mean(Temperature)
sigma = np.std(Temperature)
n, bins, patches = plt.hist(Temperature, normed=True, histtype='bar',bins='auto', color='#0504aa', rwidth=0.85)
y = mlab.normpdf(bins, mu, sigma)
plt.plot(bins,y,'r--', linewidth=1)
plt.title('Temperature distribution')
plt.xlabel('Temperature')
plt.ylabel('Probability')
plt.show()
print(normaltest(Temperature, axis=0))
二、常见数据分布
# coding = utf-8
import matplotlib.pyplot as plt
from scipy.stats import binom
import numpy as np
from pylab import mpl
from matplotlib.font_manager import _rebuild
_rebuild()
mpl.rcParams['font.sans-serif']=[u'SimHei']
mpl.rcParams['axes.unicode_minus']=False
# #二项分布
# n = 100
# p = 0.5
# mean, var, skew, kurt = binom.stats(n, p, moments= 'mvsk')
# x = np.arange(binom.ppf(0.01,n,p),binom.ppf(0.99,n,p))
# plt.plot(x, binom.pmf(x, n, p), 'o')
# plt.title(u"二项分布")
# plt.show()
# #泊松分布
# from scipy.stats import poisson
# mu = 10
# mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')
# x = np.arange(poisson.ppf(0.01,mu),poisson.ppf(0.99,mu))
# plt.plot(x, poisson.pmf(x, mu), 'o')
# plt.title(u'泊松分布')
# plt.show()
# #均匀分布
# from scipy.stats import uniform
# loc = 1
# scale = 1
# mean, var, skew, kurt = uniform.stats(loc,scale, moments = 'mvsk')
# x = np.linspace(uniform.ppf(0.01, loc, scale), uniform.ppf(0.99, loc, scale), 100)
# plt.plot(x, uniform.pdf(x, loc, scale), 'b-', label ='uniform')
# plt.title(u'均匀分布')
# plt.show()