【问题标题】:classification_report output with missing accuracy data缺少准确度数据的分类报告输出
【发布时间】:2020-03-27 06:13:44
【问题描述】:

我正在学习一门课程并做一些示例,但我的输出有误。

import pandas as pd 

df = pd.read_csv(r'E:\Python Projects\Python-Data-Science-and-Machine-Learning-Bootcamp\Machine Learning\Árvores de decisão e Florestas Aleatórias\kyphosis.csv')

from sklearn.model_selection import train_test_split

x = df.drop('Kyphosis', axis=1)
y = df['Kyphosis']

X_train, X_test, y_train, y_test = train_test_split(x,y,test_size=0.33)

from sklearn.tree import DecisionTreeClassifier

dtree = DecisionTreeClassifier()
dtree.fit(X_train, y_train)
pred = dtree.predict(X_test)

from sklearn.metrics import classification_report

print(classification_report(y_test, pred))

These 2 data are missing

【问题讨论】:

    标签: python python-3.x scikit-learn


    【解决方案1】:

    这是分类报告返回文本摘要的方式,没有任何遗漏。

    查看文档:https://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html

    >>> from sklearn.metrics import classification_report
    >>> y_true = [0, 1, 2, 2, 2]
    >>> y_pred = [0, 0, 2, 2, 1]
    >>> target_names = ['class 0', 'class 1', 'class 2']
    >>> print(classification_report(y_true, y_pred, target_names=target_names))
                  precision    recall  f1-score   support
    <BLANKLINE>
         class 0       0.50      1.00      0.67         1
         class 1       0.00      0.00      0.00         1
         class 2       1.00      0.67      0.80         3
    <BLANKLINE>
        accuracy                           0.60         5
       macro avg       0.50      0.56      0.49         5
    weighted avg       0.70      0.60      0.61         5
    <BLANKLINE>
    >>> y_pred = [1, 1, 0]
    >>> y_true = [1, 1, 1]
    >>> print(classification_report(y_true, y_pred, labels=[1, 2, 3]))
                  precision    recall  f1-score   support
    <BLANKLINE>
               1       1.00      0.67      0.80         3
               2       0.00      0.00      0.00         0
               3       0.00      0.00      0.00         0
    <BLANKLINE>
       micro avg       1.00      0.67      0.80         3
       macro avg       0.33      0.22      0.27         3
    weighted avg       1.00      0.67      0.80         3
    <BLANKLINE>
    

    报告的平均值包括宏观平均值(平均每个标签的未加权平均值)、加权平均值(平均每个标签的支持加权平均值)和样本平均值(仅适用于多标签分类)。微平均(对总的真阳性、假阴性和假阳性进行平均)仅针对多标签或具有类别子集的多类别显示,因为它对应于其他方面的准确性。

    您的准确率仅为 74%。

    【讨论】:

      【解决方案2】:

      您的分类报告没有遗漏任何内容; scikit-learn 的一个特点是它选择在那里显示准确度,但没有“精确度准确度”或“召回准确度”。您的实际准确度是在f1-score 列下显示的;这是来自documentation 的玩具数据示例:

      from sklearn.metrics import classification_report
      y_true = [0, 1, 2, 2, 2]
      y_pred = [0, 0, 2, 2, 1]
      target_names = ['class 0', 'class 1', 'class 2']
      print(classification_report(y_true, y_pred, target_names=target_names))
      

      结果:

                    precision    recall  f1-score   support
      
           class 0       0.50      1.00      0.67         1
           class 1       0.00      0.00      0.00         1
           class 2       1.00      0.67      0.80         3
      
          accuracy                           0.60         5
         macro avg       0.50      0.56      0.49         5
      weighted avg       0.70      0.60      0.61         5
      

      即这里的准确率是0.6,你可以直接验证:

      from sklearn.metrics import accuracy_score
      accuracy_score(y_true, y_pred)
      # 0.6
      

      不过,您说得对,这很奇怪,而且肯定会令人困惑。不是一个很好的设计选择...

      【讨论】:

        猜你喜欢
        • 2020-01-10
        • 2014-11-24
        • 1970-01-01
        • 2011-09-05
        • 1970-01-01
        • 2018-07-26
        • 1970-01-01
        • 2019-06-06
        • 1970-01-01
        相关资源
        最近更新 更多