HDx#

class mlquantify.matching.HDx(bins_size=None, strategy='ovr')[source]#

Hellinger Distance x (HDx) quantifier.

Estimates binary prevalence by comparing class-conditional feature histograms directly, without a classifier. The method sweeps over multiple bin counts and selects the mixture proportion that minimises the Hellinger distance between the test and mixture training histograms.

This is a binary-only method. Multiclass problems are handled with a one-vs-rest (OvR) strategy by default.

Parameters:
bins_sizearray-like or None, default=None

Array of bin counts to sweep. Defaults to a range from 2 to 30.

strategy{‘ovr’, ‘ovo’}, default=’ovr’

Multiclass decomposition strategy.

Attributes:
classes_ndarray of shape (n_classes,)

Class labels seen during fit.

References

References
[1]

González-Castro, V., Alaiz-Rodriguez, R., & Alegre, E. (2013). Class Distribution Estimation Based on the Hellinger Distance. Information Sciences, 218, 146–164.

Examples

>>> from mlquantify.matching import HDx
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=200, random_state=42)
>>> q = HDx().fit(X, y)
>>> q.predict(X)
{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 aggregate so 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)
fit_predict(X, y, X_test)[source]#

Fit and predict class prevalences without storing models.

get_distance(dist_train, dist_test, distance='hellinger')[source]#

Compute a distance between two normalized representations.

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 using the MoSS adaptive correction.

Generates posterior probabilities for X with the fitted classifier and delegates to aggregate, which selects the best MoSS merging factor and calls the base quantifier’s aggregate.

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}
save_quantifier(path: str | None = None) None[source]#

Save the quantifier instance to a file.

set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') HDx[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:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight 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.