Shape error when using PolynomialFeatures

匆匆过客 提交于 2021-02-10 16:54:32

问题


The Issue

To begin with I'm pretty new to machine learning. I have decided to test up some of the things that I have learned on some financial datam my machine learning model looks like this:

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)

all works great until the moment I try to implement PolynomialFeatures which brings to be the following error:

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.

Attempts to solve the issue:

Atempt 1

I've tried adding .values to X but the same error still comes up:

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

Atempt 2

I tried solving this problem by adding reshape(-1, 1) at the end of X_poly:

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

but it just replaces the previous error with this one:

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

Thank you very much in advance for your help.


回答1:


It wants you to transform your input. Try using X_poly = poly_features.fit_transform(X.values.reshape(1,-1))[1]



来源:https://stackoverflow.com/questions/51775903/shape-error-when-using-polynomialfeatures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!