scikit-learn 学习笔记-- Generalized Linear Models (三)
生活随笔
收集整理的這篇文章主要介紹了
scikit-learn 学习笔记-- Generalized Linear Models (三)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Bayesian regression
前面介紹的線性模型都是從最小二乘,均方誤差的角度去建立的,從最簡單的最小二乘到帶正則項的 lasso,ridge 等。而 Bayesian regression 是從 Bayesian 概率模型的角度出發(fā)的,雖然最后也會轉(zhuǎn)換成一個能量函數(shù)的形式。
從前面的線性模型中,我們都假設如下的關系:
上面這個關系式其實是直接從值的角度來考慮,其實我們也可以假設如下的關系:
y=wx+?y=wx+?
這個 ?? 表示一種誤差,或者噪聲,如果估計的值非常準確,那么 ?=0?=0, 否則,這將是一個隨機數(shù)。
如果我們有一組訓練樣本,那么每個觀察值 yy 都會有個對應的 ??, 而且我們假設 ?? 是滿足獨立同分布的。那么我們可以用概率的形式表示為:
p(y|w,x,α)=N(y|wx,α)p(y|w,x,α)=N(y|wx,α)
對于一組訓練集,我們可以表示為:
p(y|X,w)=∏i=1NN(yi|wxi,α)p(y|X,w)=∏i=1NN(yi|wxi,α)
最后,利用最大似然估計,可以將上面的表達式轉(zhuǎn)化為一個能量最小的形式。上面是從最大似然估計的角度去求系數(shù)。
下面我們考慮從最大后驗概率的角度,
p(w|y)=p(y|w)p(w|α)p(α)p(w|y)=p(y|w)p(w|α)p(α)
p(w|α)=N(w|0,α?1I)p(w|α)=N(w|0,α?1I)
p(α)p(α) 本身是服從 gamma 分布的。
sklearn 上也給出了一個例子:
import numpy as np import matplotlib.pyplot as plt from scipy import statsfrom sklearn.linear_model import BayesianRidge, LinearRegression# ############################################################################# # Generating simulated data with Gaussian weights np.random.seed(0) n_samples, n_features = 100, 100 X = np.random.randn(n_samples, n_features) # Create Gaussian data # Create weights with a precision lambda_ of 4. lambda_ = 4. w = np.zeros(n_features) # Only keep 10 weights of interest relevant_features = np.random.randint(0, n_features, 10) for i in relevant_features:w[i] = stats.norm.rvs(loc=0, scale=1. / np.sqrt(lambda_)) # Create noise with a precision alpha of 50. alpha_ = 50. noise = stats.norm.rvs(loc=0, scale=1. / np.sqrt(alpha_), size=n_samples) # Create the target y = np.dot(X, w) + noise# ############################################################################# # Fit the Bayesian Ridge Regression and an OLS for comparison clf = BayesianRidge(compute_score=True) clf.fit(X, y)ols = LinearRegression() ols.fit(X, y)# ############################################################################# # Plot true weights, estimated weights, histogram of the weights, and # predictions with standard deviations lw = 2 plt.figure(figsize=(6, 5)) plt.title("Weights of the model") plt.plot(clf.coef_, color='lightgreen', linewidth=lw,label="Bayesian Ridge estimate") plt.plot(w, color='gold', linewidth=lw, label="Ground truth") plt.plot(ols.coef_, color='navy', linestyle='--', label="OLS estimate") plt.xlabel("Features") plt.ylabel("Values of the weights") plt.legend(loc="best", prop=dict(size=12))plt.figure(figsize=(6, 5)) plt.title("Histogram of the weights") plt.hist(clf.coef_, bins=n_features, color='gold', log=True,edgecolor='black') plt.scatter(clf.coef_[relevant_features], 5 * np.ones(len(relevant_features)),color='navy', label="Relevant features") plt.ylabel("Features") plt.xlabel("Values of the weights") plt.legend(loc="upper left")plt.figure(figsize=(6, 5)) plt.title("Marginal log-likelihood") plt.plot(clf.scores_, color='navy', linewidth=lw) plt.ylabel("Score") plt.xlabel("Iterations")# Plotting some predictions for polynomial regression def f(x, noise_amount):y = np.sqrt(x) * np.sin(x)noise = np.random.normal(0, 1, len(x))return y + noise_amount * noisedegree = 10 X = np.linspace(0, 10, 100) y = f(X, noise_amount=0.1) clf_poly = BayesianRidge() clf_poly.fit(np.vander(X, degree), y)X_plot = np.linspace(0, 11, 25) y_plot = f(X_plot, noise_amount=0) y_mean, y_std = clf_poly.predict(np.vander(X_plot, degree), return_std=True) plt.figure(figsize=(6, 5)) plt.errorbar(X_plot, y_mean, y_std, color='navy',label="Polynomial Bayesian Ridge Regression", linewidth=lw) plt.plot(X_plot, y_plot, color='gold', linewidth=lw,label="Ground Truth") plt.ylabel("Output y") plt.xlabel("Feature X") plt.legend(loc="lower left") plt.show()轉(zhuǎn)載于:https://www.cnblogs.com/mtcnn/p/9412111.html
總結(jié)
以上是生活随笔為你收集整理的scikit-learn 学习笔记-- Generalized Linear Models (三)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: x264_param_default
- 下一篇: FFMpeg框架代码阅读