备注
Go to the end 下载完整的示例代码。或者通过浏览器中的MysterLite或Binder运行此示例
利用Bayesian Ridge回归进行曲线匹配#
计算正弦曲线的贝叶斯岭回归。
看到 Bayesian Ridge Regression 有关回归量的更多信息。
一般来说,当通过Bayesian Ridge回归用多项曲线进行曲线匹配时,规则化参数(Alpha、Lamb)的初始值的选择可能很重要。这是因为正规化参数是通过取决于初始值的迭代过程确定的。
在本例中,通过使用不同初始值对的函数来逼近该曲线。
当从默认值(Alpha_initi = 1.90,ambda_initi = 1.)开始时,所得曲线的偏差很大,方差很小。因此,ambda_init应该相对较小(1.e-3),以减少偏差。
此外,通过评估这些模型的log边际似然(L),我们可以确定哪一个更好。可以得出结论,L较大的车型可能性更大。
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause
生成带有噪音的曲线数据#
import numpy as np
def func(x):
return np.sin(2 * np.pi * x)
size = 25
rng = np.random.RandomState(1234)
x_train = rng.uniform(0.0, 1.0, size)
y_train = func(x_train) + rng.normal(scale=0.1, size=size)
x_test = np.linspace(0.0, 1.0, 100)
通过三次多项方程进行匹配#
from sklearn.linear_model import BayesianRidge
n_order = 3
X_train = np.vander(x_train, n_order + 1, increasing=True)
X_test = np.vander(x_test, n_order + 1, increasing=True)
reg = BayesianRidge(tol=1e-6, fit_intercept=False, compute_score=True)
用log边际似然(L)绘制真实曲线和预测曲线#
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2, figsize=(8, 4))
for i, ax in enumerate(axes):
# Bayesian ridge regression with different initial value pairs
if i == 0:
init = [1 / np.var(y_train), 1.0] # Default values
elif i == 1:
init = [1.0, 1e-3]
reg.set_params(alpha_init=init[0], lambda_init=init[1])
reg.fit(X_train, y_train)
ymean, ystd = reg.predict(X_test, return_std=True)
ax.plot(x_test, func(x_test), color="blue", label="sin($2\\pi x$)")
ax.scatter(x_train, y_train, s=50, alpha=0.5, label="observation")
ax.plot(x_test, ymean, color="red", label="predict mean")
ax.fill_between(
x_test, ymean - ystd, ymean + ystd, color="pink", alpha=0.5, label="predict std"
)
ax.set_ylim(-1.3, 1.3)
ax.legend()
title = "$\\alpha$_init$={:.2f},\\ \\lambda$_init$={}$".format(init[0], init[1])
if i == 0:
title += " (Default)"
ax.set_title(title, fontsize=12)
text = "$\\alpha={:.1f}$\n$\\lambda={:.3f}$\n$L={:.1f}$".format(
reg.alpha_, reg.lambda_, reg.scores_[-1]
)
ax.text(0.05, -1.0, text, fontsize=12)
plt.tight_layout()
plt.show()

Total running time of the script: (0分0.201秒)
相关实例
Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>
_