Xgboost分類器に異なるクラスの重みを設定する方法はありますか?たとえば、sklearn RandomForestClassifierでは、これは「class_weight」パラメーターによって実行されます。
sklearnラッパーを使用する場合、重みのパラメーターがあります。
例:
import xgboost as xgb
exgb_classifier = xgboost.XGBClassifier()
exgb_classifier.fit(X, y, sample_weight=sample_weights_data)
ここで、パラメータは、ターゲットの長さに等しい長さNのような配列である必要があります
私は最近この問題に遭遇したので、考えてみた解決策が残る
from xgboost import XGBClassifier
# manually handling imbalance. Below is same as computing float(18501)/392318
on the trainig dataset.
# We are going to inversely assign the weights
weight_ratio = float(len(y_train[y_train == 0]))/float(len(y_train[y_train ==
1]))
w_array = np.array([1]*y_train.shape[0])
w_array[y_train==1] = weight_ratio
w_array[y_train==0] = 1- weight_ratio
xgc = XGBClassifier()
xgc.fit(x_df_i_p_filtered, y_train, sample_weight=w_array)
理由はわかりませんが、結果はかなり期待外れでした。これが誰かを助けることを願っています。
[参照リンク] https://www.programcreek.com/python/example/99824/xgboost.XGBClassifier
列車データの各インスタンスにクラスの重みを割り当てるだけです。まず、sklearnのclass_weight.compute_class_weight
を使用してクラスの重みを取得し、次に列車データの各行に適切な重みを割り当てます。
ここでは、列車データにクラス番号を含む列「class」があると想定しています。また、1からnb_classesまでのnb_classesがあると仮定しました。
from sklearn.utils import class_weight
class_weights = list(class_weight.compute_class_weight('balanced',
np.unique(train['class']),
train['class']))
w_array = np.ones(y_train.shape[0], dtype = 'float')
for i, val in enumerate(y_train):
w_array[i] = class_weights[val-1]
xgb_classifier.fit(X, y, sample_weight=w_array)