在scikit-learn中尝试使用PolynomialFeatures时出错

问题描述 投票:0回答:1

当我使用PolynomialFeatures时,我的代码返回错误:

poly1 = PolynomialFeatures(degree=1)
poly3 = PolynomialFeatures(degree=3)
poly6 = PolynomialFeatures(degree=6)
poly9 = PolynomialFeatures(degree=9)
X_train = X_train.reshape(-1,1)
y_train = y_train.reshape(-1,1)

predictions = []
predict = np.linspace(0,10,100)

x_poly1 = poly1.fit_transform(X_train).reshape(-1,1)
X_train1, X_test1, y_train1, y_test1 = train_test_split(x_poly1, y_train)
linreg1 = LinearRegression().fit(X_train1, y_train1)

x_poly3 = poly3.fit_transform(X_train).reshape(-1,1)
X_train3, X_test3, y_train3, y_test3 = train_test_split(x_poly3, y_train)
linreg3 = LinearRegression().fit(X_train3, y_train3)

x_poly6 = poly6.fit_transform(X_train).reshape(-1,1)
X_train6, X_test6, y_train6, y_test6 = train_test_split(x_poly6, y_train)
linreg6 = LinearRegression().fit(X_train6, y_train6)

x_poly9 = poly9.fit_transform(X_train).reshape(-1,1)
X_train9, X_test9, y_train9, y_test9 = train_test_split(x_poly9, y_train)
linreg9 = LinearRegression().fit(X_train, y_train)

predict1 = poly1.fit_transform(predict).reshape(-1,1)
predict3 = poly3.fit_transform(predict).reshape(-1,1)
predict6 = poly6.fit_transform(predict).reshape(-1,1)
predict9 = poly9.fit_transform(predict).reshape(-1,1)

ans1 = linreg1.predict(predict1)
ans3 = linreg3.predict(predict3)
ans6 = linreg6.predict(predict6)
ans9 = linreg9.predict(predict9)

predictions.append(ans1, ans3, ans6, ans9)

在我的代码中,我试图将所有值附加到列表中以备后用,但出现错误:

    ValueError                                Traceback (most recent call last)
<ipython-input-3-bca8e3056e3a> in <module>()
     18 
     19 x_poly1 = poly1.fit_transform(X_train).reshape(-1,1)
---> 20 X_train1, X_test1, y_train1, y_test1 = train_test_split(x_poly1, y_train)
     21 linreg1 = LinearRegression().fit(X_train1, y_train1)
     22 

/opt/conda/lib/python3.6/site-packages/sklearn/model_selection/_split.py in train_test_split(*arrays, **options)
   1687         test_size = 0.25
   1688 
-> 1689     arrays = indexable(*arrays)
   1690 
   1691     if stratify is not None:

/opt/conda/lib/python3.6/site-packages/sklearn/utils/validation.py in indexable(*iterables)
    204         else:
    205             result.append(np.array(X))
--> 206     check_consistent_length(*result)
    207     return result
    208 

/opt/conda/lib/python3.6/site-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
    179     if len(uniques) > 1:
    180         raise ValueError("Found input variables with inconsistent numbers of"
--> 181                          " samples: %r" % [int(l) for l in lengths])
    182 
    183 

ValueError: Found input variables with inconsistent numbers of samples: [22, 11]

我收到此错误的原因是什么?我希望最终结果是形状为(4,100)的数组。请询问是否需要澄清。

python scikit-learn
1个回答
0
投票

有些事情看似不正确,但是如果没有一个可以重新创建的简单示例以及完整的错误消息(指出产生该错误的行),这很难说清。我可以看穿

  • 您正在呼叫poly1.fit_transform 4次,怀疑这是复制粘贴错误
  • [predictions.append(ans1, ans3, ans6, ans9)可能应该是np.concatenate((ans1, ans3, ans6, ans9))

但是我认为主要原因是您添加到reshapepoly1.fit_transform(X_train)。在这种情况下,它将采用形状为(n,2)的变换的输出,并将其重塑为(2 * n,),其长度是原始X_train的两倍。]]

[我建议学习如何创建Pipeline以将PolynomialFeaturesLinearRegression组合成一个可以拟合和预测的单个对象。

即看看Polynomial interpolation

model = make_pipeline(PolynomialFeatures(degree), LinearRegression())
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
    
© www.soinside.com 2019 - 2024. All rights reserved.