首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多项式回归度增加后训练分数降低

多项式回归度增加后训练分数降低
EN

Stack Overflow用户
提问于 2017-12-08 23:53:36
回答 0查看 867关注 0票数 0

我正在尝试使用sklearnlinear_model.LinearRegression,使用线性回归将多项式拟合到添加了一些噪声的正弦信号中的一组点。

正如预期的那样,训练和验证分数随着多项式次数的增加而增加,但在某种程度上,大约20个东西开始变得奇怪,分数开始下降,模型返回的多项式看起来与我用来训练它的数据完全不同。

下面是一些可以看到这一点的图,以及生成回归模型和图的代码:

在degree=17之前,事情是如何运作的。原始数据与预测:

在那之后,它只会变得更糟:

验证曲线,增加多项式的次数:

代码语言:javascript
复制
from sklearn.pipeline import make_pipeline
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.learning_curve import validation_curve

def make_data(N, err=0.1, rseed=1):
    rng = np.random.RandomState(1)
    x = 10 * rng.rand(N)
    X = x[:, None]
    y = np.sin(x) + 0.1 * rng.randn(N)
    if err > 0:
        y += err * rng.randn(N)
    return X, y

def PolynomialRegression(degree=4):
    return make_pipeline(PolynomialFeatures(degree),
                         LinearRegression())


X, y = make_data(400)

X_test = np.linspace(0, 10, 500)[:, None]
degrees = np.arange(0, 40)

plt.figure(figsize=(16, 8))
plt.scatter(X.flatten(), y)
for degree in degrees:
    y_test = PolynomialRegression(degree).fit(X, y).predict(X_test)
    plt.plot(X_test, y_test, label='degre={0}'.format(degree))
plt.title('Original data VS predicted values for different degrees')
plt.legend(loc='best');


degree = np.arange(0, 40)
train_score, val_score = validation_curve(PolynomialRegression(), X, y,
                                                 'polynomialfeatures__degree',
                                                 degree, cv=7)

plt.figure(figsize=(12, 6))
plt.plot(degree, np.median(train_score, 1), marker='o', 
         color='blue', label='training score')
plt.plot(degree, np.median(val_score, 1), marker='o',
         color='red', label='validation score')
plt.legend(loc='best')
plt.ylim(0, 1)
plt.title('Learning curve, increasing the degree of the polynomium')
plt.xlabel('degree')
plt.ylabel('score');

我知道,当模型的复杂性增加时,验证分数会下降,但为什么训练分数也会下降呢?这里我能遗漏什么呢?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47717818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档