所以我提供了测试数据,但是当我尝试使用clf.predict()进行测试时,它只是给了我一个错误。所以我希望它能够预测我给出的数据,即最后的收盘价,移动平均线。然而每次我尝试一些东西它只是给我一个错误。还有比熊猫更好的方法。
from sklearn import tree
import pandas as pd
import pandas_datareader.data as web
import numpy as np
df = web.DataReader('goog', 'yahoo', start='2012-5-1', end='2016-5-20')
close_price = df[['Close']]
ma_50 = (pd.rolling_mean(close_price, window=50))
ma_100 = (pd.rolling_mean(close_price, window=100))
ma_200 = (pd.rolling_mean(close_price, window=200))
#adding buys and sell based on the values
df['B/S']= (df['Close'].diff() < 0).astype(int)
closing = (df[['Close']])
buy_sell = (df[['B/S']])
ma_50 = pd.DataFrame.dropna(ma_50, 0, 'any')
ma_100 = pd.DataFrame.dropna(ma_100, 0, 'any')
ma_200 = pd.DataFrame.dropna(ma_200, 0, 'any')
closing = (df.loc['2013-02-15':'2016-05-21'])
ma_50 = (df.loc['2013-02-15':'2016-05-21'])
ma_100 = (df.loc['2013-02-15':'2016-05-21'])
ma_200 = (df.loc['2013-02-15':'2016-05-21'])
buy_sell = (df.loc['2013-02-15':'2016-05-21']) # Fixed
close = pd.DataFrame(closing)
ma50 = pd.DataFrame(ma_50)
ma100 = pd.DataFrame(ma_100)
ma200 = pd.DataFrame(ma_200)
buy_sell = pd.DataFrame(buy_sell)
clf = tree.DecisionTreeRegressor()
x = np.concatenate([close, ma50, ma100, ma200], axis=1)
y = buy_sell
clf.fit(x,y)
close_buy1 = close[:-1]
m5 = ma_50[:-1]
m10 = ma_100[:-1]
ma20 = ma_200[:-1]
b = np.concatenate([close_buy1, m5, m10, ma20], axis=1)
clf.predict([close_buy1, m5, m10, ma20])
这给出的错误是:
ValueError: cannot copy sequence with size 821 to array axis with dimension `7`
我试着做我所知道的一切,但实际上并没有成功。
答案 0 :(得分:4)
我认为你不能通过列表进行预测。
相反,您必须同时far ret
功能DataFrames / matrices:
concat