Sklearn Lasso - Training a Lasso Regression Model

Python

Lasso regression is a type of regularised linear regression that uses L1 regularisation. Lasso regression is ideally used when fewer features are expected to be important as Lasso regression effectively performs automatic feature selection.

To alter the amount of regularisation applied to the model, the value of the alpha hyperparameter can be adjusted. The larger the alpha value, the more regularisation is applied while if alpha is equal to zero then regularisation is removed altogether.

In this code snippet we train a Lasso regression model with alpha set to 0.5. We then create and print out a dictionary showing the coefficient for each feature before evaluating the model against test data using a range of regression metrics.

 1|  from sklearn.linear_model import Lasso
 2|  from sklearn.metrics import mean_squared_error, mean_absolute_error, max_error, explained_variance_score, mean_absolute_percentage_error
 3|  
 4|  # initialise & fit Lasso regression model with alpha set to 0.5
 5|  model = Lasso(alpha=0.5)
 6|  model.fit(X_train, y_train)
 7|  
 8|  # create dictionary that contains the feature coefficients
 9|  coef = dict(zip(X_train.columns, model.coef_.T))
10|  print(coef)
11|  
12|  # make prediction for test data & evaluate performance
13|  y_pred = model.predict(X_test)
14|  print('RMSE:',mean_squared_error(y_test, y_pred, squared = False))
15|  print('MAE:',mean_absolute_error(y_test, y_pred))
16|  print('MAPE:',mean_absolute_percentage_error(y_test, y_pred))
17|  print('Max Error:',max_error(y_test, y_pred))
18|  print('Explained Variance Score:',explained_variance_score(y_test, y_pred))
Did you find this snippet useful?

Sign up for free to to add this to your code library