我有以下代码,它使用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))
Run Code Online (Sandbox Code Playgroud)
如果我不进行交叉验证,我所要做的就是:
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)
Run Code Online (Sandbox Code Playgroud)
它给了我一个这样的报告:
precision recall f1-score support
0 0.50 0.24 0.33 221
1 0.00 0.00 0.00 …
Run Code Online (Sandbox Code Playgroud)