Using models exported as C++ code

Purpose

Apply the model in C++ format. The method is available within the output C++ file with the model description.

Note.
  • The ApplyCatboostModel method is inferior in performance compared to the native CatBoost application methods, especially on large models and datasets.

  • Multiclassification models are not currently supported.

Method call format

For datasets that contain only numerical features:

double ApplyCatboostModel(const std::vector<float>& features);

For datasets that contain both numerical and categorical features:

double ApplyCatboostModel(const std::vector<float>& floatFeatures, const std::vector<std::string>& catFeatures);
Parameters
ParameterPossible typesDescription
features (floatFeatures)float

The list of numerical features.

catFeaturesstring

The list of categorical features.

Note.

Numerical and categorical features must be passed separately in the same order they appear in the train dataset.

For example, let's assume that the train dataset contains the following features:

  • Numerical features: f1, f3
  • Categorical features: f2, f4

In this case, the following code must be used to apply the model:

ApplyCatboostModel(floatFeatures = {f1, f3}, catFeatures = {f2, f4})
Type of return value

Prediction of the model for the object with given features.

The result is identical to the code below but does not require the libcatboostmodel.<so|dll|dylib> linking:
  • For datasets that contain only numerical features:

    #include <catboost/libs/model_interface/wrapped_calcer.h>
    double ApplyCatboostModel(const std::vector<float>& features) {
        ModelCalcerWrapper calcer("model.cbm");
        return calcer.Calc(features, {});
    }
  • For datasets that contain both numerical and categorical features:

    #include <catboost/libs/model_interface/wrapped_calcer.h>
    double ApplyCatboostModel(const std::vector<float>& floatFeatures, const std::vector<std::string>& catFeatures) {
        ModelCalcerWrapper calcer("model.cbm");
        return calcer.Calc(floatFeatures, catFeatures);
    }
Compilers
  • For datasets that contain only numerical features:

    C++11 with support of non-static data member initializers and extended initializer lists.

  • For datasets that contain both numerical and categorical features:

    C++14 compiler with aggregate member initialization support. Tested with the following compilers:
    • Clang++ 3.8
    • g++ 5.4.1 20160904