-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_roc_curves.py
More file actions
48 lines (40 loc) · 1.95 KB
/
plot_roc_curves.py
File metadata and controls
48 lines (40 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn import model_selection
from sklearn.metrics import roc_curve, roc_auc_score
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV
def plot_roc_curve_and_auc(dataset_id, criterion):
# Load dataset
dataset = datasets.fetch_openml(data_id=dataset_id)
# Decision tree with specified criterion
decision_tree = DecisionTreeClassifier(criterion=criterion)
decision_tree.fit(dataset.data, dataset.target)
# Plot ROC curve and compute AUC value
plt.figure(figsize=(8, 6))
# Decision tree with specified criterion
y_scores = model_selection.cross_val_predict(decision_tree, dataset.data, dataset.target, method="predict_proba", cv=10)
fpr, tpr, _ = roc_curve(dataset.target, y_scores[:, 1], pos_label="1")
auc = roc_auc_score(dataset.target, y_scores[:, 1])
plt.plot(fpr, tpr, label=f"{criterion.capitalize()} (AUC = {auc:.2f})")
# Plot settings
plt.plot([0, 1], [0, 1], color='gray', linestyle='--')
plt.xlabel('1 - Specificity')
plt.ylabel('Sensitivity')
plt.title(f'ROC Curve - {criterion.capitalize()} - Dataset ID {dataset_id}')
plt.legend()
plt.grid(True)
plt.savefig(f"roc_curve_{dataset_id}_{criterion}.png", dpi=150, bbox_inches="tight")
plt.show()
# Print AUC value
print(f"AUC ({criterion.capitalize()}): {auc:.2f}")
# Parameter search on min_samples_leaf
parameters = [{"min_samples_leaf": [1, 10, 20, 50, 100]}]
tuned_dtc = GridSearchCV(decision_tree, parameters, scoring="roc_auc", cv=10)
tuned_dtc.fit(dataset.data, dataset.target)
print(f"Best parameters for {criterion} criterion:", tuned_dtc.best_params_)
if __name__ == "__main__":
plot_roc_curve_and_auc(4534, "entropy")
plot_roc_curve_and_auc(4534, "gini")
plot_roc_curve_and_auc(44, "entropy")
plot_roc_curve_and_auc(44, "gini")