mapie.mondrian
.MondrianCP¶
- class mapie.mondrian.MondrianCP(mapie_estimator: Union[MapieClassifier, MapieRegressor])[source]¶
Mondrian is a method for making conformal predictions for partition of individuals.
The Mondrian method is implemented in the MondrianCP class. It takes as input a MapieClassifier or MapieRegressor estimator and fits a model for each group of individuals. The MondrianCP class can then be used to run a conformal prediction procedure for each of these groups and hence achieve marginal coverage on each of them.
The underlying estimator must be used with cv=’prefit’ and the conformity score must be one of the following: - For MapieClassifier: ‘lac’, ‘score’, ‘cumulated_score’, ‘aps’ or ‘topk’ - For MapieRegressor: ‘absolute’ or ‘gamma’
- Parameters
- mapie_estimatorUnion[MapieClassifier, MapieRegressor]
The estimator for which the Mondrian method will be applied. It must be used with cv=’prefit’ and the conformity score must be one of the following: - For MapieClassifier: ‘lac’, ‘score’, ‘cumulated_score’, ‘aps’ or ‘topk’ - For MapieRegressor: ‘absolute’ or ‘gamma’
References
Vladimir Vovk, David Lindsay, Ilia Nouretdinov, and Alex Gammerman. Mondrian confidence machine. Technical report, Royal Holloway University of London, 2003
Examples
>>> import numpy as np >>> from sklearn.linear_model import LogisticRegression >>> from mapie.classification import MapieClassifier >>> from mapie.mondrian import MondrianCP >>> X_toy = np.arange(9).reshape(-1, 1) >>> y_toy = np.stack([0, 0, 1, 0, 1, 2, 1, 2, 2]) >>> partition_toy = [0, 0, 0, 0, 1, 1, 1, 1, 1] >>> clf = LogisticRegression(random_state=42).fit(X_toy, y_toy) >>> mapie = MondrianCP(MapieClassifier(estimator=clf, cv="prefit")).fit( ... X_toy, y_toy, partition=partition_toy ... ) >>> _, y_pi_mapie = mapie.predict( ... X_toy, partition=partition_toy, alpha=0.4) >>> print(y_pi_mapie[:, :, 0].astype(bool)) [[ True False False] [ True False False] [ True True False] [ True True False] [False True False] [False True True] [False False True] [False False True] [False False True]]
- Attributes
- partition_groupsNDArray
The unique groups of individuals for which the estimator was fitted
- mapie_estimatorsDict
A dictionary containing the fitted conformal estimator for each group of individuals
- __init__(mapie_estimator: Union[MapieClassifier, MapieRegressor])[source]¶
- fit(X: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], y: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], partition: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], **fit_params) MondrianCP [source]¶
Fit the Mondrian method
- Parameters
- XArrayLike of shape (n_samples, n_features)
The input data
- yArrayLike of shape (n_samples,) or (n_samples, n_outputs)
The target values
- partitionArrayLike of shape (n_samples,)
The groups of individuals. Must be defined by integers. There must be at least 2 individuals per group.
- **fit_params
Additional keyword arguments to pass to the estimator’s fit method that may be specific to the Mapie estimator used
- predict(X: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], partition: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], alpha: Optional[Union[float, Iterable[float]]] = None, **predict_params) Union[ndarray[Any, dtype[_ScalarType_co]], Tuple[ndarray[Any, dtype[_ScalarType_co]], ndarray[Any, dtype[_ScalarType_co]]]] [source]¶
Perform conformal prediction for each group of individuals
- Parameters
- XArrayLike of shape (n_samples, n_features)
The input data
- partitionArrayLike of shape (n_samples,), optional
The groups of individuals. Must be defined by integers.
By default None.
- alphafloat or Iterable[float], optional
The desired coverage level(s) for each group.
By default None.
- **predict_params
Additional keyword arguments to pass to the estimator’s predict method that may be specific to the Mapie estimator used
- Returns
- y_predNDArray of shape (n_samples,) or (n_samples, n_outputs)
The predicted values
- y_pssNDArray of shape (n_samples, n_outputs, n_alpha)
The predicted sets for the desired levels of coverage
- set_fit_request(*, partition: Union[bool, None, str] = '$UNCHANGED$') MondrianCP ¶
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters
- partitionstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
partition
parameter infit
.
- Returns
- selfobject
The updated object.
- set_predict_request(*, alpha: Union[bool, None, str] = '$UNCHANGED$', partition: Union[bool, None, str] = '$UNCHANGED$') MondrianCP ¶
Request metadata passed to the
predict
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed topredict
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it topredict
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters
- alphastr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
alpha
parameter inpredict
.- partitionstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
partition
parameter inpredict
.
- Returns
- selfobject
The updated object.