CDE#
- class mlquantify.likelihood.CDE(estimator=None, tol=0.0001, max_iter=100, init_cfp=1.0, strategy='ovr', n_jobs=None)[source]#
CDE-Iterate quantifier.
Targets prior probability shift. Estimates binary prevalence by iteratively adjusting the decision threshold using class-cost ratios derived from the training priors and the current prevalence estimate, re-counting at the new threshold until the predicted positive proportion stabilises. Binary-only; multiclass via one-vs-rest.
- Parameters:
- estimatorestimator, optional
A probabilistic classifier with
fitandpredict_probamethods.- tolfloat, default=1e-4
Convergence threshold on the positive prevalence change between iterations.
- max_iterint, default=100
Maximum number of iterations.
- init_cfpfloat, default=1.0
Initial cost of false positives; sets the starting cost ratio that the iteration then refines.
- strategy{‘ovr’, ‘ovo’}, default=’ovr’
Multiclass decomposition strategy.
'ovr': one-vs-rest, one binary quantifier per class.'ovo': one-vs-one, one binary quantifier per class pair.
- n_jobsint or None, default=None
Number of parallel jobs for multiclass decomposition.
- Attributes:
- estimator_estimator
The fitted underlying classifier.
- classes_ndarray of shape (n_classes,)
Class labels seen during
fit.- priors_ndarray of shape (n_classes,)
Training class prevalences.
See also
EMQExpectation-Maximization likelihood quantifier.
ACCSingle-step adjusted count.
Notes
CDE couples threshold adjustment with cost-sensitive re-counting; it behaves like an iterative cousin of the
ThresholdAdjustmentfamily rather than a likelihood maximiser, despite living in this module.References
References
[1]Barranquero, J., Díez, J., & del Coz, J. J. (2015). Quantification-Oriented Learning Based on Reliable Classifiers. Pattern Recognition, 48(2), 591–604.
Examples
>>> from mlquantify.likelihood import CDE >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.datasets import make_classification >>> X, y = make_classification(n_samples=200, random_state=42) >>> q = CDE(estimator=LogisticRegression()).fit(X, y) >>> q.predict(X) {0: ..., 1: ...} >>> # call aggregate with pre-computed posteriors >>> proba = q.estimator_.predict_proba(X) >>> q.aggregate(proba, proba, y) {0: ..., 1: ...}
- aggregate(predictions, y_train, classes=None)[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.
- classesarray-like of shape (n_classes,) or None, default=None
Class labels the output must report, in order. When
Nonethey are inferred fromy_train.
- 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: ..., 1: ...}
- 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: ..., 1: ...}
- 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.