首页 > 解决方案 > 缺少准确度数据的分类报告输出

问题描述

我正在上一门课程并做一些示例,我的输出出错了。

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))

这2个数据缺失

标签: pythonpython-3.xscikit-learn

解决方案


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

查看文档: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%。


推荐阅读