由于形状问题,使用多项式回归拟合模型不允许预测

时间:2017-10-16 02:53:24

标签: python machine-learning scikit-learn data-science

我在下面写了代码来使用多项式回归。能够适应模型,但无法预测!!

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 linreg

linreg = 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的多项式?

我正在尝试学习多项式回归,任何人都可以解释发生了什么吗?

1 个答案:

答案 0 :(得分:0)

from sklearn.pipeline import Pipeline
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

pipeline = 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))