【问题标题】:Classification report results分类报告结果
【发布时间】:2023-03-03 18:36:01
【问题描述】:

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

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 分数。我应该对所有分类报告都抱有同样的期望。

如果您发现我用于构建分类报告、分数和/或汇总表的参数有任何错误,请告诉我?

【问题讨论】:

    标签: python scikit-learn classification


    【解决方案1】:

    F1 分数与班级有着内在的联系。这就是为什么您的分类报告中有 2 个 F1 分数的原因。当您打印 f1_score(true, predicted) 时,它只会给您一个数字,根据 sklearn 的文档,该数字默认为分配为正的类的 f1 分数(来源:https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html,参数 > 平均值)。分类报告返回各种平均值,但是您包含的是 micro - f1 分数,它与之前的 f1 分数不同,是根据真阳性、假阴性和假阳性的总和计算得出的(如果您检查 https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html,在为类 2 提供的示例 micro f1 为 80%,因为 2 个“2”被正确分类为 2,另外 2 个其他实例被正确分类为“2”,一个“2”未被分类为“2”)。现在,如果您提供的第一个分数与最后一个分数不同,尽管它们都是由同一个 sklearn 函数调用的,那是因为第一个数字来自您数据的 CV 方案。

    【讨论】:

    • 您好 Gaussian Prior,谢谢您的回答。那么我在做什么错了吗?我应该认为哪个值更可靠?我需要创建一个表格,其中包含所有模型的分类报告中的所有值(精度、召回率、准确性和 f1 分数)。但是,如果我从分类报告中得到结果,然后我计算 f1, pres, ... 值是不同的。我怎样才能得到相同的结果?
    • 根据您的问题没有证据表明您做错了什么。哪个是最可靠的,没有明确的答案。有人可能会争辩说,由于你在一个类中有 580 个实例,而在另一个类中只有 80 个,你可以做 f1_score(y_true, y_pred, average='weighted') 只需指定你正在使用它以及为什么(因为类不平衡)。分类报告包括加权 f1 分数(最后一行(加权平均)),当 averate = 'weighted' 时,它应该等于 f1_score。
    猜你喜欢
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 2018-07-16
    • 2022-01-02
    • 2017-03-18
    • 2016-05-03
    相关资源
    最近更新 更多