2016-02-15 14 views
5

xgbクラシファイアの評価基準としてscikit-learnのf-スコアを使用しようとしています。ここに私のコードは次のとおりです。xgbのf-スコアを使用

clf = xgb.XGBClassifier(max_depth=8, learning_rate=0.004, 
          n_estimators=100, 
          silent=False, objective='binary:logistic', 
          nthread=-1, gamma=0, 
          min_child_weight=1, max_delta_step=0, subsample=0.8, 
          colsample_bytree=0.6, 
          base_score=0.5, 
          seed=0, missing=None) 
scores = [] 
predictions = [] 
for train, test, ans_train, y_test in zip(trains, tests, ans_trains, ans_tests): 
     clf.fit(train, ans_train, eval_metric=xgb_f1, 
        eval_set=[(train, ans_train), (test, y_test)], 
        early_stopping_rounds=900) 
     y_pred = clf.predict(test) 
     predictions.append(y_pred) 
     scores.append(f1_score(y_test, y_pred)) 

def xg_f1(y, t): 
    t = t.get_label() 
    return "f1", f1_score(t, y) 

しかし、エラーがあります:

Can't handle mix of binary and continuous

答えて

3

問題は、この方法は、バイナリいf1_scoreバイナリターゲット対非バイナリを比較しようとしていることであり、デフォルトで平均化する。 documentation "平均:文字列、[なし、 'バイナリ'(デフォルト)、 'マイクロ'、 'マクロ'、 'サンプル'、 '加重']"。

とにかく、あなたの予測が[0.001, 0.7889,0.33...]のようなもので、ターゲットがバイナリ[0,1,0...]であることを伝えています。だからあなたがあなたの閾値を知っているなら、あなたは結果をf1_score関数に送る前に前処理することをお勧めします。しきい値の通常の値は0.5です。

評価機能のテスト例です。もうエラーを出力しない:

def xg_f1(y,t): 
    t = t.get_label() 
    y_bin = [1. if y_cont > 0.5 else 0. for y_cont in y] # binaryzing your output 
    return 'f1',f1_score(t,y_bin) 
関連する問題