Skip to content

Commit e088e85

Browse files
committed
add quick start risk control
1 parent d8057c6 commit e088e85

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
:caption: Control prediction errors
2525

2626
theoretical_description_risk_control
27+
examples_risk_control/1-quickstart/plot_risk_control_binary_classification
2728
examples_risk_control/index
2829
external_risk_control_package
2930

doc/quick_start.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,10 @@ Here, we generate one-dimensional noisy data that we fit with a MLPRegressor: `U
4040
3. Classification
4141
=======================
4242

43-
Similarly, it's possible to do the same for a basic classification problem: `Use MAPIE to plot prediction sets <https://mapie.readthedocs.io/en/stable/examples_classification/1-quickstart/plot_quickstart_classification.html>`_
43+
Similarly, it's possible to do the same for a basic classification problem: `Use MAPIE to plot prediction sets <https://mapie.readthedocs.io/en/stable/examples_classification/1-quickstart/plot_quickstart_classification.html>`_
44+
45+
46+
4. Risk Control
47+
=======================
48+
49+
MAPIE implements risk control methods for multilabel classification (in particular, image segmentation) and binary classification: `Use MAPIE to control risk for a binary classifier <https://mapie.readthedocs.io/en/stable/examples_risk_control/1-quickstart/plot_risk_control_binary_classification.html>`_
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"""
2+
=================================================
3+
Use MAPIE to control risk for a binary classifier
4+
=================================================
5+
6+
In this example, we explain how to do risk control for binary classification with MAPIE.
7+
8+
"""
9+
10+
import numpy as np
11+
import matplotlib.pyplot as plt
12+
from sklearn.datasets import make_circles
13+
from sklearn.svm import SVC
14+
from sklearn.model_selection import FixedThresholdClassifier
15+
from sklearn.metrics import precision_score
16+
from sklearn.inspection import DecisionBoundaryDisplay
17+
18+
from mapie.risk_control import BinaryClassificationController, precision
19+
from mapie.utils import train_conformalize_test_split
20+
21+
RANDOM_STATE = 1
22+
23+
##############################################################################
24+
# Let us first load the dataset and fit an SVC on the training data.
25+
26+
X, y = make_circles(n_samples=3000, noise=0.3,
27+
factor=0.3, random_state=RANDOM_STATE)
28+
(X_train, X_calib, X_test,
29+
y_train, y_calib, y_test) = train_conformalize_test_split(
30+
X, y, train_size=0.8, conformalize_size=0.1, test_size=0.1,
31+
random_state=RANDOM_STATE)
32+
33+
clf = SVC(probability=True, random_state=RANDOM_STATE)
34+
clf.fit(X_train, y_train)
35+
36+
##############################################################################
37+
# Next, we initialize a :class:`~mapie.risk_control.BinaryClassificationController`
38+
# using the probability estimation function from the fitted estimator:
39+
# ``clf.predict_proba``, a risk function (here the precision), a target risk level, and
40+
# a confidence level. Then we use the calibration data to compute statistically
41+
# guaranteed thresholds using a risk control method.
42+
43+
target_precision = 0.8
44+
bcc = BinaryClassificationController(
45+
clf.predict_proba, precision, target_level=target_precision, confidence_level=0.9)
46+
bcc.calibrate(X_calib, y_calib)
47+
48+
print(f'{len(bcc.valid_predict_params)} valid thresholds found. '
49+
f'The best one is {bcc.best_predict_param:.3f}.')
50+
51+
52+
##############################################################################
53+
# In the plot below, we visualize how the threshold values impact precision, and what
54+
# thresholds have been computed as statistically guaranteed.
55+
56+
proba_positive_class = clf.predict_proba(X_calib)[:, 1]
57+
58+
tested_thresholds = bcc._predict_params
59+
precisions = np.full(len(tested_thresholds), np.inf)
60+
for i, threshold in enumerate(tested_thresholds):
61+
y_pred = (proba_positive_class >= threshold).astype(int)
62+
precisions[i] = precision_score(y_calib, y_pred)
63+
64+
valid_thresholds_indices = np.array(
65+
[t in bcc.valid_predict_params for t in tested_thresholds])
66+
best_threshold_index = np.where(
67+
tested_thresholds == bcc.best_predict_param)[0][0]
68+
69+
plt.figure()
70+
plt.scatter(tested_thresholds[valid_thresholds_indices],
71+
precisions[valid_thresholds_indices], c='tab:green',
72+
label='Valid thresholds')
73+
plt.scatter(tested_thresholds[~valid_thresholds_indices],
74+
precisions[~valid_thresholds_indices], c='tab:red',
75+
label='Invalid thresholds')
76+
plt.scatter(tested_thresholds[best_threshold_index], precisions[best_threshold_index],
77+
c='tab:green', label='Best threshold', marker='*', edgecolors='k', s=300)
78+
plt.axhline(target_precision, color='tab:gray', linestyle='--')
79+
plt.text(0, target_precision+0.02, 'Target precision',
80+
color='tab:gray', fontstyle='italic')
81+
plt.xlabel('Threshold', labelpad=15)
82+
plt.ylabel('Precision')
83+
plt.legend()
84+
plt.show()
85+
86+
##############################################################################
87+
# Contrary to the naive way of computing a threshold to satisfy a precision target on
88+
# calibration data, risk control provides statistical guarantees on unseen data.
89+
# Besides computing a set of valid thresholds,
90+
# :class:`~mapie.risk_control.BinaryClassificationController` also outputs the best
91+
# one, which in the case of precision is the threshold that, among all valid ones,
92+
# maximizes recall.
93+
#
94+
# In the figure above, the highest threshold values are considered invalid due to the
95+
# small number of observations used to compute the precision, following the Learn then
96+
# Test procedure. In the most extreme case, no observation is available, which causes
97+
# the precision value to be ill-defined and set to 0.
98+
#
99+
# After obtaining the best threshold, we can use the ``predict`` function of
100+
# :class:`~mapie.risk_control.BinaryClassificationController` for future predictions,
101+
# or use scikit-learn's ``FixedThresholdClassifier`` as a wrapper to benefit
102+
# from functionalities like easily plotting the decision boundary as seen below.
103+
104+
y_pred = bcc.predict(X_test)
105+
106+
clf_threshold = FixedThresholdClassifier(clf, threshold=bcc.best_predict_param)
107+
# necessary for plotting, alternatively you can use sklearn.frozen.FrozenEstimator
108+
clf_threshold.fit(X_train, y_train)
109+
110+
disp = DecisionBoundaryDisplay.from_estimator(
111+
clf_threshold, X_test, response_method="predict", cmap=plt.cm.coolwarm)
112+
113+
plt.scatter(X_test[y_test == 0, 0], X_test[y_test == 0, 1],
114+
edgecolors='k', c='tab:blue', alpha=0.5, label='"negative" class')
115+
plt.scatter(X_test[y_test == 1, 0], X_test[y_test == 1, 1],
116+
edgecolors='k', c='tab:red', alpha=0.5, label='"positive" class')
117+
plt.title("Decision Boundary of FixedThresholdClassifier")
118+
plt.xlabel("Feature 1")
119+
plt.ylabel("Feature 2")
120+
plt.legend()
121+
plt.show()
122+
123+
##############################################################################
124+
# Different risk functions have been implemented, such as precision and recall, but you
125+
# can also implement your own custom function using
126+
# :class:`~mapie.risk_control.BinaryClassificationRisk`.

0 commit comments

Comments
 (0)