save_model

Save the model to a file.

Method call format

save_model(fname, 
           format="cbm", 
           export_parameters=None,
           pool=None)

Parameters

ParameterPossible typesDescriptionDefault value
fnamestring

The path to the output model.

Required parameter
formatstring

The output format of the model.

Possible values:
  • cbm — CatBoost binary format.
  • coreml — Apple CoreML format (only datasets without categorical features are supported).
  • json — JSON format. Refer to the CatBoost JSON model tutorial for format details.
  • python — Standalone Python code (multiclassification models are not currently supported). See the Using models exported as Python code section for details on applying the resulting model.
  • cpp — Standalone C++ code (multiclassification models are not currently supported). See the Using models exported as C++ code section for details on applying the resulting model.
cbm
export_parametersdict

Additional parameters for the Apple CoreML export.

Possible values (all are strings):
  • prediction_type. Possible values are “probability ”and “raw”.

  • coreml_description

  • coreml_model_version

  • coreml_model_author

  • coreml_model_license

None
pool
  • catboost.Pool
  • list
  • numpy.array
  • pandas.DataFrame
  • pandas.Series
  • catboost.FeaturesData

The dataset previously used for training.

This parameter is required if the model contains categorical features and the output format is python or JSON.
Note.

The model can be saved to the JSON format without a pool. In this case it is available for review but it is not applicable.

None

Export the model to Apple CoreML

To export the model to Apple CoreML for further usage on iOS devices:
  1. Train the model and save it in CoreML format.

    For example, if training on the Iris dataset:
    import catboost
    from sklearn import datasets
    
    iris = datasets.load_iris()
    cls = catboost.CatBoostClassifier(loss_function='MultiClass')
    
    cls.fit(iris.data, iris.target)
    
    # Save model to catboost format
    cls.save_model("iris.mlmodel", format="coreml", export_parameters={'prediction_type': 'probability'})
  2. Import the resulting model to XCode.

    The following is an example of importing with Swift:
    import CoreML
    
    let model = iris()
    let sepal_l = 7.0
    let sepal_w = 3.2
    let petal_l = 4.7
    let petal_w = 1.4
    
    guard let output = try? model.prediction(input: irisInput(feature_0: sepal_l, feature_1: sepal_w, feature_2: petal_l, feature_3: petal_w)) else {
        fatalError("Unexpected runtime error.")
    }
    
    print(String(
        format: "Output probabilities: %1.5f; %1.5f; %1.5f",
        output.prediction[0].doubleValue,
        output.prediction[1].doubleValue,
        output.prediction[2].doubleValue
    ))