I am using three different of the shelf classifiers. It's a three class classification task. I want to calculate the optimal weights (c1weight, c2weight, c3weight) for each classifier (real task more classifiers and also weights for each class).

Maybe simple grid search approach or sklearn ensemble classifier could do that.

vc = VotingClassifier(estimators=[('gbc',GradientBoostingClassifier()),

params = {'weights':[[1,2,3],[2,1,3],[3,2,1]]}
grid_Search = GridSearchCV(param_grid = params, estimator=vc)

I don't understand how to implement this for the following code.

def get_classification(text, c1weight, c2weight, c3weight):

  prediction1 = classifier1.predict(text)
  if prediction1 = 1:
    class1 =+ 1 * c1weight
  elif prediction1 = 2:
    class2 =+ 1  * c1weight
    class3 =+ 1  * c1weight

  prediction2 = classifier2.predict(text)
  if prediction2 = 1:
    class1 =+ 1 * c2weight
  elif prediction2 = 2:
    class2 =+ 1  * c2weight
    class3 =+ 1  * c2weight

  prediction3 = classifier3.predict(text)
  if prediction3 = 1:
    class1 =+ 1 * c3weight
  elif prediction3 = 2:
    class2 =+ 1  * c3weight
    class3 =+ 1  * c3weight

  if class1 > class2 and class1 > class3:
    return ("class1",class1)
  elif class2 > class1 and class2 > class3:
    return ("class2",class2)

c1weight = 0.5
c2weight = 0.7
c3weight = 0.4

for i, row in df_raw.iterrows():
    classification = get_classification(df_raw.at[i, 'text'],c1weight, c2weight, c3weight)
    df_raw[i,'classification'] = classification 
score = get_accuracy(df_raw['classification'],df_raw['label'])

2 Answers 2


GridSearch finds those optimals weights for you.

You can access these weights through the attribute best_params_ of the GridSearch object, which will return all the optimal parameters (including the weights):

optimal_weights = grid_Search.best_params_
  • $\begingroup$ in that case my function must return the desired accuracy measure? $\endgroup$
    – bartman99
    Commented Oct 12, 2019 at 0:36
  • $\begingroup$ Yes, because because GridSearchCV uses ParameterGrid in the background but will return you the scores (cross validated) and best parameters : scikit-learn.org/stable/modules/generated/… $\endgroup$ Commented Oct 12, 2019 at 2:56
  • 1
    $\begingroup$ Please don't write the answer in your original post, post it below (and I think you want to use GridSearchCV instead of a simple ParameterGrid) $\endgroup$ Commented Oct 12, 2019 at 2:58

(This is the asker's solution, moved from question and comment to answer)

This sample code helped me to understand it:

def your_function(number):

from sklearn.model_selection import ParameterGrid
param_grid = {'param1': [1, 2, 3]}

grid = ParameterGrid(param_grid)

for params in grid:

I had too much paramaters for gridsearch. In this case it would take months to calculate all combinations. Finally i used hyperopt for the hyperparameter optimization. There are some nice basic tutorials out there. This one helped me a lot. You can also find a python notebook there. https://towardsdatascience.com/an-introductory-example-of-bayesian-optimization-in-python-with-hyperopt-aae40fff4ff0


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.