pandas ValueError:无法将大小为 821 的序列复制到维度为 7 的数组轴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37401096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: cannot copy sequence with size 821 to array axis with dimension 7
提问by sam202252012
So I fed the testing data, but when I try to test it with clf.predict() it just gives me an error. So I want it to predict on the data that i give, which is the last close price, the moving averages. However everytime i try something it just gives me an error. Also is there a better way to do this than on pandas.
所以我提供了测试数据,但是当我尝试使用 clf.predict() 测试它时,它只会给我一个错误。所以我希望它预测我提供的数据,即最后收盘价,移动平均线。然而,每次我尝试一些东西时,它都会给我一个错误。还有比Pandas更好的方法来做到这一点。
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])
The error which this gives is:
这给出的错误是:
ValueError: cannot copy sequence with size 821 to array axis with dimension `7`
I tried to do everything i know but it really did not work out.
我试图做我知道的一切,但它真的没有奏效。
回答by Andy Hayden
I don't think you can pass a list to predict.
我认为您无法通过列表进行预测。
Rather, you have to concat
the feature DataFrames/matrices together:
相反,您必须将数据concat
帧/矩阵一起使用:
In [11]: clf.predict(pd.concat([close_buy1, m5, m10, ma20], axis=1))
Out[11]:
array([[ 7.87401353e+02, 7.93261381e+02, 7.87071324e+02, ...,
5.48000000e+06, 3.96049623e+02, 0.00000000e+00],
[ 7.95991368e+02, 8.07001373e+02, 7.95281379e+02, ...,
5.88550000e+06, 4.03022676e+02, 0.00000000e+00],
[ 8.05301357e+02, 8.08971379e+02, 7.91791350e+02, ...,
5.54900000e+06, 3.95834832e+02, 1.00000000e+00],
...,
[ 7.15989990e+02, 7.21520020e+02, 7.04109985e+02, ...,
1.99950000e+06, 7.06229980e+02, 1.00000000e+00],
[ 7.03669983e+02, 7.11599976e+02, 7.00630005e+02, ...,
1.76340000e+06, 7.06630005e+02, 0.00000000e+00],
[ 7.02359985e+02, 7.06000000e+02, 6.96799988e+02, ...,
1.65630000e+06, 7.00320007e+02, 1.00000000e+00]])