0

I try to build multiclass classification Machine Learning model in Python. I use Hyperopt to tune my hyperparameters as below:

1. Define Parameter Space for Optimization

space = {
    "n_estimators": hp.choice("n_estimators", [100, 200, 300, 400,500,600]),
    "max_depth": hp.quniform("max_depth", 1, 15,1)
}

2. Defining a Function to Minimize (Objective Function) hyperopic minimizes the function, that why I add a negative sign in the prec to maximize precison

def hyperparameter_tuning(params):
    model = XGBClassifier(**params)
    model.fit(X_train, y_train)
    y_pred_test = model.predict(X_test)
    preds_prob_test = model.predict_proba(X_test_lgb)
    prec = precision_score(y_test, y_pred_test, average="macro")
    return {"loss": -prec, "status": STATUS_OK}

3. Fine Tune the Model

trials = Trials()

best = fmin(
    fn=hyperparameter_tuning,
    space = space, 
    algo=tpe.suggest, 
    max_evals=100, 
    trials=trials
)

4. best estimators print("Best: {}".format(best))

100%|█████████████████████████████████████████████████████████| 100/100 [10:30<00:00, 6.30s/trial, best loss: -0.8915] Best: {‘max_depth’: 11.0, ‘n_estimators’: 2}.

My task:

By above code i am able to maximize Precision for the whole model, but how can I modify Objective Function (pkt. 2) so as to maximize Precision for each class of my multiclass classification model instead of for the whole model ?

How can I do that in Python ?

dingaro
  • 2,156
  • 9
  • 29

0 Answers0