Sklearn中的3d数组错误

时间:2016-05-21 20:21:40

标签: python-3.x pandas multidimensional-array dataframe scikit-learn

因此,我试图获取有关股票的数据,收盘价和移动平均线50,100,200。我得到了另一个数组,然后是买入或卖出的标签。它是在数据框架上与所有其他阵列一起制定的。但问题是,当我尝试训练分类时,它给了我一个错误:

ValueError: Found array with dim 3. Estimator expected <= 2.

When I concatenate the array, it gives me an error, ValueError: Unknown label type: array([[7.87401353,]])中包含更多值 这是我的代码:

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)
close_buy = df[['Close']+['B/S']]
closing = df[['Close']].as_matrix()
buy_sell = df[['B/S']]


close_buy = pd.DataFrame.dropna(close_buy, 0, 'any')
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')

close_buy = (df.loc['2013-02-15':'2016-05-21']).as_matrix()
ma_50 = (df.loc['2013-02-15':'2016-05-21']).as_matrix()
ma_100 = (df.loc['2013-02-15':'2016-05-21']).as_matrix()
ma_200 = (df.loc['2013-02-15':'2016-05-21']).as_matrix()
buy_sell = (df.loc['2013-02-15':'2016-05-21']).as_matrix()  # Fixed

list(close_buy)

clf = tree.DecisionTreeClassifier()
X = list([close_buy,ma_50,ma_100,ma_200]) 
y = [buy_sell]  

1 个答案:

答案 0 :(得分:1)

问题是您正在创建一个变量X,它是一个二维数组的列表。这自动意味着第三维。

# offending line
X = list([close_buy,ma_50,ma_100,ma_200])

这需要连接以维持2个维度。

# corrected
X = np.concatenate([close_buy,ma_50,ma_100,ma_200], axis=1)

另外,我怀疑一旦这个问题得到解决,你将会有另一个问题:

 y = [buy_sell]

没有理由将其包装在[]中。这将导致相同的三维问题。就这样说:

y = buy_sell