catboostclassifier 中 class_weights 的使用

Avi*_*ava 2 catboost catboostregressor

如何在将 CatboostClassifier 用于 Multiclass 问题时使用“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)

小智 5

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

假设我们有 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)
Run Code Online (Sandbox Code Playgroud)


小智 2

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

\n\n
model_multiclass = CatBoostClassifier(iterations=1000,\n                       depth=4,\n                       learning_rate=0.05,\n                       loss_function='MultiClass',\n                       verbose=True,\n                       early_stopping_rounds = 200,\n                       bagging_temperature = 1,\n                       metric_period = 100)\n\nmodel_multiclass.fit(X_train, Y_train)\nmodel_multiclass.classes_\nResult:['35\xd0\xbc\xd1\x80', '4\xd0\xbc\xd1\x80', '\xd0\xb2\xd1\x8b\xd0\xb2\xd0\xbe\xd0\xb4 \xd0\xbd\xd0\xb0 \xd0\x98\xd0\x9f', '\xd0\xb2\xd1\x8b\xd0\xb2\xd0\xbe\xd0\xb4 \xd0\xbd\xd0\xb0 \xd0\xba\xd0\xba', '\xd0\xb2\xd1\x8b\xd0\xb2\xd0\xbe\xd0\xb4 \xd0\xbd\xd0\xb0 \xd1\x84\xd0\xbb', '\xd1\x82\xd1\x80\xd0\xb0\xd0\xbd\xd0\xb7\xd0\xb8\xd1\x82']\n
Run Code Online (Sandbox Code Playgroud)\n