分类报告结果

Classification report results

我认为我的参数存在一些问题,因为我得到了不同的结果。由于代码量巨大,我将无法全部复制粘贴,只能复制粘贴相关部分。 我正在使用不同的模型来预测一个帐户是否是假的。 模型示例如下:

rf = Pipeline([
        ('rfCV',FeaturesSelection.countVect),
        ('rf_clf',RandomForestClassifier(n_estimators=200,n_jobs=3))
        ])
    
rf.fit(DataPreparation.train_acc['Acc'],DataPreparation.train_acc['Label'])
predicted_rf = rf.predict(DataPreparation.test_acc['Acc'])
np.mean(predicted_rf == DataPreparation.test_acc['Label'])

Then I use K-Fold cross validation: 
def confusion_matrix(classifier):
    
    k_fold = KFold(n_splits=5)
    scores = []
    confusion = np.array([[0,0],[0,0]])

    for train_ind, test_ind in k_fold.split(DataPreparation.train_acc):
        train_text = DataPreparation.train_acc.iloc[train_ind]['Acc'] 
        train_y = DataPreparation.train_acc.iloc[train_ind]['Label']
    
        test_text = DataPreparation.train_acc.iloc[test_ind]['Acc']
        test_y = DataPreparation.train_acc.iloc[test_ind]['Label']
        
        classifier.fit(train_text,train_y)
        predictions = classifier.predict(test_text)
        
        confusion += confusion_matrix(test_y,predictions)
        score = f1_score(test_y,predictions)
        scores.append(score)

        return (print('Score:', sum(scores)/len(scores)))

将其应用于所有分类器

build_confusion_matrix(nb_pipeline)
build_confusion_matrix(svm_pipeline)
build_confusion_matrix(rf)

我得到:

Score: 0.5697
Score: 0.5325
Score: 0.5857

但是,如果我想按如下方式创建分类报告:

print(classification_report(DataPreparation.test_acc['Label'], predicted_nb))
print(classification_report(DataPreparation.test_acc['Label'], predicted_svm))
print(classification_report(DataPreparation.test_acc['Label'], predicted_rf))

输出不同。例如: (注意)

               precision    recall  f1-score   support

     0.0       0.97      0.86      0.91       580
     1.0       0.41      0.72      0.53        80

(支持向量机)

               precision    recall  f1-score   support

     0.0       0.94      0.96      0.95       580
     1.0       0.61      0.53      0.52        80

如果我创建一个总结报告如下:

f1 = f1_score(DataPreparation.test_acc['Label'], predicted_rf)
pres = precision_score(DataPreparation.test_acc['Label'], predicted_rf)
rec = recall_score(DataPreparation.test_acc['Label'], predicted_rf)
acc = accuracy_score(DataPreparation.test_acc['Label'], predicted_rf)
    
res = res.append({'Precision': pres, 
                     'Recall': rec, 'F1-score': f1, 'Accuracy': acc}, ignore_index = True)

我也得到了不同的结果。

我正在查看 f1 分数。我应该对所有分类报告都有同样的期望。

如果您发现我用于构建分类报告、分数、and/or 摘要的参数有任何错误,请告诉我 table?

F1 分数与 class 有着内在的联系。这就是为什么您的 class化验报告中有 2 个 F1 分数。当你打印 f1_score(true, predicted) 它只给你一个数字,根据 sklearn 的文档,它默认为 class 的 f1 分数,它被指定为正数(来源:https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html, parameters > average). The classification report returns all kinds of average, however the one you included is micro - f1 score which differs from the previous f1 score and is calculated based on the total True Positives, False Negatives and False Positives (if you check https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html ,在提供的示例中,class 2 的 micro f1 是 80%,因为 2 个 '2' 被 class 正确化为 2,而其他 2 个实例被 class 正确化而不是 '2' 和一个“2”未 class 化为“2”)。现在,如果您提供的第一个分数与最后一个分数不同,尽管它们都是由相同的 sklearn 函数调用的,那是因为第一个数字是从您的数据的 CV 方案中得出的。