マルチクラス分類問題に NB 分類子を使用する次のコードがあります。この関数は、精度を保存し、後で平均を出力することにより、相互検証を実行します。代わりに私が欲しいのは、最終的に平均精度スコアではなく、クラスごとの精度と再現率を指定する分類レポートです。
import random
from sklearn import cross_validation
from sklearn.naive_bayes import MultinomialNB
def multinomial_nb_with_cv(x_train, y_train):
random.shuffle(X)
kf = cross_validation.KFold(len(X), n_folds=10)
acc = []
for train_index, test_index in kf:
y_true = y_train[test_index]
clf = MultinomialNB().fit(x_train[train_index],
y_train[train_index])
y_pred = clf.predict(x_train[test_index])
acc.append(accuracy_score(y_true, y_pred))
相互検証を実行しない場合、私がしなければならないことは次のとおりです。
from sklearn.metrics import classification_report
from sklearn.naive_bayes import MultinomialNB
def multinomial_nb(x_train, y_train, x_test, y_test):
clf = MultinomialNB().fit(x_train, y_train)
y_pred = clf.predict(x_test)
y_true = y_test
print classification_report(y_true, y_pred)
そして、次のようなレポートが表示されます。
precision recall f1-score support
0 0.50 0.24 0.33 221
1 0.00 0.00 0.00 18
2 0.00 0.00 0.00 27
3 0.00 0.00 0.00 28
4 0.00 0.00 0.00 32
5 0.04 0.02 0.02 57
6 0.00 0.00 0.00 26
7 0.00 0.00 0.00 25
8 0.00 0.00 0.00 43
9 0.00 0.00 0.00 99
10 0.63 0.98 0.76 716
avg / total 0.44 0.59 0.48 1292
クロスバリデーションでも同様のレポートを取得するにはどうすればよいですか?