python - 缺少准确度数据的分类报告输出
问题描述
我正在上一门课程并做一些示例,我的输出出错了。
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))
解决方案
这就是分类报告返回文本摘要的方式,没有任何遗漏。
查看文档: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%。
推荐阅读
- identifier - 由于“未解析的标识符”错误,工作表视图结构崩溃
- c++ - 是否可以在 C++ 项目中模拟外部 C 库函数?
- ios - Swift Scale Animation 不是动画视图
- reactjs - 如何在反应中更新状态的对象数据
- django - 为什么 django 模板 if 语句工作错误?
- latex - 在乳胶中的 TOC 标题前添加一些空格
- git - 在一个分支上提交更改是否会从其他分支中丢弃它们?
- r - 使用 facet_grid 更改标签的问题
- payara - Payara 服务器无法启动,需要主密码才能启动服务器
- react-native - 如何在反应中使用导航,这将适用于网络和移动设备?