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
#print(boston.head())
# 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.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
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):
[2.89333694]
Coefficients (Lasso):
[ 1.00035566 -0.02439867]
Lasso prediction:
8.514947307107153
x-values:
10 11
0 15.3 396.9
8.514946415000002