天天看點

【機器學習】用可視化的方式直覺了解Bayesian models

如果你感覺Bayesian models反直覺,不好了解!

本期分享兩個工具,​​

​利用可視化的直覺方式探索貝葉斯模型​

​。

1、Python ArviZ

【機器學習】用可視化的方式直覺了解Bayesian models

主要包含以下​

​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()      
【機器學習】用可視化的方式直覺了解Bayesian models

也可以基于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",
)      
【機器學習】用可視化的方式直覺了解Bayesian models

樣本診斷,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()      
【機器學習】用可視化的方式直覺了解Bayesian models

模型比較,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()      
【機器學習】用可視化的方式直覺了解Bayesian models

模型檢驗,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()      
【機器學習】用可視化的方式直覺了解Bayesian models

2、R bayesplot

主要利用可視化探索馬爾科夫鍊蒙特卡洛方法​

​MCMC拟合後的Bayesian models​

【機器學習】用可視化的方式直覺了解Bayesian models

功能都在下圖中了,

【機器學習】用可視化的方式直覺了解Bayesian models