-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboston_cv_penalized.py
More file actions
46 lines (40 loc) · 1.35 KB
/
boston_cv_penalized.py
File metadata and controls
46 lines (40 loc) · 1.35 KB
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
37
38
39
40
41
42
43
44
45
46
# This code is supporting material for the book
# Building Machine Learning Systems with Python
# by Willi Richert and Luis Pedro Coelho
# published by PACKT Publishing
#
# It is made available under the MIT License
# This script fits several forms of penalized regression
from __future__ import print_function
import numpy as np
from sklearn.cross_validation import KFold
from sklearn.linear_model import LinearRegression, ElasticNet, Lasso, Ridge
from sklearn.metrics import r2_score
from sklearn.datasets import load_boston
boston = load_boston()
x = boston.data
y = boston.target
for name, met in [
('linear regression', LinearRegression()),
('lasso()', Lasso()),
('elastic-net(.5)', ElasticNet(alpha=0.5)),
('lasso(.5)', Lasso(alpha=0.5)),
('ridge(.5)', Ridge(alpha=0.5)),
]:
# Fit on the whole data:
met.fit(x, y)
# Predict on the whole data:
p = met.predict(x)
r2_train = r2_score(y, p)
# Now, we use 10 fold cross-validation to estimate generalization error
kf = KFold(len(x), n_folds=5)
p = np.zeros_like(y)
for train, test in kf:
met.fit(x[train], y[train])
p[test] = met.predict(x[test])
r2_cv = r2_score(y, p)
print('Method: {}'.format(name))
print('R2 on training: {}'.format(r2_train))
print('R2 on 5-fold CV: {}'.format(r2_cv))
print()
print()