Skip to content

Commit 025420c

Browse files
committed
DOC Better documentation
1 parent c71fd5b commit 025420c

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

ch07/boston1.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@
55
#
66
# It is made available under the MIT License
77

8+
# This script shows an example of simple (ordinary) linear regression
9+
810
import numpy as np
911
from sklearn.datasets import load_boston
1012
import pylab as plt
1113

1214
boston = load_boston()
1315
x = np.array([np.concatenate((v, [1])) for v in boston.data])
1416
y = boston.target
17+
18+
# np.linal.lstsq implements least-squares linear regression
1519
s, total_error, _, _ = np.linalg.lstsq(x, y)
1620

1721
rmse = np.sqrt(total_error[0] / len(x))
1822
print('Residual: {}'.format(rmse))
1923

24+
# Plot the prediction versus real:
2025
plt.plot(np.dot(x, s), boston.target, 'ro')
26+
27+
# Plot a diagonal (for reference):
2128
plt.plot([0, 50], [0, 50], 'g-')
2229
plt.xlabel('predicted')
2330
plt.ylabel('real')

ch07/boston_cv10_penalized.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#
66
# It is made available under the MIT License
77

8+
# This script fits several forms of penalized regression
9+
810
from __future__ import print_function
911
from sklearn.cross_validation import KFold
1012
from sklearn.linear_model import ElasticNet, Lasso, Ridge
@@ -20,12 +22,18 @@
2022
('lasso(.5)', Lasso(fit_intercept=True, alpha=0.5)),
2123
('ridge(.5)', Ridge(fit_intercept=True, alpha=0.5)),
2224
]:
25+
# Fit on the whole data:
2326
met.fit(x, y)
27+
28+
# Predict on the whole data:
2429
p = np.array([met.predict(xi) for xi in x])
30+
2531
e = p - y
32+
# np.dot(e, e) == sum(ei**2 for ei in e) but faster
2633
total_error = np.dot(e, e)
2734
rmse_train = np.sqrt(total_error / len(p))
2835

36+
# Now, we use 10 fold cross-validation to estimate generalization error
2937
kf = KFold(len(x), n_folds=10)
3038
err = 0
3139
for train, test in kf:

ch07/predict10k_en.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
from sklearn.linear_model import ElasticNet, LinearRegression
1212

1313
data, target = load_svmlight_file('data/E2006.train')
14-
lr = LinearRegression(fit_intercept=True)
15-
en = ElasticNet(fit_intercept=True, alpha=.1)
1614

17-
met = en
15+
# Edit the lines below if you want to switch method:
16+
# met = LinearRegression(fit_intercept=True)
17+
met = ElasticNet(fit_intercept=True, alpha=.1)
1818

1919
kf = KFold(len(target), n_folds=10)
2020
err = 0

ch07/usermodel.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323

2424
def movie_norm(xc):
25+
'''Normalize per movie'''
2526
xc = xc.copy().toarray()
27+
# x1 is the mean of the positive items
2628
x1 = np.array([xi[xi > 0].mean() for xi in xc])
2729
x1 = np.nan_to_num(x1)
2830

0 commit comments

Comments
 (0)