AggregativeBootstrap#

class mlquantify.meta.AggregativeBootstrap(quantifier, n_train_bootstraps=1, n_test_bootstraps=1, random_state=None, region_type='intervals', confidence_level=0.95)[source]#

Aggregative Bootstrap Quantifier for prevalence confidence regions.

Wraps any aggregative quantifier and applies bootstrap resampling to both training and test predictions to produce a distribution of prevalence estimates. The distribution is summarised as a point estimate together with a confidence region (intervals, ellipse, or CLR-ellipse).

Parameters:
quantifierBaseQuantifier

The base aggregative quantifier to wrap.

n_train_bootstrapsint, default=1

Number of bootstrap resamples from the training predictions.

n_test_bootstrapsint, default=1

Number of bootstrap resamples from the test predictions.

random_stateint or None, default=None

Random seed for reproducibility.

region_type{‘intervals’, ‘ellipse’, ‘ellipse-clr’}, default=’intervals’

Type of confidence region to construct.

confidence_levelfloat, default=0.95

Confidence level for the region.

Attributes:
train_predictionsndarray

Predictions on the training (or validation) set.

y_trainndarray

Labels corresponding to train_predictions.

classesndarray of shape (n_classes,)

Class labels seen during fit.

References

References
[1]

Moreo, A., & Salvati, A. (2025). Uncertainty Quantification in Quantification. LQ 2025 Workshop Proceedings.

Examples

>>> from mlquantify.meta import AggregativeBootstrap
>>> from mlquantify.likelihood import EMQ
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=200, random_state=42)
>>> q = AggregativeBootstrap(
...     EMQ(LogisticRegression()),
...     n_train_bootstraps=10,
...     n_test_bootstraps=10,
... ).fit(X, y)
>>> q.predict(X)
{0: 0.49, 1: 0.51}
aggregate(predictions, train_predictions, y_train)[source]#

Aggregate predictions via bootstrap resampling into a prevalence estimate.

Resamples both the training and test predictions n_train_bootstraps × n_test_bootstraps times, calls the base quantifier’s aggregate method on each combination, and summarises the resulting distribution as a point estimate with a confidence region.

Parameters:
predictionsndarray of shape (n_test_samples, n_classes)

Soft predictions on the test set (e.g. posterior probabilities).

train_predictionsndarray of shape (n_train_samples, n_classes)

Soft predictions stored from the training (or validation) set.

y_trainndarray of shape (n_train_samples,)

Class labels corresponding to train_predictions.

Returns:
prevalencesdict or ndarray of shape (n_classes,)

Point prevalence estimate extracted from the centre of the bootstrap confidence region.

Examples

>>> from mlquantify.meta import AggregativeBootstrap
>>> from mlquantify.likelihood import EMQ
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.datasets import make_classification
>>> import numpy as np
>>> X, y = make_classification(n_samples=200, random_state=42)
>>> lr = LogisticRegression().fit(X, y)
>>> train_preds = lr.predict_proba(X)
>>> q = AggregativeBootstrap(EMQ(lr), n_train_bootstraps=5,
...                          n_test_bootstraps=5).fit(X, y)
>>> q.aggregate(train_preds, train_preds, y)
{0: 0.49, 1: 0.51}
fit(X, y, val_split=None)[source]#

Fit the base classifier and store predictions for bootstrap resampling.

Trains only the base classifier (not the full aggregative quantifier), then stores the resulting soft predictions for later use in aggregate. Optionally holds out a validation split so the stored predictions come from unseen data.

Parameters:
Xarray-like of shape (n_samples, n_features)

Training feature matrix.

yarray-like of shape (n_samples,)

Training class labels.

val_splitfloat or None, default=None

If given, the fraction of data held out as a validation set whose predictions are stored. None uses the full training set.

Returns:
selfAggregativeBootstrap

The fitted quantifier.

Raises:
ValueError

If the wrapped quantifier is not an aggregative quantifier.

Examples

>>> from mlquantify.meta import AggregativeBootstrap
>>> from mlquantify.likelihood import EMQ
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=200, random_state=42)
>>> q = AggregativeBootstrap(EMQ(LogisticRegression()),
...                          n_train_bootstraps=10,
...                          n_test_bootstraps=10).fit(X, y)
get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

predict(X)[source]#

Predict class prevalences with bootstrap-derived confidence estimation.

Generates classifier predictions for X and delegates to aggregate with the stored training predictions to produce a bootstrap-based prevalence estimate.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test feature matrix.

Returns:
prevalencesdict or ndarray of shape (n_classes,)

Point prevalence estimate extracted from the bootstrap distribution.

Examples

>>> from mlquantify.meta import AggregativeBootstrap
>>> from mlquantify.likelihood import EMQ
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=200, random_state=42)
>>> q = AggregativeBootstrap(EMQ(LogisticRegression()),
...                          n_train_bootstraps=10,
...                          n_test_bootstraps=10).fit(X, y)
>>> q.predict(X)
{0: 0.49, 1: 0.51}
save_quantifier(path: str | None = None) None[source]#

Save the quantifier instance to a file.

set_fit_request(*, val_split: bool | None | str = '$UNCHANGED$') AggregativeBootstrap[source]#

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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.

Added in version 1.3.

Parameters:
val_splitstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for val_split parameter in fit.

Returns:
selfobject

The updated object.

set_params(**params)[source]#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.