2012-03-08 21:04:57 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
=========================================================
|
|
|
|
|
Ordinary Least Squares and Ridge Regression Variance
|
|
|
|
|
=========================================================
|
|
|
|
|
Due to the few points in each dimension and the straight
|
|
|
|
|
line that linear regression uses to follow these points
|
|
|
|
|
as well as it can, noise on the observations will cause
|
2013-06-27 21:09:16 +08:00
|
|
|
great variance as shown in the first plot. Every line's slope
|
2012-03-08 21:04:57 +08:00
|
|
|
can vary quite a bit for each prediction due to the noise
|
|
|
|
|
induced in the observations.
|
|
|
|
|
|
|
|
|
|
Ridge regression is basically minimizing a penalised version
|
|
|
|
|
of the least-squared function. The penalising `shrinks` the
|
|
|
|
|
value of the regression coefficients.
|
|
|
|
|
Despite the few data points in each dimension, the slope
|
|
|
|
|
of the prediction is much more stable and the variance
|
|
|
|
|
in the line itself is greatly reduced, in comparison to that
|
|
|
|
|
of the standard linear regression
|
|
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2012-03-08 21:04:57 +08:00
|
|
|
|
|
|
|
|
|
2013-07-30 18:41:56 +08:00
|
|
|
# Code source: Gaël Varoquaux
|
|
|
|
|
# Modified for documentation by Jaques Grobler
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-03-08 21:04:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
2013-10-24 22:25:30 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2012-03-08 21:04:57 +08:00
|
|
|
|
|
|
|
|
from sklearn import linear_model
|
|
|
|
|
|
2012-04-28 18:04:36 +08:00
|
|
|
X_train = np.c_[.5, 1].T
|
2012-03-08 21:04:57 +08:00
|
|
|
y_train = [.5, 1]
|
2012-04-28 18:04:36 +08:00
|
|
|
X_test = np.c_[0, 2].T
|
2012-03-08 21:04:57 +08:00
|
|
|
|
|
|
|
|
np.random.seed(0)
|
|
|
|
|
|
2012-12-25 20:16:05 +08:00
|
|
|
classifiers = dict(ols=linear_model.LinearRegression(),
|
|
|
|
|
ridge=linear_model.Ridge(alpha=.1))
|
2012-03-08 21:04:57 +08:00
|
|
|
|
2014-02-02 19:52:31 +08:00
|
|
|
for name, clf in classifiers.items():
|
2018-10-09 21:44:57 +08:00
|
|
|
fig, ax = plt.subplots(figsize=(4, 3))
|
2012-03-08 21:04:57 +08:00
|
|
|
|
|
|
|
|
for _ in range(6):
|
2012-04-28 18:04:36 +08:00
|
|
|
this_X = .1 * np.random.normal(size=(2, 1)) + X_train
|
2012-03-08 21:04:57 +08:00
|
|
|
clf.fit(this_X, y_train)
|
|
|
|
|
|
2018-10-01 22:32:42 +08:00
|
|
|
ax.plot(X_test, clf.predict(X_test), color='gray')
|
|
|
|
|
ax.scatter(this_X, y_train, s=3, c='gray', marker='o', zorder=10)
|
2012-03-08 21:04:57 +08:00
|
|
|
|
|
|
|
|
clf.fit(X_train, y_train)
|
|
|
|
|
ax.plot(X_test, clf.predict(X_test), linewidth=2, color='blue')
|
2018-10-01 22:32:42 +08:00
|
|
|
ax.scatter(X_train, y_train, s=30, c='red', marker='+', zorder=10)
|
2012-03-08 21:04:57 +08:00
|
|
|
|
2018-10-09 21:44:57 +08:00
|
|
|
ax.set_title(name)
|
|
|
|
|
ax.set_xlim(0, 2)
|
2012-03-08 21:04:57 +08:00
|
|
|
ax.set_ylim((0, 1.6))
|
|
|
|
|
ax.set_xlabel('X')
|
|
|
|
|
ax.set_ylabel('y')
|
2018-10-09 21:44:57 +08:00
|
|
|
|
|
|
|
|
fig.tight_layout()
|
2012-03-08 21:04:57 +08:00
|
|
|
|
2013-10-24 22:25:30 +08:00
|
|
|
plt.show()
|