预期2D阵列,取而代之的是1D阵列,有什么解决方案吗?

时间:2018-12-28 15:32:23

标签: python arrays numpy

我是使用机器学习的新手,我正在尝试预测30天的股票价格。

这是我的代码:

import pandas as pd
import matplotlib.pyplot as plt
import pymysql as MySQLdb
import numpy as np 
import sqlalchemy
import datetime
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing, svm
from sklearn.model_selection import train_test_split

forecast_out = int(30) 
df['Prediction'] = df[['LastPrice']].shift(-forecast_out)
df['Prediction'].fillna(0)
X = np.array(df['Prediction'].fillna(0))
X = preprocessing.scale(X)
X_forecast = X[-forecast_out:] 
X = X[:-forecast_out]
y = np.array(df['Prediction'].fillna(0))
y = y[:-forecast_out]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
X_train, X_test, y_train, y_test.reshape(-1,1)

# Training
clf = LinearRegression()
clf.fit(X_train,y_train)
# Testing
confidence = clf.score(X_test, y_test)
print("confidence: ", confidence)
forecast_prediction = clf.predict(X_forecast)
print(forecast_prediction)

我收到此错误:

  

ValueError:预期的2D数组,取而代之的是1D数组:

array=[-0.46939923 -0.47076913 -0.47004993 ... -0.42782272  3.07433019 -0.46573474].
     

使用

重塑数据      

array.reshape(-1, 1)(如果您的数据具有单一功能)
  或
  array.reshape(1, -1)(如果其中包含一个样本)。

0 个答案:

没有答案
相关问题