CC#

class mlquantify.adjust_counting.CC(learner=None, threshold=0.5)[source]#

Classify and Count (CC) quantifier.

Implements the Classify and Count method for quantification, describe as a baseline approach in the literature [1][2].

Parameters:
learnerestimator, optional

A supervised learning estimator with fit and predict methods. If None, it is expected that the aggregate method is used directly.

thresholdfloat, default=0.5

Decision threshold for converting predicted probabilities into class labels. Must be in the interval [0.0, 1.0].

Attributes:
learnerestimator

Underlying classification model.

Notes

The Classify and Count approach performs quantification by classifying each instance using the classifier’s predicted labels at a given threshold, then counting the prevalence of each class.

This method can be biased when class distributions differ between training and test sets, motivating further adjustment methods.

References

[1]

Forman, G. (2005). “Counting Positives Accurately Despite Inaccurate Classification”, ECML, pp. 564-575.

[2]

Forman, G. (2008). “Quantifying Counts and Costs via Classification”, Data Mining and Knowledge Discovery, 17(2), 164-206.

Examples

>>> from mlquantify.adjust_counting import CC
>>> import numpy as np
>>> from sklearn.linear_model import LogisticRegression
>>> X = np.random.randn(100, 5)
>>> y = np.random.randint(0, 2, 100)
>>> q = CC(learner=LogisticRegression())
>>> q.fit(X, y)
>>> q.predict(X)
{0: 0.47, 1: 0.53}
>>> q2 = CC()
>>> predictions = np.random.rand(200)
>>> q2.aggregate(predictions)
{0: 0.51, 1: 0.49}
aggregate(predictions, train_y_values=None)[source]#

Aggregate predictions into class prevalence estimates.

fit(X, y, learner_fitted=False, *args, **kwargs)[source]#

Fit the quantifier using the provided data and learner.

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 for the given data.

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

Save the quantifier instance to a file.

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

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