使用多项式回归拟合模型时,由于形状问题无法进行预测

我编写了下面的代码来使用多项式回归。虽然能够拟合模型,但无法进行预测!

def polynomial_function(power=5, random_state=9):    global X_train    global y_train    X_train =  X_train[['item_1','item_2','item_3','item_4']]    rng = np.random.RandomState(random_state)    poly = PolynomialFeatures(degree=power, include_bias=False)    linreg = LinearRegression(normalize=True)    new_X_train = poly.fit_transform(X_train)    linreg.fit(new_X_train, y_train)    new_x_test  = np.array([4, 5, 6, 7]).reshape(1, -1)    print linreg.predict(new_x_test)    return linreglinreg = polynomial_function()

我收到了以下错误消息:

ValueError: shapes (1,4) and (125,) not aligned: 4 (dim 1) != 125 (dim 0)       

错误发生在这里,

new_x_test  = np.array([4, 5, 6, 7]).reshape(1, -1)print linreg.predict(new_x_test)

我发现 new_X_train 的形状是 (923, 125),而 new_x_test 的形状是 (1, 4)

这有什么关系吗?

当我尝试使用形状为 (1, 4) 的数据进行预测时,算法会尝试将其转换为不同的形状吗?

它会尝试为测试数据找到一个5次多项式吗?

我正在学习多项式回归,有人能解释一下发生了什么吗?


回答:

from sklearn.pipeline import Pipelinefrom sklearn.linear_model import LinearRegressionfrom sklearn.preprocessing import PolynomialFeaturespipeline = Pipeline([    ('poly', PolynomialFeatures(degree=5, include_bias=False)),    ('linreg', LinearRegression(normalize=True))    ])pipeline.fit(X_train, y_train)pipeline.predict(np.array([4, 5, 6, 7]).reshape(1, -1))

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注