|
3 | 3 | Reproducing Example 7 from Sadinle et al. (2019)
|
4 | 4 | ================================================
|
5 | 5 |
|
6 |
| -We use :class:`~mapie.classification._MapieClassifier` to reproduce |
| 6 | +We use :class:`~mapie_v1.classification.SplitConformalClassifier` to reproduce |
7 | 7 | Example 7 from Sadinle et al. (2019).
|
8 | 8 |
|
9 | 9 | We consider a two-dimensional dataset with three labels. The distribution
|
|
12 | 12 | We model the data with Gaussian Naive Bayes classifier
|
13 | 13 | :class:`~sklearn.naive_bayes.GaussianNB` as a base model.
|
14 | 14 |
|
15 |
| -Prediction sets are estimated by :class:`~mapie.classification._MapieClassifier` |
16 |
| -from the distribution of the softmax scores of the true labels for three |
17 |
| -alpha values (0.2, 0.1, and 0.05) giving different class coverage levels. |
| 15 | +Prediction sets are estimated by |
| 16 | +:class:`~mapie_v1.classification.SplitConformalClassifier` from the distribution of the |
| 17 | +softmax scores of the true labels for three confidence level values (0.8, 0.9, and 0.95) |
| 18 | +giving different class coverage levels. |
18 | 19 |
|
19 | 20 | When the class coverage level is not large enough, the prediction sets can be
|
20 | 21 | empty.
|
|
25 | 26 | import numpy as np
|
26 | 27 | from sklearn.naive_bayes import GaussianNB
|
27 | 28 |
|
28 |
| -from mapie.classification import _MapieClassifier |
29 |
| -from mapie.conformity_scores import LACConformityScore |
| 29 | +from mapie.classification import SplitConformalClassifier |
30 | 30 |
|
31 | 31 | # Create training set from multivariate normal distribution
|
32 | 32 | centers = [(0, 3.5), (-2, 0), (2, 0)]
|
|
35 | 35 | x_min, x_max, y_min, y_max, step = -6, 8, -6, 8, 0.1
|
36 | 36 | n_samples = 500
|
37 | 37 | n_classes = 3
|
38 |
| -alpha = [0.2, 0.1, 0.05] |
| 38 | +confidence_level = [0.8, 0.9, 0.95] |
39 | 39 | np.random.seed(42)
|
40 | 40 | X_train = np.vstack(
|
41 | 41 | [
|
|
52 | 52 | )
|
53 | 53 | X_test = np.stack([xx.ravel(), yy.ravel()], axis=1)
|
54 | 54 |
|
55 |
| -# Apply _MapieClassifier on the dataset to get prediction sets |
56 |
| -clf = GaussianNB().fit(X_train, y_train) |
| 55 | +# Apply SplitConformalClassifier on the dataset to get prediction sets |
| 56 | +clf = GaussianNB() |
| 57 | +clf.fit(X_train, y_train) |
57 | 58 | y_pred = clf.predict(X_test)
|
58 | 59 | y_pred_proba = clf.predict_proba(X_test)
|
59 | 60 | y_pred_proba_max = np.max(y_pred_proba, axis=1)
|
60 |
| -mapie = _MapieClassifier( |
| 61 | +mapie = SplitConformalClassifier( |
61 | 62 | estimator=clf,
|
62 |
| - cv="prefit", |
63 |
| - conformity_score=LACConformityScore() |
| 63 | + confidence_level=confidence_level, |
| 64 | + prefit=True, |
| 65 | + conformity_score="lac" |
64 | 66 | )
|
65 |
| -mapie.fit(X_train, y_train) |
66 |
| -y_pred_mapie, y_ps_mapie = mapie.predict(X_test, alpha=alpha) |
| 67 | +mapie.conformalize(X_train, y_train) |
| 68 | +y_pred_mapie, y_ps_mapie = mapie.predict_set(X_test) |
67 | 69 |
|
68 | 70 | # Plot the results
|
69 | 71 | tab10 = plt.cm.get_cmap("Purples", 4)
|
|
84 | 86 | edgecolor="k",
|
85 | 87 | )
|
86 | 88 | axs[0].set_title("Predicted labels")
|
87 |
| -for i, alpha_ in enumerate(alpha): |
| 89 | +for i, confidence_level_ in enumerate(confidence_level): |
88 | 90 | y_ps_sums = y_ps_mapie[:, :, i].sum(axis=1)
|
89 | 91 | num_labels = axs[i + 1].scatter(
|
90 | 92 | X_test[:, 0],
|
|
98 | 100 | vmax=3,
|
99 | 101 | )
|
100 | 102 | cbar = plt.colorbar(num_labels, ax=axs[i + 1])
|
101 |
| - axs[i + 1].set_title(f"Number of labels for alpha={alpha_}") |
| 103 | + axs[i + 1].set_title(f"Number of labels for confidence_level={confidence_level_}") |
102 | 104 | plt.show()
|
0 commit comments