|
3 | 3 | Example use of the prefit parameter with neural networks |
4 | 4 | ======================================================== |
5 | 5 |
|
6 | | -:class:`mapie.regression.MapieRegressor` and |
| 6 | +:class:`mapie.regression.MapieRegressor` and |
7 | 7 | :class:`mapie.quantile_regression.MapieQuantileRegressor`` |
8 | | -are used to calibrate uncertainties for large models for |
9 | | -which the cost of cross-validation is too high. Typically, |
| 8 | +are used to calibrate uncertainties for large models for |
| 9 | +which the cost of cross-validation is too high. Typically, |
10 | 10 | neural networks rely on a single validation set. |
11 | 11 |
|
12 | 12 | In this example, we first fit a neural network on the training set. We |
13 | 13 | then compute residuals on a validation set with the `cv="prefit"` parameter. |
14 | 14 | Finally, we evaluate the model with prediction intervals on a testing set. |
15 | 15 | """ |
16 | 16 |
|
17 | | -from cProfile import label |
18 | | -import subprocess |
19 | 17 |
|
20 | | -subprocess.run("pip install lightgbm", shell=True) |
21 | | - |
22 | | -import scipy |
23 | | -from lightgbm import LGBMRegressor |
24 | 18 | import numpy as np |
25 | | -from sklearn.model_selection import train_test_split |
| 19 | +from lightgbm import LGBMRegressor |
26 | 20 | from matplotlib import pyplot as plt |
| 21 | +import scipy |
| 22 | +from sklearn.model_selection import train_test_split |
27 | 23 |
|
28 | 24 | from mapie.regression import MapieRegressor |
29 | 25 | from mapie.quantile_regression import MapieQuantileRegressor |
@@ -87,15 +83,15 @@ def f(x: NDArray) -> NDArray: |
87 | 83 | estimator = LGBMRegressor( |
88 | 84 | objective='quantile', |
89 | 85 | alpha=0.5, |
90 | | -) |
| 86 | +) # Note that this is the same model as used for QR |
91 | 87 | estimator.fit(X_train.reshape(-1, 1), y_train) |
92 | 88 | list_estimators.append(estimator) |
93 | 89 |
|
94 | 90 | # Calibrate uncertainties on validation set |
95 | 91 | mapie_cqr = MapieQuantileRegressor(list_estimators, cv="prefit") |
96 | 92 | mapie_cqr.fit(X_val.reshape(-1, 1), y_val) |
97 | 93 | y_pred_cqr, y_pis_cqr = mapie_cqr.predict(X_test.reshape(-1, 1)) |
98 | | -y_pred_low_cqr, y_pred_up_cqr = y_pis[:, 0, 0], y_pis[:, 1, 0] |
| 94 | +y_pred_low_cqr, y_pred_up_cqr = y_pis_cqr[:, 0, 0], y_pis_cqr[:, 1, 0] |
99 | 95 | coverage_cqr = regression_coverage_score(y_test, y_pred_low_cqr, y_pred_up_cqr) |
100 | 96 |
|
101 | 97 | # Plot obtained prediction intervals on testing set |
@@ -127,14 +123,14 @@ def f(x: NDArray) -> NDArray: |
127 | 123 | X_test[order], |
128 | 124 | y_pred_low[order], |
129 | 125 | y_pred_up[order], |
130 | | - alpha=0.2, |
| 126 | + alpha=0.4, |
131 | 127 | label="prediction intervals QR" |
132 | 128 | ) |
133 | 129 | plt.fill_between( |
134 | 130 | X_test[order], |
135 | 131 | y_pred_low_cqr[order], |
136 | 132 | y_pred_up_cqr[order], |
137 | | - alpha=0.2, |
| 133 | + alpha=0.4, |
138 | 134 | label="prediction intervals CQR" |
139 | 135 | ) |
140 | 136 | plt.title( |
|
0 commit comments