拟合多项式回归时形状未对齐

Posted

技术标签:

【中文标题】拟合多项式回归时形状未对齐【英文标题】:Shapes not aligned when fitting polynomial regression 【发布时间】:2021-12-26 08:49:17 【问题描述】:

长时间聆听,第一次来电...

我知道过去已经回答了类似的问题(请参阅 here 了解我引用的其他线程),但我仍然遇到困难。我怎样才能让我的回归适合?我的代码如下:

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

#data
np.random.seed(0)
n = 15
x = np.linspace(0,10,n) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10

X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)

#regression fitting
X_predict_input = np.linspace(0,10,100).reshape(-1,1)
y_train = y_train.reshape((-1,1))
X_train = X_train.reshape((-1,1))

#looping through different degree values
for i, degree in enumerate([1,3,6,9]):
    poly = PolynomialFeatures(degree=degree)
    X_train_poly = poly.fit_transform(X_train)
    linreg = LinearRegression().fit(X_train_poly, y_train)
    result[i,:] = linreg.predict(X_predict_input)

我尝试修复 X_train 和 y_train 的整形问题,但在查看了每个形状后,我认为 X_train_poly 是导致此错误的原因...

X_train shape: (11, 1)
y_train shape: (11, 1)
X_train_poly shape: (11, 10)

相应的错误信息:

ValueError:形状 (100,1) 和 (2,1) 未对齐:1 (dim 1) != 2 (dim 0)

当我尝试通过以下方式解决 X_train_poly 中的形状不一致问题时...

X_train_poly = poly.fit_transform(X_train).reshape((-1,1))

...我收到此错误:

ValueError: 发现样本数量不一致的输入变量:[22, 11]

我在这方面花了很多时间,所以任何见解都将不胜感激!

提前谢谢你:)

【问题讨论】:

我在运行您的脚本时收到一条不同的错误消息:ValueError: X has 1 features, but LinearRegression is expecting 2 features as input.。这个错误是有道理的,因为 degree=1 的回归有 2 个特征,而 X_predict_input 只有一个。我不知道您是如何得到问题中显示的 ValueError 的。 【参考方案1】:

我认为问题很简单。您正在使用 PolynomialFeatures 转换为训练数据生成特征,但在预测方面,您并未对输入数据应用相同的转换。

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

# data
np.random.seed(0)
n = 15
x = np.linspace(0, 10, n) + np.random.randn(n)/5
y = np.sin(x) + x/6 + np.random.randn(n)/10

X_train, X_test, y_train, y_test = train_test_split(x.reshape((-1, 1)), 
                                                    y.reshape((-1, 1)), 
                                                    random_state=0)

# Check data matrices are in columns
assert(X_train.shape == (11, 1))
assert(y_train.shape == (11, 1))

# Build library of polynomial features
degree = 3
poly = PolynomialFeatures(degree)
X_train_poly = poly.fit_transform(X_train)
assert(X_train_poly.shape == (11, 4))

# Fit model
linreg = LinearRegression().fit(X_train_poly, y_train)

# Make prediction
X_predict = np.linspace(0, 10, 100).reshape(-1, 1)
X_predict_poly = poly.fit_transform(X_predict)
y_predict = linreg.predict(X_predict_poly)
assert(y_predict.shape == X_predict.shape)

更新:

为避免每次进行预测时都必须应用转换带来的不便,您可能需要查看sklearn.Pipeline:

# Using a pipeline to automate the input transformation
from sklearn.pipeline import Pipeline

poly = PolynomialFeatures(degree)
model = LinearRegression()
pipeline = Pipeline(steps=[('t', poly), ('m', model)])

linreg = pipeline.fit(X_train, y_train)
y_predict2 = linreg.predict(X_predict)

assert(np.array_equal(y_predict, y_predict2))

【讨论】:

以上是关于拟合多项式回归时形状未对齐的主要内容,如果未能解决你的问题,请参考以下文章

带有sklearn的numpy多项式线性回归

多项式回归

回归-多项式回归算法

回归-多项式回归算法

插值 回归 拟合 逼近的区别

如何从多项式拟合线性回归模型中的给定 Y 值预测 X 值?