2016-02-15 10 views
5

Próbuję użyć f-score z scikit-learn jako metryki oceny w klasyfikatorze xgb. Tu jest mój kodu:Użycie f-score w xgb

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) 

Ale jest błąd:

Can't handle mix of binary and continuous

Odpowiedz

3

Problemem jest to, że f1_score próbuje porównać non-binarny vs. binarnych celów i domyślnie metoda ta robi binarny uśrednianie. Od documentation "średnia: string, [None," binary "(domyślnie)," micro "," macro "," samples "," weighted "]".

W każdym razie, jest to informacja, że ​​twoja prognoza jest podobna do tej [0.001, 0.7889,0.33...], a twój cel jest binarny [0,1,0...]. Więc jeśli znasz swój próg, zalecam, abyś przetworzył swój wynik przed wysłaniem go do funkcji f1_score. Zwykła wartość progu wynosiłaby 0.5.

Testowany przykład funkcji oceny. Nie wyświetla już błędu wyjściowego:

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)