fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn.metrics import roc_curve, auc
  4.  
  5. # Example true labels and predicted probabilities
  6. # Replace these with your actual data
  7. y_true = np.array([0, 0, 1, 1, 0, 1, 0, 1, 1, 0]) # True labels
  8. y_scores = np.array([0.1, 0.4, 0.35, 0.8, 0.7, 0.9, 0.2, 0.65, 0.85, 0.5]) # Predicted probabilities
  9.  
  10. # Calculate TPR and FPR
  11. fpr, tpr, thresholds = roc_curve(y_true, y_scores)
  12. roc_auc = auc(fpr, tpr) # Calculate AUC
  13.  
  14. # Plotting the ROC curve
  15. plt.figure(figsize=(8, 6))
  16. plt.plot(fpr, tpr, color='blue', lw=2, label='ROC curve (AUC = {:.2f})'.format(roc_auc))
  17. plt.plot([0, 1], [0, 1], color='grey', linestyle='--') # Diagonal line (random chance)
  18. plt.xlim([0.0, 1.0])
  19. plt.ylim([0.0, 1.05])
  20. plt.xlabel('False Positive Rate')
  21. plt.ylabel('True Positive Rate')
  22. plt.title('Receiver Operating Characteristic (ROC) Curve')
  23. plt.legend(loc='lower right')
  24. plt.grid()
  25. plt.show()
Success #stdin #stdout 3.19s 95672KB
stdin
Standard input is empty
stdout
Standard output is empty