BaseMatchingQuantifier#

class mlquantify.matching.BaseMatchingQuantifier(representation, normalize=False)[source]#

Base class for distribution matching quantifiers.

Distribution matching quantifiers represent the training class-conditional distributions and the test distribution in a common space, then estimate class prevalences by matching the test representation with a convex combination of the training representations.

Parameters:
representationrepresentation instance

A representation object that can be fitted to training data and then used to transform both training and test data into a common space. Must define a class_representations_ attribute after fitting.

normalizebool, default=False

Whether to normalize the representations to form valid probability distributions before computing distances.

Attributes:
representationrepresentation instance

The representation object used for fitting and transforming data.

classes_ndarray of shape (n_classes,)

Class labels seen during fit.

tr_representation_ndarray of shape (n_classes, n_representation_features)

Class-conditional representations learned from the training data.

best_distance_float

The distance between the test representation and the best-matching convex combination of the training representations.

distances_list of float

Distances computed during the optimization process (if applicable).

Examples

>>> from mlquantify.matching import BaseMatchingQuantifier
>>> from mlquantify.representations import KDERepresentation
>>> import numpy as np
>>> class MyMatching(BaseMatchingQuantifier):
...     def __init__(self):
...         super().__init__(representation=KDERepresentation())
...     def _solve_prevalence(self, test_representation, train_representations):
...         # Dummy implementation for illustration
...         prevalences = np.random.dirichlet(np.ones(len(train_representations)))
...         distance = np.random.rand()
...         return prevalences, distance
>>> X_train, y_train = np.random.randn(100, 5), np.random.randint(0, 2, 100)
>>> X_test = np.random.randn(50, 5)
>>> MyMatching().fit(X_train, y_train).predict(X_test)
{0: 0.6, 1: 0.4}
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.

save_quantifier(path: str | None = None) None[source]#

Save the quantifier instance to a file.

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.