5.2 分类器的评价指标—accuracy、precision、recall、F1、Fβ、AUC与ROC

1. 分类器评价指标公式

在这里插入图片描述
在这里插入图片描述
AUC与ROC

2.实例

#!/usr/bin/python
# -*- coding:utf-8 -*-

import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score, recall_score, f1_score, fbeta_score
from sklearn.metrics import precision_recall_fscore_support, classification_report

if __name__ == "__main__":
    y_true = np.array([1, 1, 1, 1, 0, 0])
    y_hat = np.array([1, 0, 1, 1, 1, 1])
    print('Accuracy:\t', accuracy_score(y_true, y_hat))

    precision = precision_score(y_true, y_hat)
    print('Precision:\t', precision)

    recall = recall_score(y_true, y_hat)
    print('Recall:  \t', recall)

    print('f1 score: \t', f1_score(y_true, y_hat))
    print(2 * (precision * recall) / (precision + recall))

    print('F-beta:')
    for beta in np.logspace(-3, 3, num=7, base=10):
        fbeta = fbeta_score(y_true, y_hat, beta=beta)
        print('\tbeta=%9.3f\tF-beta=%.5f' % (beta, fbeta))

    print(precision_recall_fscore_support(y_true, y_hat, beta=1))
    print(classification_report(y_true, y_hat))

Accuracy:	 0.5
Precision:	 0.6
Recall:  	 0.75
f1 score: 	 0.6666666666666665
0.6666666666666665
F-beta:
	beta=    0.001	F-beta=0.60000
	beta=    0.010	F-beta=0.60001
	beta=    0.100	F-beta=0.60119
	beta=    1.000	F-beta=0.66667
	beta=   10.000	F-beta=0.74815
	beta=  100.000	F-beta=0.74998
	beta= 1000.000	F-beta=0.75000
(array([0. , 0.6]), array([0.  , 0.75]), array([0.        , 0.66666667]), array([2, 4], dtype=int64))
              precision    recall  f1-score   support

           0       0.00      0.00      0.00         2
           1       0.60      0.75      0.67         4

    accuracy                           0.50         6
   macro avg       0.30      0.38      0.33         6
weighted avg       0.40      0.50      0.44         6

猜你喜欢

转载自blog.csdn.net/weixin_46649052/article/details/112750040
5.2