2016-05-21 2 views
1

Ich habe versucht, importieren Daten von Yahoo Finance über Panda dann wandeln Sie es in Arrays über .as_matrix(), dann als ich die Daten in die Classifer eingeben, um zu trainieren, gibt es mir einen Fehler.Sklearn Fehler, Array mit 4 dim. Estimator <= 2

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

Das unten ist mein Code:

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) 

Antwort

1

Ich fand ein paar Bugs/Dinge Fixierung benötigt.

  1. Fehlende Klammern buy_sell = (df.loc['2013-02-15':'2016-05-21']).as_matrix
  2. [[close_buy,ma_50,ma_100,ma_200]] ist, was Sie Ihre 4 Dimensionen gibt. Stattdessen würde ich np.concatenate verwenden, die eine Liste von Arrays aufnimmt und sie entweder in der Länge oder Breite anfügt. Der Parameter axis=1 spezifiziert die Breite. Was dies tut ist x eine 822 x 28 Matrix von 822 Beobachtungen von 28 Funktionen. Wenn das nicht das ist, was Sie anstrebten, dann habe ich eindeutig nicht ins Schwarze getroffen. Aber diese Dimensionen passen zu Ihrem y.

Statt:

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) 

Das ist für mich lief:

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') 
+0

was tut np.concatenate – sam202252012

+0

ich einen Kommentar hinzufügen werde zu beantworten ... done – piRSquared

+0

Also, wenn diese läuft , wird es sein, der Preis, ma_50, ma_100, ma_200. Werden diese Daten in das clf als – sam202252012