Search code examples
pythonmachine-learningattributeerrorgridsearchcv

how to find the optimized parameters using GridSearchCV


I'm trying to get the optimized parameters using GridSearchCV but I get the erorr:

AttributeError: 'DecisionTreeClassifier' object has no attribute 'best_params_'

I don't know where I did wrong.. this is the code of the models:

#DT
classifier = DecisionTreeClassifier(max_depth=800, min_samples_split=5)
params = {'criterion':['gini','entropy'],'splitter':['best', 'random']}
classifier = GridSearchCV(classifier, params, cv=3, n_jobs=4)
classifier.fit(train_vectors, train_labels)
classifier = classifier.best_estimator_
print("Best: using {0}".format(classifier.best_params_))

im new in this field, any help?


Solution

  • The classifier.best_estimator_ returns the best trained model which is a DecisionTreeClassifier in this case.

    To access the params use the method get_params() (see here)

    classifier.get_params()
    >>>
    {'ccp_alpha': 0.0,
     'class_weight': None,
     'criterion': 'gini',
     'max_depth': 5,
     'max_features': None,
     'max_leaf_nodes': None,
     'min_impurity_decrease': 0.0,
     'min_impurity_split': None,
     'min_samples_leaf': 1,
     'min_samples_split': 5,
     'min_weight_fraction_leaf': 0.0,
     'presort': 'deprecated',
     'random_state': None,
     'splitter': 'best'}
    

    This returns a dictionary from where you can now access the optimized params