如果你感觉Bayesian models反直觉,不好理解!
本期分享两个工具,利用可视化的直观方式探索贝叶斯模型
。
1、Python ArviZ
主要包含以下4方面功能:
后验分析,posterior analysis
数据存储,data storage
样本诊断,sample diagnostics
模型比较,model comparison
模型检验,Model Checking
以下举例简单介绍,
后验分析,posterior analysis
可以基于matplotlib,
import matplotlib.pyplot as plt
# arviz底层可选matplotlib或者bokeh,此处选matplotlib
import arviz as az
plt.figure(dpi=150)
az.style.use("ggplot") #类似matplotlib中的plt.style.use
#数据准备
data = az.load_arviz_data("centered_eight")
coords = {"school": ["Choate"]}
# plot_posterior
axes = az.plot_posterior(
data,
var_names=["mu", "theta"],
coords=coords,
rope=(-1, 1),
figsize=(11.5, 5),
)
fig = axes.flatten()[0].get_figure()
fig.suptitle("Centered Eight: mu and theta for Choate")
plt.show()
也可以基于Bokeh:Plotting with Bokeh
import arviz as az
data = az.load_arviz_data("centered_eight")
coords = {"school": ["Choate", "Mt. Hermon", "Deerfield"]}
#同样使用plot_posterior
ax = az.plot_posterior(
data,
var_names=["mu", "theta"],
combine_dims={"school"},
coords=coords,
backend="bokeh",
)
样本诊断,sample diagnostics
import matplotlib.pyplot as plt
import arviz as az
data = az.load_arviz_data("centered_eight")
coords = {"school": ["Choate", "Deerfield"]}
az.plot_pair(
data,
var_names=["theta", "mu", "tau"],
coords=coords,
divergences=True,
textsize=22,
)
plt.show()
模型比较,model comparison
import arviz as az
centered_data = az.load_arviz_data("centered_eight")
non_centered_data = az.load_arviz_data("non_centered_eight")
axes = az.plot_density(
[centered_data, non_centered_data],
data_labels=["Centered", "Non-Centered"],
var_names=["theta"],
shade=0.2,
)
fig = axes.flatten()[0].get_figure()
fig.suptitle("94% High Density Intervals for Theta")
plt.show()
模型检验,Model Checking
import matplotlib.pyplot as plt
import arviz as az
data = az.load_arviz_data("non_centered_eight")
az.plot_ppc(data, alpha=0.05, kind="cumulative", textsize=14)
plt.show()
2、R bayesplot
主要利用可视化探索马尔科夫链蒙特卡洛方法MCMC拟合后的Bayesian models
功能都在下图中了,
-推荐阅读-
往期精彩回顾
适合初学者入门人工智能的路线及资料下载(图文+视频)机器学习入门系列下载机器学习及深度学习笔记等资料打印《统计学习方法》的代码复现专辑机器学习交流qq群955171419,加入微信群请扫码