使用10折交叉验证获取多项式朴素贝叶斯的类别精确度和召回率分类报告

我有一段代码,使用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

在进行交叉验证的情况下,如何获得类似的报告?


回答:

您可以使用cross_val_predict来生成交叉验证预测,然后使用classification_report

from sklearn.datasets import make_classificationfrom sklearn.cross_validation import cross_val_predictfrom sklearn.naive_bayes import GaussianNBfrom sklearn.metrics import classification_report# 生成一些具有11个类别的模拟数据X, y = make_classification(n_samples=2000, n_features=20, n_informative=10, n_classes=11, random_state=0)# 您的分类器,这里假设使用GaussianNB来处理非整数数据Xestimator = GaussianNB()# 使用10折分层抽样生成交叉验证预测y_pred = cross_val_predict(estimator, X, y, cv=10)y_pred.shapeOut[91]: (2000,)# 生成报告print(classification_report(y, y_pred))             precision    recall  f1-score   support          0       0.47      0.36      0.41       181          1       0.38      0.46      0.41       181          2       0.45      0.53      0.48       182          3       0.29      0.45      0.35       183          4       0.37      0.33      0.35       183          5       0.40      0.44      0.42       182          6       0.27      0.13      0.17       183          7       0.47      0.44      0.45       182          8       0.34      0.27      0.30       182          9       0.41      0.44      0.42       179         10       0.42      0.41      0.41       182avg / total       0.39      0.39      0.38      2000

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注