Python Catboost:多类F1分数自定义度量

2024-09-27 09:32:37 发布

您现在位置:Python中文网/ 问答频道 /正文

如何找到多类Catboost分类器每个类的F1分数?我已经通读了documentationgithub repo,有人问了同样的问题。然而,我无法找出实现这一目标的编码技术。我知道我必须在CatBoostClassifier()中使用custom_metric参数,但我不知道当我想要多类数据集的每个类的F1分数时custom_metric可以接受哪些参数

假设您有一个玩具数据集(来自文档):

from catboost import Pool
cat_features = [0, 1, 2]
data = [["a","b", 1, 4, 5, 6],
        ["a","b", 4, 5, 6, 7],
        ["c","d", 30, 40, 50, 60]]

label = [0, 1, 2]

from sklearn.model_selection import train_test_split    
X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2)
train_pool = Pool(X_train, y_train, cat_features=categorical_features_indices)
validate_pool = Pool(X_test, y_test, cat_features=categorical_features_indices)
params = {"loss_function": "MultiClass",
          "depth": symmetric_tree_depth,
          "num_trees": 500,
#           "eval_metric": "F1", # this doesn't work
          "verbose": False}

model = CatBoostClassifier(**params)
model.fit(train_pool, eval_set=validate_pool)

Tags: 数据fromtest参数modelcustomtrainmetric

热门问题