From f94479c3a6fb09edf03ce57f2c8526703cbfcb60 Mon Sep 17 00:00:00 2001 From: Amar Date: Tue, 8 Mar 2022 22:23:41 +0530 Subject: [PATCH 1/3] update notebook-style for plot_calibration.py --- examples/calibration/plot_calibration.py | 33 +++++++++++++++++------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/examples/calibration/plot_calibration.py b/examples/calibration/plot_calibration.py index ea1871a7104d8..1b15aced1302c 100644 --- a/examples/calibration/plot_calibration.py +++ b/examples/calibration/plot_calibration.py @@ -22,13 +22,15 @@ Brier score. """ - -# Author: Mathieu Blondel -# Alexandre Gramfort -# Balazs Kegl -# Jan Hendrik Metzen +# Author: +# Mathieu Blondel +# Alexandre Gramfort +# Balazs Kegl +# Jan Hendrik Metzen # License: BSD Style. +# %% + import numpy as np import matplotlib.pyplot as plt from matplotlib import cm @@ -40,6 +42,9 @@ from sklearn.model_selection import train_test_split +# %% +# Generate synthetic dataset +# -------------------------- n_samples = 50000 n_bins = 3 # use 3 bins for calibration_curve as we have 3 clusters here @@ -58,17 +63,23 @@ X, y, sample_weight, test_size=0.9, random_state=42 ) -# Gaussian Naive-Bayes with no calibration +# %% +# Gaussian Naive-Bayes +# -------------------- + +# %% + +# With no calibration clf = GaussianNB() clf.fit(X_train, y_train) # GaussianNB itself does not support sample-weights prob_pos_clf = clf.predict_proba(X_test)[:, 1] -# Gaussian Naive-Bayes with isotonic calibration +# With isotonic calibration clf_isotonic = CalibratedClassifierCV(clf, cv=2, method="isotonic") clf_isotonic.fit(X_train, y_train, sample_weight=sw_train) prob_pos_isotonic = clf_isotonic.predict_proba(X_test)[:, 1] -# Gaussian Naive-Bayes with sigmoid calibration +# With sigmoid calibration clf_sigmoid = CalibratedClassifierCV(clf, cv=2, method="sigmoid") clf_sigmoid.fit(X_train, y_train, sample_weight=sw_train) prob_pos_sigmoid = clf_sigmoid.predict_proba(X_test)[:, 1] @@ -84,8 +95,9 @@ clf_sigmoid_score = brier_score_loss(y_test, prob_pos_sigmoid, sample_weight=sw_test) print("With sigmoid calibration: %1.3f" % clf_sigmoid_score) -# ############################################################################# -# Plot the data and the predicted probabilities +# %% +# Plot data and the predicted probabilities +# ----------------------------------------- plt.figure() y_unique = np.unique(y) colors = cm.rainbow(np.linspace(0.0, 1.0, y_unique.size)) @@ -105,6 +117,7 @@ plt.title("Data") plt.figure() + order = np.lexsort((prob_pos_clf,)) plt.plot(prob_pos_clf[order], "r", label="No calibration (%1.3f)" % clf_score) plt.plot( From d22a0fdbbf767e44bd7b191b29d2e2591dc66fbf Mon Sep 17 00:00:00 2001 From: Amar Date: Sun, 13 Mar 2022 22:27:43 +0530 Subject: [PATCH 2/3] Move imports closer to their use --- examples/calibration/plot_calibration.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/examples/calibration/plot_calibration.py b/examples/calibration/plot_calibration.py index 1b15aced1302c..ccfdb9d256415 100644 --- a/examples/calibration/plot_calibration.py +++ b/examples/calibration/plot_calibration.py @@ -30,21 +30,13 @@ # License: BSD Style. # %% - +# Generate synthetic dataset +# -------------------------- import numpy as np -import matplotlib.pyplot as plt -from matplotlib import cm from sklearn.datasets import make_blobs -from sklearn.naive_bayes import GaussianNB -from sklearn.metrics import brier_score_loss -from sklearn.calibration import CalibratedClassifierCV from sklearn.model_selection import train_test_split - -# %% -# Generate synthetic dataset -# -------------------------- n_samples = 50000 n_bins = 3 # use 3 bins for calibration_curve as we have 3 clusters here @@ -66,8 +58,9 @@ # %% # Gaussian Naive-Bayes # -------------------- - -# %% +from sklearn.calibration import CalibratedClassifierCV +from sklearn.metrics import brier_score_loss +from sklearn.naive_bayes import GaussianNB # With no calibration clf = GaussianNB() @@ -98,6 +91,9 @@ # %% # Plot data and the predicted probabilities # ----------------------------------------- +from matplotlib import cm +import matplotlib.pyplot as plt + plt.figure() y_unique = np.unique(y) colors = cm.rainbow(np.linspace(0.0, 1.0, y_unique.size)) From 51d24cc2cadaa569e4f9ff696c0c2a1ce58c8a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20du=20Boisberranger?= <34657725+jeremiedbb@users.noreply.github.com> Date: Thu, 17 Mar 2022 11:25:24 +0100 Subject: [PATCH 3/3] Update examples/calibration/plot_calibration.py --- examples/calibration/plot_calibration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/calibration/plot_calibration.py b/examples/calibration/plot_calibration.py index ccfdb9d256415..75d1ea15b8fbd 100644 --- a/examples/calibration/plot_calibration.py +++ b/examples/calibration/plot_calibration.py @@ -22,7 +22,7 @@ Brier score. """ -# Author: +# Authors: # Mathieu Blondel # Alexandre Gramfort # Balazs Kegl