Search code examples
pythonmachine-learningcatboost

How to specify more than one eval_metric for a CatBoostRegressor?


I want to specify more than one evaluation metric for my CatBoostRegressor:

model=catboost.CatBoostRegressor(eval_metric=['RMSE', 'MAE', 'R2'])

So I can get the results very simple with the .get_best_score() method, but it does not accept the metrics in a list. Is there any way to do this? I could not figure it out nor find an answer. I know that it is easy to solve another way, but I want to know if this can be done with a different input format for the metrics or something or it is not supported. Thank you in advance!


Solution

  • You should pass the list of evaluation metrics to custom_metric instead of eval_metric:

    from catboost import CatBoostRegressor
    from sklearn.datasets import make_regression
    from sklearn.model_selection import train_test_split
    
    # generate the data
    X, y = make_regression(n_samples=100, n_features=10, random_state=0)
    
    # split the data
    X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state=0)
    
    # fit the model
    model = CatBoostRegressor(iterations=10, custom_metric=['RMSE', 'MAE', 'R2'])
    model.fit(X=X_train, y=y_train, eval_set=(X_valid, y_valid), silent=True)
    
    # get the best score
    print(model.get_best_score())
    
    # {'learn': {
    #     'MAE': 42.36387514896515, 
    #     'R2': 0.9398622316668792, 
    #     'RMSE': 54.878286259899525
    #  },
    # 'validation': {
    #     'MAE': 102.37559908734613, 
    #     'R2': 0.6989698975428136, 
    #     'RMSE': 134.75006267018009
    #  }}