data set
from sklearn.datasets import load_diabetes
diabetes = load_diabetes()
from sklearn.model_selection import train_test_split
diabetes_X_train, diabetes_X_test, diabetes_y_train,
diabetes_y_test = train_test_split(diabetes['data'],
diabetes['target'], random_state=0)
What is the training and test R2 for the Lasso model using the
default parameters? How many features does this model use? What are
the names of those features?
Here is the code to fit model:
from sklearn.linear_model import Lasso
lr = Lasso()
lr.fit(diabetes_X_train, diabetes_y_train)
Code to compute R-squared on train and test:
from sklearn.metrics import r2_score
tr_p = lr.predict(diabetes_X_train)
print(r2_score(diabetes_y_train, tr_p))
Score on Train: 0.4141
te_p = lr.predict(diabetes_X_test)
print(r2_score(diabetes_y_test, te_p))
Score on Test: 0.2781
We build model on all the features but since lasso penalizes the coefficients and reduces to 0, there are only 2 featires left:
1.) bmi
2.) s5
Get Answers For Free
Most questions answered within 1 hours.