【问题标题】:Shape error when using PolynomialFeatures使用多项式特征时的形状错误
【发布时间】:2019-03-22 23:05:44
【问题描述】:

问题

首先,我对机器学习还很陌生。我决定测试我在一些财务数据上学到的一些东西,我的机器学习模型如下所示:

import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

df = pd.read_csv("/Users/Documents/Trading.csv")
poly_features = PolynomialFeatures(degree=2, include_bias=False)
linear_reg = LinearRegression(fit_intercept = True)

X = df_copy[["open","volume", "base volume", "RSI_14"]]
X_poly = poly_features.fit_transform(X)[1]


y = df_copy[["high"]]

linear_reg.fit(X_poly, y)

x = linear_reg.predict([[1.905E-05, 18637.07503453,0.35522205,  69.95820948552947]])
print(x)

在我尝试实现 PolynomialFeatures 之前一切正常,这会导致以下错误:

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

解决问题的尝试:

尝试 1

我尝试将 .values 添加到 X 但仍然出现相同的错误:

X_poly = poly_features.fit_transform(X.values)[1]

尝试 2

我尝试通过在X_poly 末尾添加reshape(-1, 1) 来解决这个问题:

 X_poly = poly_features.fit_transform(X)[1].reshape(-1, 1)

但它只是用这个替换了之前的错误:

ValueError: Found input variables with inconsistent numbers of samples: [14, 5696]

非常感谢您的帮助。

【问题讨论】:

    标签: python pandas numpy scikit-learn


    【解决方案1】:

    它希望你转换你的输入。尝试使用X_poly = poly_features.fit_transform(X.values.reshape(1,-1))[1]

    【讨论】:

    • 天哪,非常感谢它成功了!!!我刚刚运行了代码(大约 5 分钟前它还在运行,你知道这是为什么吗?)
    • PolynomialFeatures 以指数方式转换您的数据,因此取决于数据集的维度,您的运行时间也会如此。
    猜你喜欢
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 2020-10-10
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多