-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLasso_Regression.py
More file actions
36 lines (25 loc) · 824 Bytes
/
Lasso_Regression.py
File metadata and controls
36 lines (25 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pandas as pd
from numpy import arange
from sklearn.linear_model import LassoCV
from sklearn.model_selection import RepeatedKFold
# read in data
data_full = pd.read_csv("mtcars.csv"")
# select subset of data
data = data_full[["mpg", "wt", "drat", "qsec", "hp"]]
# view first six rows of data
data[0:6]
# define predictor and response variables
X = data[["mpg", "wt", "drat", "qsec"]]
y = data["hp"]
# define cross-validation method to evaluate model
cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)
# define model
model = LassoCV(alphas=arange(0, 1, 0.01), cv=cv, n_jobs=-1)
# fit model
model.fit(X, y)
# display lambda that produced the lowest test MSE
print(model.alpha_)
# define new observation
new = [24, 2.5, 3.5, 18.5]
# predict hp value using lasso regression model
model.predict([new])