【发布时间】: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 的。
标签: python scikit-learn regression