在資料處理中常用的5種概率分佈的python實現
阿新 • • 發佈:2019-01-09
1.二項分佈
from scipy.stats import binom, norm, beta, expon
import numpy as np
import matplotlib.pyplot as plt
binom_sim = data = binom.rvs(n=10, p=0.3, size=10000)
print('Mean: %g' % np.mean(binom_sim))
print('SD: %g' % np.std(binom_sim, ddof=1))
plt.hist(binom_sim, bins=10, normed=True)
plt.xlabel(('x'))
plt.ylabel('density' )
plt.show()
2.正態分佈
# 正態分佈 mu = 0 sigma = 1 x = np.arange(-5, 5, 0.1) y = norm.pdf(x, 0, 1) plt.plot(x, y) plt.xlabel('x') plt.ylabel('density') plt.show()
3.泊松分佈
x = np.random.poisson(lam=5, size=10000) pillar = 15 a = plt.hist(x, bins=pillar, normed=True, range=[0, pillar], color='g', alpha=0.5) plt.plot(a[1][0:pillar], a[0], 'r') plt.grid() plt.show()
3.beta分佈
a = 0.5 b = 0.5 x = np.arange(0.01, 1, 0.01) y = beta.pdf(x, a, b) plt.plot(x, y) plt.title('Beta: a=%.1f,b=%.1f' % (a, b)) plt.xlabel('x') plt.ylabel('density') plt.show()
5.指數分佈
lam = 0.5 x = np.arange(0, 15, 0.1) y = expon.pdf(x, lam) plt.plot(x, y) plt.title('Exponential: lam=%.2f'% lam) plt.xlabel('x') plt.ylabel('density') plt.show()