Created
April 13, 2022 16:36
-
-
Save rasbt/7236bd6ac818f1aa6ab0a869a98db88c to your computer and use it in GitHub Desktop.
Classifier Calibration -- A Minimal Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.datasets import make_classification | |
from sklearn.model_selection import cross_val_score | |
from sklearn.tree import DecisionTreeClassifier | |
from sklearn.calibration import CalibratedClassifierCV | |
X, y = make_classification( | |
n_samples=1000, n_features=5, n_redundant=2, | |
n_clusters_per_class=1, random_state=123) | |
model = DecisionTreeClassifier(random_state=123) | |
scores = cross_val_score(model, X, y, scoring='roc_auc', cv=10, n_jobs=-1) | |
print(f'Orig ROC AUC: {np.mean(scores):.3f}') | |
calibrated = CalibratedClassifierCV(model, method='isotonic', cv=10) | |
scores = cross_val_score(calibrated, X, y, scoring='roc_auc', cv=10, n_jobs=-1) | |
print(f'Calibrated ROC AUC: {np.mean(scores):.3f}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment