线性回归器无法预测一组值;错误:ValueError:形状(100,1)和(2,1)未对齐:1(dim 1)!= 2(dim 0)
Posted
技术标签:
【中文标题】线性回归器无法预测一组值;错误:ValueError:形状(100,1)和(2,1)未对齐:1(dim 1)!= 2(dim 0)【英文标题】:Linear Regressor unable to predict a set of values; Error: ValueError: shapes (100,1) and (2,1) not aligned: 1 (dim 1) != 2 (dim 0) 【发布时间】:2019-11-23 10:29:29 【问题描述】:我有 2 个 numpy 数组:
x= np.linspace(1,10,100) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10
我想使用这些数据集训练线性回归器。为了比较复杂度和泛化之间的关系,我对一组 4 度 (1, 3, 6, 9)
使用 h 多项式特征预处理。
拟合模型后,我想在数组上测试x = np.linspace(1, 10, 100)
经过多次尝试,我发现 x 和 y 数组需要重新整形,我就这样做了。但是,当我创建要预测的新 x 数据集时,它抱怨维度未对齐。估计器正在对原始 x 数组进行测试拆分。
下面是我的代码
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
np.random.seed(0)
n = 100
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)
def fn_one():
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
x_predict = np.linspace(0,10,100)
x_predict = x_predict.reshape(-1, 1)
degrees = [1, 3, 6, 9]
predictions = []
for i, deg in enumerate(degrees):
linReg = LinearRegression()
pf = PolynomialFeatures(degree=deg)
xt = x.reshape(-1, 1)
yt = y.reshape(-1, 1)
X_transformed = pf.fit_transform(xt)
X_train_transformed, X_test_transformed, y_train_temp, y_test_temp = train_test_split(X_transformed, yt, random_state=0)
linReg.fit(X_train_transformed, y_train_temp)
predictions.append(linReg.predict(x_predict))
np.array(predictions)
return predictions
不同数组的形状(@degree 3 in the loop)
x_predict = (100, 1)
xt = 100, 1
yt = 100, 1
X_train_transformed = 75, 4
y_train_temp = 75, 1
X_test_transformed = 25, 4
y_train_temp = 25, 1
X_test_transformed = 4, 25, 1 的预测
x_predict 的预测 = 不工作:
Error = ValueError:形状 (100,1) 和 (2,1) 未对齐:1 (dim 1) != 2(暗淡 0)
【问题讨论】:
【参考方案1】:您忘记转换您的x_predict
。我在下面更新了您的代码:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
np.random.seed(0)
n = 100
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)
def fn_one():
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
x_predict = np.linspace(0,10,100)
x_predict = x_predict.reshape(-1, 1)
degrees = [1, 3, 6, 9]
predictions = []
for i, deg in enumerate(degrees):
linReg = LinearRegression()
pf = PolynomialFeatures(degree=deg)
xt = x.reshape(-1, 1)
yt = y.reshape(-1, 1)
X_transformed = pf.fit_transform(xt)
X_train_transformed, X_test_transformed, y_train_temp, y_test_temp = train_test_split(X_transformed, yt, random_state=0)
linReg.fit(X_train_transformed, y_train_temp)
x_predict_transformed = pf.fit_transform(x_predict)
predictions.append(linReg.predict(x_predict_transformed))
np.array(predictions)
return predictions
现在,当您致电 fn_one()
时,您将获得预测。
希望这会有所帮助!
【讨论】:
如果您认为这个答案帮助您解决了问题,我恳请您接受它的答案,因为如果他们也遇到类似的问题,它将帮助其他人找到正确的答案。提前致谢!更多详情请参考***.com/help/someone-answers以上是关于线性回归器无法预测一组值;错误:ValueError:形状(100,1)和(2,1)未对齐:1(dim 1)!= 2(dim 0)的主要内容,如果未能解决你的问题,请参考以下文章
机器学习之路:python线性回归分类器 进行良恶性肿瘤分类预测