Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scikit-learn using GridSearchCV on DecisionTreeClassifier

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)
like image 383
user5425156 Avatar asked Aug 01 '16 23:08

user5425156


People also ask

Can we use GridSearchCV for decision tree?

DecisionTree Classifier — Working on Moons Dataset using GridSearchCV to find best hyperparameters. Decision Tree's are an excellent way to classify classes, unlike a Random forest they are a transparent or a whitebox classifier which means we can actually find the logic behind decision tree's classification.

Can we use GridSearchCV for regression?

Let's take example of common machine learning algorithms starting with regression models: There are two different approaches which you can take, use gridsearchcv to perform hyperparameter tuning on one model or multiple models.

Is RandomizedSearchCV better than GridSearchCV?

The only difference between both the approaches is in grid search we define the combinations and do training of the model whereas in RandomizedSearchCV the model selects the combinations randomly. Both are very effective ways of tuning the parameters that increase the model generalizability.


1 Answers

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!

like image 64
Abhinav Arora Avatar answered Sep 25 '22 23:09

Abhinav Arora