# Custom objective function to optimize f_score metric XGBClassifier multiclassification python [closed]

I use xgboost.XGBClassifier for a multiclass classification problem, and I want to adjust the algorithm such way as to make the metric f1_score(average='macro') higher. I tried changing a parameter objective of XGBClassifier and made the following:

from sklearn.metrics import f1_score
def f1_macro(y_true, y_pred):
return f1_score(y_true, y_pred, average='macro')
xg_tree = xgb.XGBClassifier(max_depth = 3, n_estimators = 1000, booster = 'gbtree',
n_jobs=-1, subsample=1, learning_rate=0.005, objective = f1_macro, num_cass = 3)
xg_tree.fit(X_train, y_train)


But I have a mistake

ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets

As I've read the custom objective function should output the value of a gradient and a hessian.

Could you please help me to figure out what way I should rewrite my objective function to fix this mistake?

• @MatthewDrury Thank you for your answer. Do you mean, that I should firstly fit classifier, using mlogloss as an objective function. After that I should somehow pick up the value of threshold( which is used to identify what class an object belongs to ) such way as to maximize f_score? – D F Mar 18 '18 at 16:33