import numpy as np
from sklearn import metrics
y_true = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
y_scores = np.array([0.1, 0.1,0.1,0.1,0.1,0.1, 0.1, 0.1, 0.1, 0.9, 0.9])
print(metrics.roc_auc_score(y_true, y_scores))
print(metrics.recall_score(y_true,(y_scores>0.5).astype(int)))
print(metrics.precision_score(y_true,(y_scores>0.5).astype(int)))
print(metrics.f1_score(y_true,(y_scores>0.5).astype(int)))
print("---")
y_true = np.array([0, 0, 0, 0, 0, 1])
y_scores = np.array([0.1, 0.1, 0.1, 0.1, 0.9, 0.9])
print(metrics.roc_auc_score(y_true, y_scores))
print(metrics.recall_score(y_true,(y_scores>0.5).astype(int)))
print(metrics.precision_score(y_true,(y_scores>0.5).astype(int)))
print(metrics.f1_score(y_true,(y_scores>0.5).astype(int)))
print("---")
y_true = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,1])
y_scores = np.array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.9, 0.9, 0.9])
print(metrics.roc_auc_score(y_true, y_scores))
print(metrics.recall_score(y_true,(y_scores>0.5).astype(int)))
print(metrics.precision_score(y_true,(y_scores>0.5).astype(int)))
print(metrics.f1_score(y_true,(y_scores>0.5).astype(int)))
打印结果
0.9500000000000001
1.0
0.5
0.6666666666666666
---
0.9
1.0
0.5
0.6666666666666666
---
0.9500000000000001
1.0
0.6666666666666666
0.8