1

ためScikit-学ぶ(パイソン)異なるメトリック結果(F1スコア)は次のとおりです。StratifiedKFold

def best_classifier(clf,k,x,y): 

    skf = StratifiedKFold(n_splits=k,shuffle=True) 

    bestclf = None 
    bestf1 = 0 
    bestsplit = [] 
    cnt = 1 
    totalf1 = 0 

    for train_index,test_index in skf.split(x,y): 
     x_train,x_test = x[train_index],x[test_index] 
     y_train,y_test = y[train_index],y[test_index] 
     clf.fit(x_train,y_train) 
     predicted_y = clf.predict(x_test) 
     f1 = f1_score(y_test,predicted_y) 
     totalf1 = totalf1+f1 
     print(y_test.shape) 

     print(cnt," iteration f1 score",f1) 
     if cnt==10: 
      avg = totalf1/10 
      print(avg) 
     if f1>bestf1: 
      bestf1 = f1 
      bestclf = clf 
      bestsplit = [train_index,test_index] 

     cnt = cnt+1 
    return [bestclf,bestf1,bestsplit] 

best_of_best = best_classifier(sgd,10,x_selected,y) 

を今私がCA以降:この機能は私に(最高のスプリットに適合した)私の分類器の配列、最高f1scoreと、次のように私はそれを呼び出す

最良の分割のインデックスを返します。ベストスプリットと私のクラシファイアを確認する私は同じスプリットのためにそれをもう一度テストして、機能の中に入ったのと同じ結果を得ているかどうかをチェックするだけです。しかし明らかにそうではありません。 コード:

bestclf= best_of_best[0] 
test_index = best_of_best[2][1] 
x_cv = x_selected[test_index] 
y_cv = y[test_index] 
pred_cv = bestclf.predict(x_cv) 
f1_score(y_cv,pred_cv) 

結果メソッドがbest_classifierであると呼ばれている:我々は、このF1を見ることができるように、私はstatifiedkfold

0.86181818181818182 

の最良のスプリット外予測

(679,) 
1 iteration f1 score 0.643298969072 
(679,) 
2 iteration f1 score 0.761750405186 
(678,) 
3 iteration f1 score 0.732773109244 
(678,) 
4 iteration f1 score 0.632911392405 
(678,) 
5 iteration f1 score 0.74179743224 
(678,) 
6 iteration f1 score 0.749140893471 
(677,) 
7 iteration f1 score 0.750830564784 
(677,) 
8 iteration f1 score 0.756756756757 
(677,) 
9 iteration f1 score 0.682170542636 
(677,) 
10 iteration f1 score 0.63813229572 
0.708956236151 

結果スコアは10倍では観測されません。どうしてですか?何か間違っているのですか?私の方法論は間違っていますか?

+1

。それをFalseに設定するとどうなりますか? 'shuffle = True'を保ち、' random_state = 1'をセットして、各繰り返しを同じシャッフルにすることもできます。 –

+0

Nopeはそれを試しましたが動作しませんでした。シャッフル= Trueを設定しても、シャッフルごとに分割インデックスを取得します。 – Kaushal

答えて

0

解決済み。問題は、clfオブジェクトをbestclfに深くコピーしていないためです。私が深いコピーではなかったので、私のbestclf参照を実行するために使用されたK番目のフォールドが現在のclfに変更されるたびに。私は `シャッフル= true`には、各` skf.split`前にデータをシャッフル考えるsklearnのStratifiedKFoldについてはあまり知らず

bestclf = copy.deepcopy(clf) 
関連する問題