[doc] Add R code tabs to custom_metric_obj tutorial#12196
Conversation
Port Python code blocks to multi-language tab format for the Custom Objective and Evaluation Metric tutorial. Adds equivalent R code alongside existing Python examples. Contributes to dmlc#11413
There was a problem hiding this comment.
Pull request overview
This PR updates the “Custom Objective and Evaluation Metric” tutorial to present side-by-side Python and R examples using Sphinx tabbed code blocks, aiming to make the tutorial multi-language and consistent with the tabs plugin introduced in #11410.
Changes:
- Converted existing Python
.. code-block::examples into.. tabs::/.. code-tab::blocks. - Added R equivalents for custom objective, custom metric, and multi-class objective/metric examples.
- Added a note clarifying that the scikit-learn interface section is Python-only.
Comments suppressed due to low confidence (1)
doc/tutorials/custom_metric_obj.rst:167
- This paragraph still says “Since we are demonstrating in Python…”, but the surrounding section now contains both Python and R tabs. Consider rewording to avoid implying the discussion only applies to Python (or explicitly scope the statement to the Python tab).
Since we are demonstrating in Python, the metric or objective need not be a function, any
callable object should suffice. Similar to the objective function, our metric also
accepts ``predt`` and ``dtrain`` as inputs, but returns the name of the metric itself and
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # With the use of `custom_metric` parameter in train function, custom metric receives | ||
| # raw input only when custom objective is also being used. Otherwise custom metric | ||
| # will receive transformed prediction. | ||
| assert predt.shape == (d_train.num_row(), n_classes) |
|
|
||
| def merror(predt: np.ndarray, dtrain: xgb.DMatrix): | ||
| """Used when there's no custom objective.""" | ||
| # No need to do transform, XGBoost handles it internally. |
| n_classes <- length(preds) / n_samples | ||
| # Reshape predictions into matrix (n_samples x n_classes) | ||
| pred_matrix <- matrix(preds, nrow = n_samples, ncol = n_classes, byrow = TRUE) |
| grad[i, labels[i] + 1] <- grad[i, labels[i] + 1] - 1 | ||
| } | ||
| hess <- pmax(2 * pred_matrix * (1 - pred_matrix), 1e-6) | ||
| return(list(grad = as.vector(t(grad)), hess = as.vector(t(hess)))) |
| # No need to transform, XGBoost handles it internally. | ||
| labels <- getinfo(dtrain, "label") | ||
| err <- sum(labels != preds) / length(labels) |
| params = list(tree_method = "hist", seed = 1994), | ||
| data = dtrain, | ||
| nrounds = 10, | ||
| obj = squared_log |
| disable_default_eval_metric = TRUE), | ||
| data = dtrain, | ||
| nrounds = kRounds, | ||
| obj = softprob_obj, |
| data = dtrain, | ||
| nrounds = kRounds, | ||
| obj = softprob_obj, | ||
| feval = merror_with_transform, | ||
| evals = list(train = dtrain) | ||
| ) |
| disable_default_eval_metric = TRUE), | ||
| data = dtrain, | ||
| nrounds = 10, | ||
| obj = squared_log, |
|
All 8 issues addressed in a7abc43 |
|
Hi @trivialfis, just checking in on this one. I've resolved all the issues raised in the initial review, and the branch is fully up to date with master with no conflicts. Ready for a human review whenever you have a moment. Thanks |
|
Hi @trivialfis, pushed a fix in 4f76e51 addressing the two remaining issues (removed evals_result from R xgb.train() and fixed merror() to treat preds as a pre-formed matrix). The branch is up to date and has no conflicts. There are 12 workflows awaiting maintainer approval to run CI — could you approve them when you get a chance? Thanks! |
Adds R code tabs alongside existing Python examples in the Custom Objective
and Evaluation Metric tutorial using the
.. tabs::/.. code-tab::Sphinxplugin introduced in #11410.
Changes:
multi-class examples
Contributes to #11413