sklearn.linear model.BayesianRidge

From GM-RKB
Jump to: navigation, search

A sklearn.linear_model.BayesianRidge is a Bayesian Ridge Regression System within sklearn.linear_model class.

1) Import Bayesian Ridge Regression model from scikit-learn : from sklearn.linear_model import BayesianRidge
2) Create design matrix X and response vector Y
3) Create BayesianRidge object: BRreg=BayesianRidge([n_iter=300, tol=0.001, alpha_1=1e-06, alpha_2=1e-06, lambda_1=1e-06, lambda_2=1e-06, compute_score=False, fit_intercept=True, normalize=False, copy_X=True, verbose=False])
4) Choose method(s):
Input: Output:
#Importing modules
from sklearn.linear_model import BayesianRidge
from sklearn.model_selection import cross_val_predict
from sklearn.datasets import load_boston
from sklearn.metrics import explained_variance_score, mean_squared_error
import numpy as np
import pylab as pl
boston = load_boston() #Loading boston datasets
x = boston.data # Creating Regression Design Matrix
y = boston.target # Creating target dataset
BRreg= BayesianRidge() # Create BR regression object
BRreg.fit(x,y) # predicted values

#Calculaton of RMSE and Explained Variances

yp_cv = cross_val_predict(BRreg, x, y, cv=10) #Calculation 10-Fold CV
Evariance=explained_variance_score(y,yp)
Evariance_cv=explained_variance_score(y,yp_cv)
RMSE =np.sqrt(mean_squared_error(y,yp))
RMSECV =sqrt(mean_squared_error(y,yp_cv)

# Printing Results

print('Method: BayesianRidge Regression')
print('RMSE on the dataset: %.4f' %RMSE)
print('RMSE on 10-fold CV: %.4f' %RMSECV)
print('Explained Variance Regression Score on the dataset: %.4f' %Evariance)
print('Explained Variance Regression 10-fold CV: %.4f' %Evariance_cv)

#plotting real vs predicted data pl.figure(1) pl.plot(yp, y,'ro') pl.plot(yp_cv, y,'bo', alpha=0.25, label='10-folds CV') pl.xlabel('predicted') pl.title('BR Regression') pl.ylabel('real') pl.grid(True) pl.show()

BR boston10fold.png
(blue dots correspond to 10-Fold CV)


Method: BayesianRidge Regression
RMSE on the dataset: 4.7641
RMSE on 10-fold CV: 5.8463
Explained Variance Regression Score on the dataset: 0.7311
Explained Variance Regression 10-fold CV: 0.5952


References

2017

  • http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.BayesianRidge.html
    • QUOTE: class sklearn.linear_model.BayesianRidge(n_iter=300, tol=0.001, alpha_1=1e-06, alpha_2=1e-06, lambda_1=1e-06, lambda_2=1e-06, compute_score=False, fit_intercept=True, normalize=False, copy_X=True, verbose=False)

      Bayesian ridge regression

      Fit a Bayesian ridge model and optimize the regularization parameters lambda (precision of the weights) and alpha (precision of the noise).