|
| 1 | +""" |
| 2 | +====================================================== |
| 3 | +Use MAPIE to plot prediction sets |
| 4 | +====================================================== |
| 5 | +
|
| 6 | +In this example, we explain how to use MAPIE on a basic classification setting. |
| 7 | +""" |
| 8 | + |
| 9 | +################################################################################## |
| 10 | +# We will use MAPIE to estimate prediction sets on a two-dimensional dataset with |
| 11 | +# three labels. |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +from sklearn.neighbors import KNeighborsClassifier |
| 15 | +from sklearn.datasets import make_blobs |
| 16 | +from matplotlib import pyplot as plt |
| 17 | +from matplotlib.colors import ListedColormap |
| 18 | +from mapie.utils import train_conformalize_test_split |
| 19 | +from mapie.classification import SplitConformalClassifier |
| 20 | +from mapie.metrics.classification import classification_coverage_score |
| 21 | + |
| 22 | +np.random.seed(42) |
| 23 | + |
| 24 | +############################################################################## |
| 25 | +# Firstly, let us create our dataset: |
| 26 | + |
| 27 | +X, y = make_blobs(n_samples=500, n_features=2, centers=3, cluster_std=3.4) |
| 28 | + |
| 29 | +(X_train, X_conformalize, X_test, |
| 30 | + y_train, y_conformalize, y_test) = train_conformalize_test_split( |
| 31 | + X, y, train_size=0.4, conformalize_size=0.4, test_size=0.2 |
| 32 | +) |
| 33 | + |
| 34 | +############################################################################## |
| 35 | +# We fit our training data with a KNN estimator. |
| 36 | +# Then, we initialize a :class:`~mapie.classification.SplitConformalClassifier` |
| 37 | +# using our estimator, indicating that it has already been fitted with |
| 38 | +# `prefit=True`. |
| 39 | +# Lastly, we compute the prediction sets with the desired confidence level using the |
| 40 | +# ``conformalize`` and ``predict_set`` methods. |
| 41 | + |
| 42 | +classifier = KNeighborsClassifier(n_neighbors=10) |
| 43 | +classifier.fit(X_train, y_train) |
| 44 | + |
| 45 | +confidence_level = 0.95 |
| 46 | +mapie_classifier = SplitConformalClassifier( |
| 47 | + estimator=classifier, confidence_level=confidence_level, prefit=True |
| 48 | +) |
| 49 | +mapie_classifier.conformalize(X_conformalize, y_conformalize) |
| 50 | +y_pred, y_pred_set = mapie_classifier.predict_set(X_test) |
| 51 | + |
| 52 | +############################################################################## |
| 53 | +# ``y_pred`` represents the point predictions as a ``np.ndarray`` of shape |
| 54 | +# ``(n_samples)``. |
| 55 | +# ``y_pred_set`` corresponds to the prediction sets as a ``np.ndarray`` of shape |
| 56 | +# ``(n_samples, 3, 1)``. This array contains only boolean values: ``True`` if the label |
| 57 | +# is included in the prediction set, and ``False`` if not. |
| 58 | + |
| 59 | +############################################################################## |
| 60 | +# Finally, we can easily compute the coverage score (i.e., the proportion of times the |
| 61 | +# true labels fall within the predicted sets). |
| 62 | + |
| 63 | +coverage_score = classification_coverage_score(y_test, y_pred_set) |
| 64 | +print(f"For a confidence level of {confidence_level:.2f}, " |
| 65 | + f"the target coverage is {confidence_level:.3f}, " |
| 66 | + f"and the effective coverage is {coverage_score[0]:.3f}.") |
| 67 | + |
| 68 | +############################################################################## |
| 69 | +# In this example, the effective coverage is slightly above the target coverage |
| 70 | +# (i.e., 0.95), indicating that the confidence level we set has been reached. |
| 71 | +# Therefore, we can confirm that the prediction sets effectively contain the |
| 72 | +# true label more than 95% of the time. |
| 73 | + |
| 74 | +############################################################################## |
| 75 | +# Now, let us plot the confidence regions across the plane. |
| 76 | +# This plot will give us insights about what the prediction set looks like for each |
| 77 | +# point. |
| 78 | + |
| 79 | +x_min, x_max = np.min(X[:, 0]), np.max(X[:, 0]) |
| 80 | +y_min, y_max = np.min(X[:, 1]), np.max(X[:, 1]) |
| 81 | +step = 0.1 |
| 82 | + |
| 83 | +xx, yy = np.meshgrid(np.arange(x_min, x_max, step), np.arange(y_min, y_max, step)) |
| 84 | +X_test_mesh = np.stack([xx.ravel(), yy.ravel()], axis=1) |
| 85 | + |
| 86 | +y_pred_set = mapie_classifier.predict_set(X_test_mesh)[1][:, :, 0] |
| 87 | + |
| 88 | +cmap_back = ListedColormap( |
| 89 | + [(0.7803921568627451, 0.9137254901960784, 0.7529411764705882), |
| 90 | + (0.9921568627450981, 0.8156862745098039, 0.6352941176470588), |
| 91 | + (0.6196078431372549, 0.6039215686274509, 0.7843137254901961), |
| 92 | + (0.7764705882352941, 0.8588235294117647, 0.9372549019607843), |
| 93 | + (0.6196078431372549, 0.6039215686274509, 0.7843137254901961), |
| 94 | + (0.6196078431372549, 0.6039215686274509, 0.7843137254901961)] |
| 95 | +) |
| 96 | +cmap_dots = ListedColormap( |
| 97 | + [(0.19215686274509805, 0.5098039215686274, 0.7411764705882353), |
| 98 | + (0.9019607843137255, 0.3333333333333333, 0.050980392156862744), |
| 99 | + (0.19215686274509805, 0.6392156862745098, 0.32941176470588235)] |
| 100 | +) |
| 101 | + |
| 102 | +plt.scatter( |
| 103 | + X_test_mesh[:, 0], X_test_mesh[:, 1], |
| 104 | + c=np.ravel_multi_index(y_pred_set.T, (2, 2, 2)), |
| 105 | + cmap=cmap_back, marker='.', s=10 |
| 106 | +) |
| 107 | +plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_dots) |
| 108 | +plt.xlabel("x1") |
| 109 | +plt.ylabel("x2") |
| 110 | +plt.title("Confidence regions with KNN") |
| 111 | +plt.show() |
| 112 | + |
| 113 | +############################################################################## |
| 114 | +# On the plot above, the dots represent the samples from our dataset, with their |
| 115 | +# color indicating their respective label. |
| 116 | +# The blue, orange and green zones correspond to prediction sets |
| 117 | +# containing only the blue label, orange label and green label respectively. |
| 118 | +# The purple zone represents areas where the prediction sets contain more than one |
| 119 | +# label, indicating that the model is uncertain. |
0 commit comments