了解及;使用Matplotlib绘制L2正则化
我正在尝试学习L2正则化&使用matplotlib绘制它 我遵循官方的sklearn文档。我创建了一个示例代码来了解它是如何工作的。 下面是我的代码了解及;使用Matplotlib绘制L2正则化,matplotlib,machine-learning,scikit-learn,Matplotlib,Machine Learning,Scikit Learn,我正在尝试学习L2正则化&使用matplotlib绘制它 我遵循官方的sklearn文档。我创建了一个示例代码来了解它是如何工作的。 下面是我的代码 import matplotlib.pyplot as plt import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.preprocessing import PolynomialFeatur
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import Ridge
lambda_value = 0.1
X = np.sort(np.random.uniform(0, 1, 100))[:, np.newaxis]
N = np.random.normal(0, 1)
y = np.sin(2*np.pi*X) + N * 0.1
X_plot = np.linspace(0, 1, 100)
X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.5)
train_err = []
test_err = []
pipeline = make_pipeline(PolynomialFeatures(degree=9),Ridge(alpha=lambda_value))
pipeline.fit(X_train, y_train)
y_train_preds = pipeline.predict(X_train)
y_test_preds = pipeline.predict(X_test)
plt.scatter(X, y, color='b')
plt.plot(X_test, y_test_preds, color='r')
plt.show()
当我画这个的时候,它看起来像这样。
为什么它是这样合适(红色线)?根据我的理解,这应该是一条线
预测错了吗?还是策划
我哪里做错了