aif360.sklearn.postprocessing
.PostProcessingMeta
- class aif360.sklearn.postprocessing.PostProcessingMeta(estimator, postprocessor, *, prefit=False, val_size=0.25, **options)[source]
A meta-estimator which wraps a given estimator with a post-processing step.
The post-processor trains on a separate training set from the estimator to prevent leakage.
Note
Because of the dataset splitting, if a Pipeline is necessary it should be used as the input to this meta-estimator not the other way around.
- Variables:
estimator_ – Fitted estimator.
postprocessor_ – Fitted postprocessor.
classes_ (array, shape (n_classes,)) – Class labels from
estimator_
.
- Parameters:
estimator (sklearn.BaseEstimator) – Original estimator.
postprocessor – Post-processing algorithm.
prefit (bool) – If
True
, it is assumed that estimator has been fitted already and all data is used to train postprocessor.val_size (int or float) – Size of validation set used to fit the postprocessor. The estimator fits on the remainder of the training set. See
train_test_split()
for details.**options – Keyword options passed through to
train_test_split()
. Note: ‘train_size’ and ‘test_size’ will be ignored in favor of ‘val_size’.
Methods
Splits the training samples with
train_test_split()
and uses the resultant 'train' portion to train the estimator.get_metadata_routing
Get metadata routing of this object.
get_params
Get parameters for this estimator.
Predict class labels for the given samples.
Log of probability estimates.
Probability estimates.
Returns the output of the post-processor's score function on the given test data and labels.
Request metadata passed to the
fit
method.set_params
Set the parameters of this estimator.
Request metadata passed to the
score
method.Attributes
Class labels from the base estimator.
- __init__(estimator, postprocessor, *, prefit=False, val_size=0.25, **options)[source]
- Parameters:
estimator (sklearn.BaseEstimator) – Original estimator.
postprocessor – Post-processing algorithm.
prefit (bool) – If
True
, it is assumed that estimator has been fitted already and all data is used to train postprocessor.val_size (int or float) – Size of validation set used to fit the postprocessor. The estimator fits on the remainder of the training set. See
train_test_split()
for details.**options – Keyword options passed through to
train_test_split()
. Note: ‘train_size’ and ‘test_size’ will be ignored in favor of ‘val_size’.
- property classes_
Class labels from the base estimator.
- fit(X, y, sample_weight=None, **fit_params)[source]
Splits the training samples with
train_test_split()
and uses the resultant ‘train’ portion to train the estimator. Then the estimator predicts on the ‘test’ portion of the split data and the post-processor is trained with those prediction-ground-truth target pairs.- Parameters:
X (array-like) – Training samples.
y (pandas.Series) – Training labels.
sample_weight (array-like, optional) – Sample weights.
**fit_params – Parameters passed to the post-processor
fit()
method. Note: these do not need to be prefixed with__
notation.
- Returns:
self
- predict(X)[source]
Predict class labels for the given samples.
First, runs
self.estimator_.predict()
(orpredict_proba()
if required) then returns the post-processed output from those predictions.- Parameters:
X (pandas.DataFrame) – Test samples.
- Returns:
numpy.ndarray – Predicted class label per sample.
- predict_log_proba(X)[source]
Log of probability estimates.
First, runs
self.estimator_.predict()
(orpredict_proba()
if required) then returns the post-processed output from those predictions.The returned estimates for all classes are ordered by the label of classes.
- Parameters:
X (pandas.DataFrame) – Test samples.
- Returns:
array – Returns the log-probability of the sample for each class in the model, where classes are ordered as they are in
self.classes_
.
- predict_proba(X)[source]
Probability estimates.
First, runs
self.estimator_.predict()
(orpredict_proba()
if required) then returns the post-processed output from those predictions.The returned estimates for all classes are ordered by the label of classes.
- Parameters:
X (pandas.DataFrame) – Test samples.
- Returns:
numpy.ndarray – Returns the probability of the sample for each class in the model, where classes are ordered as they are in
self.classes_
.
- score(X, y, sample_weight=None)[source]
Returns the output of the post-processor’s score function on the given test data and labels.
First, runs
self.estimator_.predict()
(orpredict_proba()
if required) then gets the post-processed output from those predictions and scores it.- Parameters:
X (pandas.DataFrame) – Test samples.
y (array-like) – True labels for X.
sample_weight (array-like, optional) – Sample weights.
- Returns:
float – Score value.
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') PostProcessingMeta [source]
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:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weight
parameter infit
.- Returns:
self (object) – The updated object.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') PostProcessingMeta [source]
Request metadata passed to the
score
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 toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.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:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weight
parameter inscore
.- Returns:
self (object) – The updated object.