Skip to content

(R) Make 'predict' an S3 method #1657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions catboost/R-package/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
S3method(dim,catboost.Pool)
S3method(dimnames,catboost.Pool)
S3method(head,catboost.Pool)
S3method(predict,catboost.Model)
S3method(print,catboost.Model)
S3method(print,catboost.Pool)
S3method(summary,catboost.Model)
Expand All @@ -27,6 +28,7 @@ export(catboost.sum_models)
export(catboost.train)
export(catboost.virtual_ensembles_predict)
import(jsonlite)
importFrom(stats,predict)
importFrom(utils,download.file)
importFrom(utils,head)
importFrom(utils,tail)
Expand Down
40 changes: 31 additions & 9 deletions catboost/R-package/R/catboost.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#' @import jsonlite
#' @importFrom stats predict
#' @importFrom utils head
#' @importFrom utils tail
#' @importFrom utils write.table
Expand Down Expand Up @@ -1785,16 +1786,19 @@ catboost.save_model <- function(model, model_path,


#' @name catboost.predict
#' @title Apply the model
#' @title Get predictions from a CatBoost model
#'
#' @description Apply the model to the given dataset.
#' @description Get predictions from a CatBoost model on new data.
#' @details The function `catboost.predict` is a synonym for `predict.catboost.Model`,
#' which is an S3 method (i.e. called like `predict(model, newdata)`).
#'
#' Peculiarities: In case of multiclassification the prediction is returned in the form of a matrix.
#' Each line of this matrix contains the predictions for one object of the input dataset.
#' @param model The model obtained as the result of training.
#' In case of multiclassification the prediction is returned in the form of a matrix.
#' Each row of this matrix contains the predictions for one row of the input dataset.
#' @param object The model obtained as the result of training.
#'
#' Default value: Required argument
#' @param pool The input dataset.
#' @param newdata The input data on which to make predictions. Should be a `catboost.Pool`
#' object.
#'
#' Default value: Required argument
#' @param verbose Verbose output to stdout.
Expand Down Expand Up @@ -1828,9 +1832,12 @@ catboost.save_model <- function(model, model_path,
#' @return Vector of predictions (matrix for multi-class classification).
#' @export
#' @seealso \url{https://catboost.ai/docs/concepts/r-reference_catboost-predict.html}
catboost.predict <- function(model, pool,

predict.catboost.Model <- function(model, pool,
verbose = FALSE, prediction_type = "RawFormulaVal",
ntree_start = 0, ntree_end = 0, thread_count = -1) {
if (!inherits(model, "catboost.Model"))
stop("Expected catboost.Model, got: ", class(model))
if (!inherits(pool, "catboost.Pool"))
stop("Expected catboost.Pool, got: ", class(pool))
if (is.null.handle(pool))
Expand All @@ -1839,12 +1846,27 @@ catboost.predict <- function(model, pool,
catboost.restore_handle(model)
prediction <- .Call("CatBoostPredictMulti_R", model$cpp_obj$handle, pool,
verbose, prediction_type, ntree_start, ntree_end, thread_count)
if (length(prediction) != nrow(pool)) {
prediction <- matrix(prediction, nrow = nrow(pool), byrow = TRUE)
if (length(prediction) != nrow(newdata)) {
prediction <- matrix(prediction, nrow = nrow(newdata), byrow = TRUE)
}
return(prediction)
}

#' @rdname catboost.predict
#' @export
#' @usage catboost.predict(
#' object,
#' newdata,
#' verbose = FALSE,
#' prediction_type = "RawFormulaVal",
#' ntree_start = 0,
#' ntree_end = 0,
#' thread_count = -1
#' )
catboost.predict <- function(...) {
return(predict.catboost.Model(...))
}


#' @name catboost.staged_predict
#' @title Apply the model for each tree
Expand Down
32 changes: 24 additions & 8 deletions catboost/R-package/man/catboost.predict.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.