2011-07-08 17:16:56 +08:00
|
|
|
"""
|
|
|
|
|
===========================================================
|
|
|
|
|
Plot Ridge coefficients as a function of the regularization
|
|
|
|
|
===========================================================
|
|
|
|
|
|
2013-07-22 21:48:44 +08:00
|
|
|
Shows the effect of collinearity in the coefficients of an estimator.
|
|
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
.. currentmodule:: sklearn.linear_model
|
2011-07-08 17:16:56 +08:00
|
|
|
|
2013-07-22 21:48:44 +08:00
|
|
|
:class:`Ridge` Regression is the estimator used in this example.
|
|
|
|
|
Each color represents a different feature of the
|
2012-05-24 21:44:18 +08:00
|
|
|
coefficient vector, and this is displayed as a function of the
|
|
|
|
|
regularization parameter.
|
|
|
|
|
|
2016-01-10 01:29:13 +08:00
|
|
|
This example also shows the usefulness of applying Ridge regression
|
|
|
|
|
to highly ill-conditioned matrices. For such matrices, a slight
|
|
|
|
|
change in the target variable can cause huge variances in the
|
|
|
|
|
calculated weights. In such cases, it is useful to set a certain
|
|
|
|
|
regularization (alpha) to reduce this variation (noise).
|
|
|
|
|
|
|
|
|
|
When alpha is very large, the regularization effect dominates the
|
|
|
|
|
squared loss function and the coefficients tend to zero.
|
2012-05-24 21:44:18 +08:00
|
|
|
At the end of the path, as alpha tends toward zero
|
2011-07-08 17:16:56 +08:00
|
|
|
and the solution tends towards the ordinary least squares, coefficients
|
2016-01-10 01:29:13 +08:00
|
|
|
exhibit big oscillations. In practise it is necessary to tune alpha
|
|
|
|
|
in such a way that a balance is maintained between both.
|
2011-07-08 17:16:56 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Author: Fabian Pedregosa -- <fabian.pedregosa@inria.fr>
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2011-07-08 17:16:56 +08:00
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-07-08 17:16:56 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import linear_model
|
2011-07-08 17:16:56 +08:00
|
|
|
|
|
|
|
|
# X is the 10x10 Hilbert matrix
|
|
|
|
|
X = 1. / (np.arange(1, 11) + np.arange(0, 10)[:, np.newaxis])
|
|
|
|
|
y = np.ones(10)
|
|
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-07-08 17:16:56 +08:00
|
|
|
# Compute paths
|
|
|
|
|
|
|
|
|
|
n_alphas = 200
|
|
|
|
|
alphas = np.logspace(-10, -2, n_alphas)
|
|
|
|
|
|
|
|
|
|
coefs = []
|
|
|
|
|
for a in alphas:
|
2017-02-01 20:13:12 +08:00
|
|
|
ridge = linear_model.Ridge(alpha=a, fit_intercept=False)
|
|
|
|
|
ridge.fit(X, y)
|
|
|
|
|
coefs.append(ridge.coef_)
|
2011-07-08 17:16:56 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-07-08 17:16:56 +08:00
|
|
|
# Display results
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
ax = plt.gca()
|
2011-07-08 17:16:56 +08:00
|
|
|
|
|
|
|
|
ax.plot(alphas, coefs)
|
|
|
|
|
ax.set_xscale('log')
|
2011-12-24 02:12:26 +08:00
|
|
|
ax.set_xlim(ax.get_xlim()[::-1]) # reverse axis
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlabel('alpha')
|
|
|
|
|
plt.ylabel('weights')
|
|
|
|
|
plt.title('Ridge coefficients as a function of the regularization')
|
|
|
|
|
plt.axis('tight')
|
|
|
|
|
plt.show()
|