PlotUtils.py 1.1 KB

1234567891011121314151617181920212223242526272829
  1. import matplotlib.pyplot as plt
  2. from sklearn.metrics import roc_curve, auc
  3. def plot_roc_curve(test_labels: list, test_df: list, title: str, figsize=(8, 8), savefile = None, show: bool = True):
  4. fpr, tpr, thresholds = roc_curve(test_labels, test_df)
  5. auc_score = auc(fpr, tpr)
  6. plt.figure(figsize=figsize)
  7. plt.plot(fpr, tpr, lw=1, label="ROC Curve")
  8. plt.plot([0, 1], [0, 1], color="lime", linestyle="--")
  9. plt.xlim([0.0, 1.0])
  10. plt.ylim([0.0, 1.05])
  11. plt.xlabel("FPR")
  12. plt.ylabel("TPR")
  13. plt.title(f"{title} (AUC = {auc_score})")
  14. plt.legend(loc="lower right")
  15. if savefile is not None:
  16. plt.savefig(f"{savefile}.png", bbox_inches="tight")
  17. plt.savefig(f"{savefile}.pdf", bbox_inches="tight")
  18. if show:
  19. plt.show()
  20. return fpr, tpr, thresholds, auc_score
  21. def get_percentiles(fpr, tpr, thresholds, percentiles=[0.9, 0.95, 0.98, 0.99]):
  22. for percentile in percentiles:
  23. for i, tp in enumerate(tpr):
  24. if tp >= percentile:
  25. print(f"{percentile} percentile : TPR = {tp:.4f}, FPR = {fpr[i]:.4f} <-> TNR = {(1 - fpr[i]):.4f} @ thresh {thresholds[i]}")
  26. break