I have started an MLS course. As a beginner and non-mathematician it has been hard.

I am trying to understand the exercise about Lasso Models. I have done Lasso models on R-cran, but this is my first time with Python.

I have a dataset of cars with seven variables. The exercise consists on doing a Lasso Model to predict the gasoline consumption of the cars, the dependent variable, so $x$ is a table with the rest of variables and $y$ is the consumption.

Then, if I launch Lasso on scikitlearn:

modelLasso = Lasso(alpha=0.1).fit(x, y)
  • I do not understand the result. Has it generated a prediction on $y$ of every row in the table?

  • If so, how can I access to the array of predictions of the model and how do I use the model to predict the consumption given new $x$ values?


Here is a little lasso example using the Boston Housing Data. The code also shows how to:

  • choose optimal alpha,
  • display data and predictions,
  • display estimated coefficients,
  • and how to make a prediction by hand.

# Import toy data
from sklearn.datasets import load_boston
import pandas as pd
# Load toy data
boston = load_boston(return_X_y=False)
# Make data a pandas data frame
boston = pd.DataFrame(boston.data)
# Print head of toy data
# Define x,y (only take few columns for x)
y = boston[[12]] # choose column 12
x = boston[[10,11]] # only column 10,11

from sklearn.linear_model import Lasso
from sklearn.linear_model import LassoCV
lasso = Lasso(max_iter=10000)

# Perform lasso CV to get the best parameter alpha for regulation
# usually use scaled data mean = 0, sd = 1 by applying scale()
# I don't do this here for brevity
# https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.scale.html
lassocv = LassoCV(alphas=None, cv=10, max_iter=10000)
lassocv.fit(x, y.values.ravel())

# Fit lasso using the best alpha
lasso.fit(x, y)

# Show the estimated coefficients/intercept
print('Intercept (Lasso): \n', lasso.intercept_)
print('Coefficients (Lasso): \n', lasso.coef_)

# Predict lasso / print first predicted value
preds = lasso.predict(x)
print('Lasso prediction: \n', preds[0])

# Print first x values
print('x-values: \n', x.head(1))

# Predict "by hand"
print(2.89333694+1.00035566*15.3 - 0.02439867*396.9)

Output is:

Intercept (Lasso): 
Coefficients (Lasso): 
 [ 1.00035566 -0.02439867]

Lasso prediction: 

      10     11
0  15.3  396.9

| improve this answer | |
  • 1
    $\begingroup$ Nice thanks for the example. $\endgroup$ – user70164 Jun 12 '19 at 20:28
  • 1
    $\begingroup$ @Universal_learner: btw, you might know the book „Introduction to Statistical Learning“, find PDF here: www-bcf.usc.edu/~gareth/ISL. It is fab and not too technical. The book comes with a lot of good R examples. Python code is also available: github.com/a-martyn/ISL-python. IMO the best way to start ML. Chapter 6 covers Lasso etc... $\endgroup$ – Peter Jun 12 '19 at 21:29
  • 1
    $\begingroup$ Thanks also for that reference. $\endgroup$ – user70164 Jun 12 '19 at 21:34

Lasso() created a Lasso regressor object. The .fit() argument makes the regressor "learn" from your data. The parameters are tuned in order to "fit" the training data.

Once the regressor object is fit, you can use it to .predict() on test data.

| improve this answer | |
  • $\begingroup$ Thanks. And so if I do modelLasso.predict(x), being x a table with no consumption row, the output are the values on y the model predicts? $\endgroup$ – user70164 Jun 12 '19 at 13:05
  • $\begingroup$ Ah ok, yes it works, so simple in Python. thanks again. $\endgroup$ – user70164 Jun 12 '19 at 13:08
  • $\begingroup$ You're welcome. If you find it helpful, please consider accepting the answer $\endgroup$ – Leevo Jun 12 '19 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy