Advertisement
gk231192

Module 9.2 Model Comparison

Jul 3rd, 2025
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. models = {
  2.     'SVM': SVC(kernel='linear', probability=True),
  3.     'Naive Bayes': GaussianNB(),
  4.     'Decision Tree': DecisionTreeClassifier(random_state=42),
  5.     'Random Forest': RandomForestClassifier(n_estimators=100, random_state=42),
  6.     'Gradient Boosting': GradientBoostingClassifier(n_estimators=100, random_state=42)
  7. }
  8.  
  9. # Plot ROC curves
  10. plt.figure(figsize=(12, 8))
  11. for name, model in models.items():
  12.     model.fit(X_train_scaled, y_train)
  13.     if hasattr(model, "predict_proba"):
  14.         probs = model.predict_proba(X_test_scaled)[:, 1]
  15.     else:
  16.         probs = model.decision_function(X_test_scaled)
  17.     fpr, tpr, _ = roc_curve(y_test, probs)
  18.     auc_score = roc_auc_score(y_test, probs)
  19.     plt.plot(fpr, tpr, label=f'{name} (AUC = {auc_score:.2f})')
  20.  
  21. # Reference line for random guess
  22. plt.plot([0, 1], [0, 1], 'k--')
  23.  
  24. plt.title('ROC Curves of ML Models (Titanic Example)', fontsize=14)
  25. plt.xlabel('False Positive Rate')
  26. plt.ylabel('True Positive Rate')
  27. plt.legend()
  28. plt.grid(True)
  29. plt.tight_layout()
  30. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement