机器学习中统计概率分布大全(Python代码)
来源:https://awstip.com/statistical-probability-distributions
转自:我得学诚
随机变量(Random Variable) 密度函数(Density Functions) 伯努利分布(Bernoulli Distribution) 二项式分布(Binomial Distribution) 均匀分布(Uniform Distribution) 泊松分布(Poisson Distribution) 正态分布(Normal Distribution) 长尾分布(Long-Tailed Distribution) 学生 t 检验分布(Student’s t-test Distribution) 对数正态分布(Lognormal Distribution) 指数分布(Exponential Distribution) 威布尔分布(Weibull Distribution) 伽马分布(Gamma Distribution) 卡方分布(Chi-square Distribution) 中心极限定理(Central Limit Theorem)
1. 随机变量
专属福利 👉点击领取:最全Python资料合集
2. 密度函数
PMF。来源:https://en.wikipedia.org/wiki/Probability_mass_function
PDF。来源:https://byjus.com/maths/probability-density-function/
CDF(指数分布的累积分布函数)。来源:https://en.wikipedia.org/wiki/Cumulative_distribution_function
3. 离散分布
import seaborn as snsfrom scipy.stats import bernoulli# 单一观察值# 生成数据 (1000 points, possible outs: 1 or 0, probability: 50% for each)data = bernoulli.rvs(size=1000,p=0.5)# 绘制图形ax = sns.distplot(data_bern,kde=False,hist_kws={"linewidth": 10,'alpha':1})ax.set(xlabel='Bernouli', ylabel='freq')
二项式分布
import matplotlib.pyplot as pltfrom scipy.stats import binomn = 20# 实验次数p = 0.5# 成功的概率r = list(range(n + 1))# the number of success# pmf值pmf_list = [binom.pmf(r_i, n, p) for r_i in r ]# 绘图plt.bar(r, pmf_list)plt.show()
带有偏差硬币的二项式分布
均匀分布
data = np.random.uniform(1, 6, 6000)
掷 6000 次。
Poisson 分布
import matplotlib.pyplot as pltfrom scipy.statsimport poissonr = range(0,11)# 呼叫次数lambda_val = 4# 均值# 概率值data = poisson.pmf(r, lambda_val)# 绘图fig, ax = plt.subplots(1, 1, figsize=(8, 6))ax.plot(r, data, 'bo', ms=8, label='poisson')plt.ylabel("Probability", fontsize="12")plt.xlabel("# Calls", fontsize="12")plt.title("Poisson Distribution", fontsize="16")ax.vlines(r, 0, data, colors='r', lw=5, alpha=0.5)
4. 连续分布
正态分布
import scipymean = 0standard_deviation = 5x_values = np. arange(-30, 30, 0.1)y_values = scipy.stats.norm(mean, standard_deviation)plt.plot(x_values, y_values. pdf(x_values))
QQ 图
import numpy as npimport statsmodels.api as smpoints = np.random.normal(0, 1, 1000)fig = sm.qqplot(points, line ='45')plt.show()
长尾分布
import matplotlib.pyplot as pltfrom scipy.stats import skewnormdef generate_skew_data(n: int, max_val: int, skewness: int):# Skewnorm functionrandom = skewnorm.rvs(a = skewness,loc=max_val, size=n)plt.hist(random,30,density=True, color = 'red', alpha=0.1)plt.show()generate_skew_data(1000, 100, -5) # negative (-5)-> 左偏分布
generate_skew_data(1000, 100, 5) # positive (5)-> 右偏分布
t 分布和 z 分布。来源:https://www.geeksforgeeks.org/students-t-distribution-in-statistics/
对数正态分布
import numpy as npimport matplotlib.pyplot as pltfrom scipy import statsX = np.linspace(0, 6, 1500)std = 1mean = 0lognorm_distribution = stats.lognorm([std], loc=mean)lognorm_distribution_pdf = lognorm_distribution.pdf(X)fig, ax = plt.subplots(figsize=(8, 5))plt.plot(X, lognorm_distribution_pdf, label="μ=0, σ=1")ax.set_xticks(np.arange(min(X), max(X)))plt.title("Lognormal Distribution")plt.legend()plt.show()
指数分布
from scipy.stats import exponimport matplotlib.pyplot as pltx = expon.rvs(scale=2, size=10000) # 2 calls# 绘图plt.hist(x, density=True, edgecolor='black')
x 轴表示时间间隔的百分比。
韦伯分布
import matplotlib.pyplot as pltx = np.arange(1,100.)/50.def weib(x,n,a):return (a / n) * (x / n)**(a - 1) * np.exp(-(x / n)**a)count, bins, ignored = plt.hist(np.random.weibull(5.,1000))x = np.arange(1,100.)/50.scale = count.max()/weib(x, 1., 5.).max()plt.plot(x, weib(x, 1., 5.)*scale)plt.show()
Gamma 分布
import numpy as npimport scipy.stats as statsimport matplotlib.pyplot as plt#Gamma distributionsx = np.linspace(0, 60, 1000)y1 = stats.gamma.pdf(x, a=5, scale=3)y2 = stats.gamma.pdf(x, a=2, scale=5)y3 = stats.gamma.pdf(x, a=4, scale=2)# plotsplt.plot(x, y1, label='shape=5, scale=3')plt.plot(x, y2, label='shape=2, scale=5')plt.plot(x, y3, label='shape=4, scale=2')#add legendplt.legend()#displayplotplt.show()
Gamma 分布。X 轴表示随机变量 X 可能取到的潜在值,Y 轴表示分布的概率密度函数(PDF)值。
5.中心极限定理
中心极限定理。来源:https://en.wikipedia.org/wiki/Central_limit_theore