代码如下:
#!/usr/bin/env python # coding: utf-8 import sys from sklearn.metrics import roc_curve, auc, precision_score, recall_score, f1_score import matplotlib.pyplot as plt yTrue = list() yPred = list() yPredBinary = list() for line in open(sys.argv[1]): splits = line.strip().split("\t") if len(splits) < 2: continue label = [0, 1][int(splits[0])] yTrue.append(label) yPred.append(float(splits[1])) yPredBinary.append(float(splits[1]) > 0.5) precision = precision_score(yTrue, yPredBinary) recall = recall_score(yTrue, yPredBinary) f1 = f1_score(yTrue, yPredBinary) fpr, tpr, thresholds = roc_curve(yTrue, yPred) auc = auc(fpr, tpr) print("precision:\t" + str(precision)) print("recall:\t" + str(recall)) print("f1:\t" + str(f1)) print("auc:\t" + str(auc)) plt.plot(fpr, tpr, lw=1, label='ROC (area = %0.3f)' % auc) plt.show()