Custom Objective Function

Hello, this will be a long question. I’m trying to define my own custom objective function
I want the XGBClassifier, so I run

from xgboost import XGBClassifier

the documentation of xgboost says:

''A custom objective function can be provided for the objective parameter. In this case, it should have the signature
objective(y_true, y_pred) -> grad, hess :
y_true: array_like of shape [n_samples], The target values
y_pred: array_like of shape [n_samples], The predicted values
grad: array_like of shape [n_samples], The value of the gradient for each sample point.
hess: array_like of shape [n_samples], The value of the second derivative for each sample point ‘’

Now, I’ve coded this custom:

def guess_averse_loss(y_true, y_pred):

y_true = y_true.astype(int)
y_pred = y_pred.astype(int) 
... stuffs ...
return grad, hess

everything is compatible with the previous documentation.

If I run:

classifier.train(X_train, y_train)

(where custom_weighted_accuracy is a custom metric defined by me following the documentation of scikitlearn)
I get the error:

-> first_term = np.multiply(cost_matrix[y_true, y_pred], np.exp(y_pred - y_true))

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (4043,) (4043,5)

So, y_pred enters the function as a matrix (n_samples x n_classes) where the element ij is the probability that the sample i belongs to the class j.

Then, I modify the line as

first_term = np.multiply(cost_matrix[y_true, np.argmax(y_pred, axis=1)],np.exp(np.argmax(y_pred, axis=1) - y_true))

so it passes from a matrix to an array,
This leads to the error:

unknown custom metric

so it seems that the problem now is the metric.

I try to remove the custom obj function using the default one and another error comes:

XGBoostError: Check failed: in_gpair->Size() % ngroup == 0U (3 vs. 0) : must have exactly ngroup * nrow gpairs


Hi, not sure if this is still unresolved. It would be great if you can post a self-contained snippet for others to run. Otherwise, it’s quite difficult to guess what’s happening exactly on your end.