Explain Grid Search with example and code.

844    Asked by AshishSinha in Data Science , Asked on Nov 30, 2019
Answered by Ashish Sinha

Grid search is an algorithm used for tuning a model by considering every hyperparameters specified and used for evaluating a model. It is used to determine the optimal values for a given model.

To implement Grid Search in Python, we can use the following code.

# Applying Grid Search to find the best model and the best parameters

from sklearn.model_selection import GridSearchCV

parameters = [{'C': [1, 10, 100, 1000], 'kernel': ['linear']},

              {'C': [1, 10, 100, 1000], 'kernel': ['rbf'], 'gamma': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]}]

grid_search = GridSearchCV(estimator = classifier,

                           param_grid = parameters,

                           scoring = 'accuracy',

                           cv = 10,

                           n_jobs = -1)

grid_search = grid_search.fit(X_train, y_train)

best_accuracy = grid_search.best_score_

best_parameters = grid_search.best_params_

To implement Grid search in R, we use the following code




Your Answer

Interviews

Parent Categories