Sunday, April 2, 2023

How to use GridSearchCV?

sklearn.model_selection.GridSearchCV(estimator, param_grid,scoring=None,

          n_jobs=None, iid='deprecated', refit=True, cv=None, verbose=0, 

          pre_dispatch='2*n_jobs', error_score=nan, return_train_score=False) 


1.estimator: Pass the model instance for which you want to check the hyperparameters.

2.params_grid: the dictionary object that holds the hyperparameters you want to try

3.scoring: evaluation metric that you want to use, you can simply pass a valid string/ object of evaluation metric

4.cv: number of cross-validation you have to try for each selected set of hyperparameters

5.verbose: you can set it to 1 to get the detailed print out while you fit the data to GridSearchCV

6.n_jobs: number of processes you wish to run in parallel for this task if it -1 it will use all available processors. 



#import all necessary libraries

import sklearn

from sklearn.datasets import load_breast_cancer

from sklearn.metrics import classification_report, confusion_matrix 

from sklearn.datasets import load_breast_cancer 

from sklearn.svm import SVC 

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import train_test_split 

 

#load the dataset and split it into training and testing sets

dataset = load_breast_cancer()

X=dataset.data

Y=dataset.target

X_train, X_test, y_train, y_test = train_test_split( 

                        X,Y,test_size = 0.30, random_state = 101) 

# train the model on train set without using GridSearchCV 

model = SVC() 

model.fit(X_train, y_train) 

   

# print prediction results 

predictions = model.predict(X_test) 

print(classification_report(y_test, predictions)) 




# defining parameter range 

param_grid = {'C': [0.1, 1, 10, 100],  

              'gamma': [1, 0.1, 0.01, 0.001, 0.0001], 

              'gamma':['scale', 'auto'],

              'kernel': ['linear']}  

   

grid = GridSearchCV(SVC(), param_grid, refit = True, verbose = 3,n_jobs=-1) 

   

# fitting the model for grid search 

grid.fit(X_train, y_train) 

 

# print best parameter after tuning 

print(grid.best_params_) 

grid_predictions = grid.predict(X_test) 

   

# print classification report 

print(classification_report(y_test, grid_predictions)) 

references:

No comments:

Post a Comment