I would like to add the kappa evaluation metric to use in xgboost in Python. I am having trouble understanding how to connect a Python function with xgboost.
According to the xgboost documentation, a "User can add multiple evaluation metrics, for python user, remember to pass the metrics in as list of parameters pairs instead of map, so that latter ‘eval_metric’ won’t override previous one"
This has been raised in xgboost's github page for R but not for Python.
For example if the kappa function is:
def kappa(preds, y):
# perform kappa calculation
return score
How do I go about implementing it with xgboost?
Specifing 'kappa'
as a string in the eval_metric
parameter
results in XGBoostError: unknown evaluation metric type: kappa
.
Likewise specifying the kappa method object results in XGBoostError: unknown evaluation metric type: <function kappa at 0x7fbef4b03488>
.
How can a custom evaluation metric be used in xgboost in python?
The eval_metric parameter determines the metrics that will be used to evaluate the model at each iteration, not to guide optimization. They are only reported and are not used to guide the CV optimization AFAIK.
The XGBoost objective function used when predicting numerical values is the “reg:squarederror” loss function. “reg:squarederror”: Loss function for regression predictive modeling problems.
As usual, you start by importing the library xgboost and other important libraries that you will be using for building the model. Note you can install python libraries like xgboost on your system using pip install xgboost on cmd. Separate the target variable and rest of the variables using . iloc to subset the data.
Change your method to:
def kappa(preds, y):
# perform kappa calculation
return 'kappa', score
And use it with feval
argument:
bst = xgb.train(params, dtrain, num_rounds, watchlist, feval=kappa, maximize=False)
When writing custom evaluation metrics remember about setting maximize
argument. Setting it to true means that the algorithm is getting better with bigger score of the evaluation metric.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With