多項式插值?
這個例子演示了如何用嶺回歸用n_degree多項式逼近一個函數。具體而言,從n_samples 1D點 出發,建立Vandermonde矩陣就足夠了,它是n個樣本 x n_degree+1,其形式如下:
[[1, x_1, x_1 ** 2, x_1 ** 3, …],
[1, x_2, x_2 ** 2, x_2 ** 3, …], …]
直觀地說,這個矩陣可以解釋為偽特征矩陣(提高到某種冪的點)。該矩陣類似于(但不同于)由多項式核導出的矩陣。
此示例顯示,您可以使用線性模型進行非線性回歸,使用pipeline添加非線性特性。核方法擴展了這一思想,可以誘導出非常高(甚至無限)維數的特征空間。

print(__doc__)
# Author: Mathieu Blondel
# Jake Vanderplas
# License: BSD 3 clause
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
def f(x):
""" function to approximate by polynomial interpolation"""
return x * np.sin(x)
# 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)
rng = np.random.RandomState(0)
rng.shuffle(x)
x = np.sort(x[:20])
y = f(x)
# create matrix versions of these arrays
X = x[:, np.newaxis]
X_plot = x_plot[:, np.newaxis]
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")
for count, degree in enumerate([3, 4, 5]):
model = make_pipeline(PolynomialFeatures(degree), Ridge())
model.fit(X, y)
y_plot = model.predict(X_plot)
plt.plot(x_plot, y_plot, color=colors[count], linewidth=lw,
label="degree %d" % degree)
plt.legend(loc='lower left')
plt.show()
腳本的總運行時間:(0分0.091秒)
Download Python source code:plot_polynomial_interpolation.py
Download Jupyter notebook:plot_polynomial_interpolation.ipynb