【问题标题】:Multivariable linear regression doesn't get more accurate with higher polynomial degree?多项式次数越高,多元线性回归不会变得更准确?
【发布时间】:2019-08-09 04:39:05
【问题描述】:

我正在计算 MSE 在训练集上,所以我希望 MSE 在使用更高的多项式时会降低。但是,从 4 级到 5 级,MSE 显着增加。可能是什么原因?

import pandas as pd, numpy as np
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt

path = "https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/automobileEDA.csv"
df = pd.read_csv(path)
r=[]
max_degrees = 10

y = df['price'].astype('float')
x = df[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']].astype('float')

for i in range(1,max_degrees+1):
    Input = [('scale', StandardScaler()), ('polynomial', PolynomialFeatures(degree=i)), ('model', LinearRegression())]
    pipe = Pipeline(Input)
    pipe.fit(x,y)
    yhat = pipe.predict(x)
    r.append(mean_squared_error(yhat, y))
    print("MSE for MLR of degree "+str(i)+" = "+str(round(mean_squared_error(yhat, y)/1e6,1)))

plt.figure(figsize=(10,3))
plt.plot(list(range(1,max_degrees+1)),r)
plt.show()

结果:

【问题讨论】:

    标签: python scikit-learn regression mse


    【解决方案1】:

    最初,您在 y 中有 200 个观测值,在 X 中有 4 个特征(列),然后您将其缩放和转换为多项式特征。

    因此,4 级具有 120 200 个多项式特征的,即比观察更多的特征。

    当特征多于观察值时,线性回归定义不明确,不应使用,如here 所述。这可能解释了从 4 级到 5 级时拟合训练集的突然恶化。对于更高的度,LR 求解器似乎仍然能够过度拟合训练数据。

    【讨论】:

      猜你喜欢
      • 2021-02-02
      • 2020-04-20
      • 2015-03-22
      • 2019-07-25
      • 2010-11-23
      • 2015-07-04
      • 2020-09-03
      • 2019-05-02
      • 1970-01-01
      相关资源
      最近更新 更多