Search code examples
machine-learningscikit-learngrid-search

How to use the best parameter as parameter of a classifier in GridSearchCV?


I have a function called svc_param_selection(X, y, n) which returns best_param_. Now I want to use the best_params returned as the parameter of a classifier like: .

parameters = svc_param_selection(X, y, 2)

from sklearn.model_selection import ParameterGrid

from sklearn.svm import SVC

param_grid = ParameterGrid(parameters)

for params in param_grid:
    svc_clf = SVC(**params)
    print (svc_clf)

classifier2=SVC(**svc_clf)

It seems parameters is not a grid here..


Solution

  • You can use GridSearchCV to do this. There is a example here:

    # Applying GridSearch to find best parameters
    from sklearn.model_selection import GridSearchCV
    parameters = [{ 'criterion' : ['gini'], 'splitter':['best','random'], 'min_samples_split':[0.1,0.2,0.3,0.4,0.5], 
               'min_samples_leaf': [1,2,3,4,5]},
              {'criterion' : ['entropy'], 'splitter':['best','random'], 'min_samples_split':[0.1,0.2,0.3,0.4,0.5],
               'min_samples_leaf': [1,2,3,4,5]} ]
    gridsearch = GridSearchCV(estimator = classifier, param_grid = parameters,refit= False, scoring='accuracy', cv=10)
    gridsearch = gridsearch.fit(x,y)
    optimal_accuracy = gridsearch.best_score_
    optimal_parameters = gridsearch.best_params_
    

    But for param_grid of GridSearchCV, you should pass a dictionary of parameter name and value for you classifier. For example a classifier like this:

    from sklearn.tree import DecisionTreeClassifier
    classifier = DecisionTreeClassifier(random_state=0, presort=True, 
                                        criterion='entropy')
    classifier = classifier.fit(x_train,y_train)
    

    Then after finding best parameters by GridSearchCV you apply them on you model.