How to adjust gridsearchcv in python to optimize for one class?



I am trying to get the best results for a particular class in my data, for example I have 5 classes and I am trying to have gridsearch find the best params in SVC to optimize for class 2 because that is the class I want the model to be able to detect.
Thank you


Hi, you can try this.

svc_clf = SVC()
grid_search = GridSearchCV(svc_clf, param_grid), y_train == 2)

Note that we are passing binary class label in fit method.


Thank yo for your reply.
so in case we have a binary classfication problem we pass to “” x_train and for y_train we pass the class either 0 or 1 that we want gridsearch to optimize for, correct ?

so:, y_train==1)

Can you please link to any documentation etc… that I can learn more ?

Also please help with Multiclass problem, for example from classes 0 , 1, 2 , i want to get the best parameters from grid search to optimize the results for class “2”.

Thank you so much.


Hi, when we train a model in supervised scenario, the model learns the optimized parameters with respect to the target labels. So, if when we pass y_train == 2 as target vector to train a model, we are passing a vector where 2 means True and NOT 2 means False, which essentially implies that our model learns to distinguish between 2 and everything else other than 2. That’s what the code I gave above does.

Now, I am not sure if you are looking for that or something else. Because if you are building a model which will learn multiclass classification, the hyperparameters should be optimized in such a way the model learns all labels properly. At least this is what my understanding is, I might be wrong though.

Can you please elaborate a bit on what exactly you are trying to achieve?


Thank you for your explanation.

We have two scenarios:

1- Binary Classification, where we want to train our model to classify based on input data whether the input data will be classified as 1 or 0

In this scenario when training the model:, y_train) the model is by default optimizing to get both classes as accurate as possible. Now if the the dataset is imbalanced, say 99% class 0 and 1% class 1 , then the model will by default over-emphasize the majority class (0) and return a model that is good at detecting class (0) but not class (1).

So above was in case just using, y_train) without using gridsearchcv.

From what I understand the code you provided and by assigning:, y_train == 2) or, y_train == 0) or, y_train == 1) ,

tells gridsearch to find the parameters that are best optimized to what I have assigned. So for example:, y_train == 2) means to find the best parameters that emphasizes the target class (2) and this can be applied both for binary [0,1] or a multiclass dataset where we have more than two target classes for example [0,1, 2, 3, 4, 5]

so to recap if we don’t tell our model or gridsearch that we want you to optimize for a specific class, then what happens is the model or gridsearch based on the scoring parameter like f1 or log_loss tries to optmize for all target classes.

2- Multiclass classification which we covered above and entails just telling the model or gridsearch what our
target class is, y_train==2)

I hope I make sense.


The grid search provided by [Grid Search CV exhaustively generates candidates from a grid of parameter values specified with the param_grid parameter. For instance, the following param_grid :

param_grid = [ {‘C’: [1, 10, 100, 1000], ‘kernel’: [‘linear’]}, {‘C’: [1, 10, 100, 1000], ‘gamma’: [0.001, 0.0001], ‘kernel’: [‘rbf’]}, ]

specifies that two grids should be explored: one with a linear kernel and C values in [1, 10, 100, 1000], and the second one with an RBF kernel, and the cross-product of C values ranging in [1, 10, 100, 1000] and gamma values in [0.001, 0.0001].

The GridSearchCV instance implements the usual estimator API: when “fitting” it on a dataset all the possible combinations of parameter values are evaluated and the best combination is retained.