Sklearn错误,数组具有4个暗角.估计器< = 2 [英] Sklearn Error, array with 4 dim. Estimator <=2
本文介绍了Sklearn错误,数组具有4个暗角.估计器< = 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试通过熊猫从yahoo Finance导入数据,然后通过.as_matrix()将其转换为数组,然后当我将数据输入到分类器中进行训练时,它给了我一个错误.
I been trying import data from yahoo finance via panda then convert it to arrays via .as_matrix(), then as i input the data into the classifer to train, it gives me an error.
ValueError: Found array with dim 4. Estimator expected <= 2.
这是我的代码:
from sklearn import tree
import pandas as pd
import pandas_datareader.data as web
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
print(ma_100)
clf = tree.DecisionTreeClassifier()
x = [[close_buy,ma_50,ma_100,ma_200]]
y = [buy_sell]
clf.fit(x,y)
推荐答案
我发现了几个需要修复的错误/内容.
I found a couple of bugs/things needing fixing.
- 缺少括号
buy_sell = (df.loc['2013-02-15':'2016-05-21']).as_matrix
-
[[close_buy,ma_50,ma_100,ma_200]]
是为您提供4个维度的要素.取而代之的是,我使用np.concatenate
,它接受一个数组列表,并将它们彼此附加在长度方向或宽度方向.参数axis=1
指定宽度方向.这是使x
成为822 x 28矩阵,其中包含822个对28个特征的观察结果.如果这不是您想要的,那么显然我没有达到目标.但是这些尺寸符合您的y
.
- Missing parantheses
buy_sell = (df.loc['2013-02-15':'2016-05-21']).as_matrix
[[close_buy,ma_50,ma_100,ma_200]]
is what gives you your 4 dimensions. Instead, I'd usenp.concatenate
which takes a list of arrays and appends them to each other either length wise or width wise. the parameteraxis=1
specifies width wise. What this does is makex
an 822 x 28 matrix of 822 observations of 28 features. If this isn't what you were going for, then clearly I didn't hit the mark. But those dimensions line up with youry
.
相反:
from sklearn import tree
import pandas as pd
import pandas_datareader.data as web
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
print(ma_100)
clf = tree.DecisionTreeClassifier()
x = np.concatenate([close_buy,ma_50,ma_100,ma_200], axis=1) # Fixed
y = buy_sell # Brackets not necessary... I don't think
clf.fit(x,y)
这为我跑了
DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
min_samples_split=2, min_weight_fraction_leaf=0.0,
random_state=None, splitter='best')
这篇关于Sklearn错误,数组具有4个暗角.估计器< = 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文