BinaryQuantifier#
- class mlquantify.multiclass.BinaryQuantifier[source]#
Meta-quantifier enabling One-vs-Rest and One-vs-One strategies.
This class extends a base quantifier to handle multiclass problems by decomposing them into binary subproblems. It automatically delegates fitting, prediction, and aggregation operations to the appropriate binary quantifiers.
- Attributes:
- qtfs_dict
Dictionary mapping class labels or label pairs to fitted binary quantifiers.
- strategy{‘ovr’, ‘ovo’}
Defines how multiclass quantification is decomposed.
- aggregate(predictions, y_train)[source]#
Aggregate posteriors into prevalences using MoSS score simulation.
Searches over
merging_factorsto find the synthetic score distribution (generated byMoSS) whose histogram is closest to the test score distribution, then passes that synthetic set as the training reference to the base quantifier’saggregate.- Parameters:
- predictionsndarray of shape (n_samples, n_classes)
Posterior probabilities of the test instances.
- y_trainndarray of shape (n_train_samples,)
Training class labels used to resolve class ordering.
- Returns:
- prevalencesdict or ndarray of shape (n_classes,)
Estimated class prevalences.
Examples
>>> from mlquantify.meta import QuaDapt >>> from mlquantify.matching import DyS >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.datasets import make_classification >>> X, y = make_classification(n_samples=200, random_state=42) >>> q = QuaDapt(DyS(LogisticRegression())).fit(X, y) >>> proba = LogisticRegression().fit(X, y).predict_proba(X) >>> q.aggregate(proba, y) {0: 0.49, 1: 0.51}
- fit(X, y)[source]#
Fit the base classifier of the wrapped quantifier.
Only the underlying estimator is trained here; the full aggregation is deferred to
aggregateso that the MoSS-based correction can be applied at prediction time.- Parameters:
- Xarray-like of shape (n_samples, n_features)
Training feature matrix.
- yarray-like of shape (n_samples,)
Training class labels.
- Returns:
- selfQuaDapt
The fitted quantifier.
- Raises:
- ValueError
If the wrapped quantifier does not use soft (probabilistic) predictions.
Examples
>>> from mlquantify.meta import QuaDapt >>> from mlquantify.matching import DyS >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.datasets import make_classification >>> X, y = make_classification(n_samples=200, random_state=42) >>> q = QuaDapt(DyS(LogisticRegression())).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
MetadataRequestencapsulating 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 using the MoSS adaptive correction.
Generates posterior probabilities for
Xwith the fitted classifier and delegates toaggregate, which selects the best MoSS merging factor and calls the base quantifier’saggregate.- Parameters:
- Xarray-like of shape (n_samples, n_features)
Test feature matrix.
- Returns:
- prevalencesdict or ndarray of shape (n_classes,)
Estimated class prevalences.
Examples
>>> from mlquantify.meta import QuaDapt >>> from mlquantify.matching import DyS >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.datasets import make_classification >>> X, y = make_classification(n_samples=200, random_state=42) >>> q = QuaDapt(DyS(LogisticRegression())).fit(X, y) >>> q.predict(X) {0: 0.49, 1: 0.51}
- 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.