Scikit分类报告 - 更改显示结果的格式

Cri*_*a23 6 python classification machine-learning svm scikit-learn

Scikit分类报告仅显示两位数的精确度和召回分数.是否有可能使它在点后显示4位数,我的意思是代替0.67显示0.6783?

 from sklearn.metrics import classification_report
 print classification_report(testLabels, p, labels=list(set(testLabels)), target_names=['POSITIVE', 'NEGATIVE', 'NEUTRAL'])
                     precision    recall  f1-score   support

         POSITIVE       1.00      0.82      0.90     41887
         NEGATIVE       0.65      0.86      0.74     19989
         NEUTRAL        0.62      0.67      0.64     10578
Run Code Online (Sandbox Code Playgroud)

另外,我应该担心精度得分为1.00吗?谢谢!

Cen*_*tAu 18

我刚刚遇到了这个老问题.确实可以有更多的精确点classification_report.你只需要传递一个digits参数.

classification_report(y_true, y_pred, target_names=target_names, digits=4)
Run Code Online (Sandbox Code Playgroud)

文档:

digits:int格式化输出浮点值的位数

示范:

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))
Run Code Online (Sandbox Code Playgroud)

输出:

       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

avg / total       0.70      0.60      0.61         5
Run Code Online (Sandbox Code Playgroud)

有4位数字:

print(classification_report(y_true, y_pred, target_names=target_names, digits=4))
Run Code Online (Sandbox Code Playgroud)

输出:

             precision    recall  f1-score   support

    class 0     0.5000    1.0000    0.6667         1
    class 1     0.0000    0.0000    0.0000         1
    class 2     1.0000    0.6667    0.8000         3

avg / total     0.7000    0.6000    0.6133         5
Run Code Online (Sandbox Code Playgroud)


alf*_*lfa 5

不,无法使用 来显示更多数字classification_report。格式字符串是硬编码的,请参见此处

编辑:有更新,请参阅 CentAu 的答案