sklearn.linear model.Lars

From GM-RKB
Jump to: navigation, search

A sklearn.linear model.Lars is an Least Angle Regression System within sklearn.linear_model class.

1) Import Lars Regression model from scikit-learn : from sklearn.linear_model import Lars
2) Create design matrix X and response vector Y
3) Create Lars Regression object: Larsreg=Lars([fit_intercept=True, verbose=False, normalize=True, precompute=’auto’, n_nonzero_coefs=500, eps=2.2204460492503131e-16, copy_X=True, fit_path=True, positive=False])
4) Choose method(s):
Input: Output:
#Importing modules
from sklearn.linear_model import Lars
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
Larsreg= Lars() # Create Lars regression object
Larsreg.fit(x,y) # predicted values

#Calculaton of RMSE and Explained Variances

yp_cv = cross_val_predict(Larsreg, 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: Lars 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('Lars Regression')
pl.ylabel('real')
pl.grid(True)
pl.show()
lars boston10fold.png
(blue dots correspond to 10-Fold CV)


Method: Lars Regression
RMSE on the dataset: 4.6800
RMSE on 10-fold CV: 6.2862
Explained Variance Regression Score on the dataset: 0.7406
Explained Variance Regression 10-fold CV: 0.5320


References

2017

Least Angle Regression model a.k.a. LAR.