Predict with CatBoost Model and Return JSON Results

  • Share this:

Code introduction


This function loads a CatBoost model from a given path and makes predictions using the input features. It then returns the predictions in JSON format.


Technology Stack : CatBoost, json

Code Type : Prediction function

Code Difficulty : Intermediate


                
                    
def predict_with_catboost(model_path, input_features):
    """
    This function predicts the output using a CatBoost model loaded from a given model path.
    """
    import catboost as cb
    import json

    # Load the CatBoost model
    model = cb.CatBoostModel.load(model_path)

    # Make predictions using the loaded model
    predictions = model.predict(input_features)

    # Convert predictions to a JSON string
    predictions_json = json.dumps(predictions.tolist())

    return predictions_json                
              
Tags: