【问题标题】:Usage of class_weights in catboostclassifier在 catboostclassifier 中使用 class_weights
【发布时间】:2019-12-25 04:35:09
【问题描述】:

在使用 CatboostClassifier 解决多类问题时如何使用“class_weights”。文档说它应该是一个列表,但是我需要按什么顺序放置权重?我有一个标签数组,包含从 -2 到 +2 的 15 个类,包括十进制数,与其他类相比,0 类的密度要高得多。 请帮忙。 谢谢,

我尝试了二进制类,它更容易使用,但对多类一无所知。

cb_model_step1 = run_catboost(X_train, y_train_new, X_test, y_test_new, n_estimators = 1000, verbose=100, eta = 0.3, loss_function = 'MultiClassOneVsAll', class_weights = counter_new)

cb = CatBoostClassifier(thread_count=4, n_estimators=n_estimators, max_depth=10, class_weights = class_weights, eta=eta, loss_function = loss_function)

【问题讨论】:

  • 另外,对于具有不平衡权重的 2 类输出,它是如何工作的?

标签: catboost catboostregressor


【解决方案1】:

现在可以传递带有标签和相应权重的字典。

假设我们有 X_train、y_train 和多分类问题。那么我们就可以进行以下操作了

import numpy as np 
from catboost import CatBoostClassifier
from sklearn.utils.class_weight import compute_class_weight
 
classes = np.unique(y_train)
weights = compute_class_weight(class_weight='balanced', classes=classes, y=y_train)
class_weights = dict(zip(classes, weights))

clf = CatBoostClassifier(loss_function='MultiClassOneVsAll', class_weights=class_weights)
clf.fit(X_train, y_train)

【讨论】:

  • 此外,对于具有不平衡权重的 2 类输出,它是如何工作的?如果有 2 个类,它会出错。
【解决方案2】:

您需要在旅游数据集上拟合没有任何权重的模型,然后运行 ​​CatBoostClassifier().classes_。它会在 catboost 中向您显示课程顺序:

model_multiclass = CatBoostClassifier(iterations=1000,
                       depth=4,
                       learning_rate=0.05,
                       loss_function='MultiClass',
                       verbose=True,
                       early_stopping_rounds = 200,
                       bagging_temperature = 1,
                       metric_period = 100)

model_multiclass.fit(X_train, Y_train)
model_multiclass.classes_
Result:['35мр', '4мр', 'вывод на ИП', 'вывод на кк', 'вывод на фл', 'транзит']

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 2023-02-17
    • 2019-12-10
    • 2023-04-09
    • 2021-07-13
    • 2021-07-28
    • 2020-09-09
    • 2021-03-16
    • 2021-02-08
    相关资源
    最近更新 更多