Scikit-learn using GridSearchCV on DecisionTreeClassifier

user5425156 picture user5425156 · Aug 2, 2016 · Viewed 16.2k times · Source

I tried to use GridSearchCV on DecisionTreeClassifier, but get the following error: TypeError: unbound method get_params() must be called with DecisionTreeClassifier instance as first argument (got nothing instead)

here's my code:

from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.model_selection import GridSearchCV
from sklearn.cross_validation import  cross_val_score

X, Y = createDataSet(filename)
tree_para = {'criterion':['gini','entropy'],'max_depth':[4,5,6,7,8,9,10,11,12,15,20,30,40,50,70,90,120,150]}
clf = GridSearchCV(DecisionTreeClassifier, tree_para, cv=5)
clf.fit(X, Y)

Answer

Abhinav Arora picture Abhinav Arora · Aug 2, 2016

In your call to GridSearchCV method, the first argument should be an instantiated object of the DecisionTreeClassifier instead of the name of the class. It should be

clf = GridSearchCV(DecisionTreeClassifier(), tree_para, cv=5)

Check out the example here for more details.

Hope that helps!