MAPIE (Mapping Prediction Intervals)
MAPIE (Mapping Prediction Intervals) is a scikit-learn-compatible Python library for estimating prediction intervals. It provides tools for both regression and classification tasks, leveraging conformal prediction methods to quantify uncertainty. The current version is 1.3.0, and the library maintains an active release cadence with several major and minor updates per year.
Common errors
-
ImportError: cannot import name 'MapieRegressor' from 'mapie'
cause Attempting to import `MapieRegressor` or `MapieClassifier` directly from the top-level `mapie` package after the v1.0.0 API rework.fixUpdate your import statement to use the correct submodule, e.g., `from mapie.regression import MapieRegressor` or `from mapie.classification import MapieClassifier`. -
TypeError: 'BinaryClassificationRisk' object is not callable
cause Passing an instantiated `BinaryClassificationRisk` object to `BinaryClassificationController` after the v1.2.0 update, which now expects a string for built-in risks.fixPass the risk name as a string, e.g., `BinaryClassificationController(risk_score_function='fdr', ...)` instead of `BinaryClassificationController(risk_score_function=BinaryClassificationRisk('fdr'), ...)`. -
RuntimeError: Python version 3.8.x is not supported by MAPIE. Please use Python 3.9 or newer.
cause Running MAPIE on an unsupported Python version.fixUpgrade your Python environment to version 3.9 or higher.
Warnings
- breaking The v1.0.0 release introduced a major API rework, significantly changing import paths and the public API for classification and regression modules. Code written for versions prior to v1.0.0 will likely break.
- breaking The risk control API in v1.2.0 changed how built-in risks are specified. Previously, users might have passed `BinaryClassificationRisk` objects, but now string representations are expected for built-in risks.
- gotcha MAPIE requires Python >=3.9. Attempting to install or run MAPIE on older Python versions will result in an error.
- gotcha MAPIE has specific scikit-learn version requirements (currently >=1.0, <1.4). Using an incompatible scikit-learn version may lead to runtime errors or unexpected behavior.
Install
-
pip install mapie
Imports
- MapieRegressor
from mapie import MapieRegressor
from mapie.regression import MapieRegressor
- MapieClassifier
from mapie import MapieClassifier
from mapie.classification import MapieClassifier
- MapieTimeSeriesRegressor
from mapie.time_series_regression import MapieTimeSeriesRegressor
- BinaryClassificationController
from mapie.risk_control import BinaryClassificationController
Quickstart
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_regression
from mapie.regression import MapieRegressor
# 1. Generate synthetic data
X, y = make_regression(n_samples=500, n_features=1, noise=20, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# 2. Fit a MAPIE regressor
regressor = LinearRegression()
mapie_regressor = MapieRegressor(regressor, cv="split", random_state=42)
mapie_regressor.fit(X_train, y_train)
# 3. Predict prediction intervals
y_pred, y_pis = mapie_regressor.predict(X_test, alpha=0.1)
# 4. Print results (example)
print(f"Predicted value for first test sample: {y_pred[0]:.2f}")
print(f"Prediction interval for first test sample: [{y_pis[0, 0, 0]:.2f}, {y_pis[0, 1, 0]:.2f}]")