# What happens when scikit-learn does a Lasso Model?

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
import pandas as pd
# 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)

# 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.set_params(alpha=lassocv.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

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


Output is:

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

Lasso prediction:
8.514947307107153

x-values:
10     11
0  15.3  396.9

8.514946415000002

• Nice thanks for the example.
– user70164
Jun 12, 2019 at 20:28
• @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... Jun 12, 2019 at 21:29
• Thanks also for that reference.
– user70164
Jun 12, 2019 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.

• 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?
– user70164
Jun 12, 2019 at 13:05
• Ah ok, yes it works, so simple in Python. thanks again.
– user70164
Jun 12, 2019 at 13:08
• You're welcome. If you find it helpful, please consider accepting the answer Jun 12, 2019 at 13:34