2011-03-24 15:04:40 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
"""
|
|
|
|
|
========================
|
|
|
|
|
Polynomial interpolation
|
|
|
|
|
========================
|
|
|
|
|
|
|
|
|
|
This example demonstrates how to approximate a function with a polynomial of
|
|
|
|
|
degree n_degree by using ridge regression. Concretely, from n_samples 1d
|
|
|
|
|
points, it suffices to build the Vandermonde matrix, which is n_samples x
|
|
|
|
|
n_degree+1 and has the following form:
|
|
|
|
|
|
|
|
|
|
[[1, x_1, x_1 ** 2, x_1 ** 3, ...],
|
|
|
|
|
[1, x_2, x_2 ** 2, x_2 ** 3, ...],
|
|
|
|
|
...]
|
|
|
|
|
|
|
|
|
|
Intuitively, this matrix can be interpreted as a matrix of pseudo features (the
|
|
|
|
|
points raised to some power). The matrix is akin to (but different from) the
|
|
|
|
|
matrix induced by a polynomial kernel.
|
|
|
|
|
|
|
|
|
|
This example shows that you can do non-linear regression with a linear model,
|
2013-11-12 09:18:16 +08:00
|
|
|
using a pipeline to add non-linear features. Kernel methods extend this idea
|
|
|
|
|
and can induce very high (even infinite) dimensional feature spaces.
|
2011-03-24 15:04:40 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-03-24 15:04:40 +08:00
|
|
|
|
|
|
|
|
# Author: Mathieu Blondel
|
2013-11-12 09:18:16 +08:00
|
|
|
# Jake Vanderplas
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2011-03-24 15:04:40 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2013-11-13 04:15:14 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-03-24 15:04:40 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.linear_model import Ridge
|
2013-11-12 09:18:16 +08:00
|
|
|
from sklearn.preprocessing import PolynomialFeatures
|
2013-12-19 06:37:03 +08:00
|
|
|
from sklearn.pipeline import make_pipeline
|
2011-03-24 15:04:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def f(x):
|
|
|
|
|
""" function to approximate by polynomial interpolation"""
|
|
|
|
|
return x * np.sin(x)
|
|
|
|
|
|
2013-12-19 06:37:03 +08:00
|
|
|
|
2011-03-24 15:04:40 +08:00
|
|
|
# generate points used to plot
|
|
|
|
|
x_plot = np.linspace(0, 10, 100)
|
|
|
|
|
|
|
|
|
|
# generate points and keep a subset of them
|
|
|
|
|
x = np.linspace(0, 10, 100)
|
2011-12-19 18:43:24 +08:00
|
|
|
rng = np.random.RandomState(0)
|
|
|
|
|
rng.shuffle(x)
|
2011-03-24 15:04:40 +08:00
|
|
|
x = np.sort(x[:20])
|
|
|
|
|
y = f(x)
|
|
|
|
|
|
2013-11-12 09:18:16 +08:00
|
|
|
# create matrix versions of these arrays
|
|
|
|
|
X = x[:, np.newaxis]
|
|
|
|
|
X_plot = x_plot[:, np.newaxis]
|
|
|
|
|
|
2015-10-22 20:12:06 +08:00
|
|
|
colors = ['teal', 'yellowgreen', 'gold']
|
|
|
|
|
lw = 2
|
|
|
|
|
plt.plot(x_plot, f(x_plot), color='cornflowerblue', linewidth=lw,
|
|
|
|
|
label="ground truth")
|
|
|
|
|
plt.scatter(x, y, color='navy', s=30, marker='o', label="training points")
|
2011-03-24 15:04:40 +08:00
|
|
|
|
2015-10-22 20:12:06 +08:00
|
|
|
for count, degree in enumerate([3, 4, 5]):
|
2013-12-19 06:37:03 +08:00
|
|
|
model = make_pipeline(PolynomialFeatures(degree), Ridge())
|
2013-11-12 09:18:16 +08:00
|
|
|
model.fit(X, y)
|
|
|
|
|
y_plot = model.predict(X_plot)
|
2015-10-22 20:12:06 +08:00
|
|
|
plt.plot(x_plot, y_plot, color=colors[count], linewidth=lw,
|
|
|
|
|
label="degree %d" % degree)
|
2011-03-24 15:04:40 +08:00
|
|
|
|
2013-11-12 09:18:16 +08:00
|
|
|
plt.legend(loc='lower left')
|
2011-03-24 15:04:40 +08:00
|
|
|
|
2013-11-12 09:18:16 +08:00
|
|
|
plt.show()
|