CC#
- class mlquantify.counting.CC(estimator=None, threshold=0.5)[source]#
Classify and Count (CC) quantifier.
Targets prior probability shift. Estimates class prevalences by classifying each test instance with a hard classifier and counting the fraction assigned to each class. It is the simplest quantification baseline and is systematically biased whenever the test class distribution differs from the training one.
- Parameters:
- estimatorestimator, optional
A classifier with
fitandpredictmethods. IfNone, callaggregatedirectly with pre-computed predictions.- thresholdfloat, default=0.5
Decision threshold applied to soft scores to produce hard labels. Must be in
[0.0, 1.0].
- Attributes:
- estimator_estimator
The fitted underlying classifier.
- classes_ndarray of shape (n_classes,)
Class labels seen during
fit.
Notes
CC applies no correction for classifier error, so its absolute error grows roughly linearly as the test prevalence departs from the training prevalence. Contrast with
PCC, which averages soft posteriors, andACC, which corrects for the true- and false-positive rates.References
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.counting import CC >>> from sklearn.linear_model import LogisticRegression >>> import numpy as np >>> X, y = np.random.randn(100, 5), np.random.randint(0, 2, 100) >>> q = CC(LogisticRegression()).fit(X, y) >>> q.predict(X) {0: ..., 1: ...} >>> # call aggregate directly with pre-computed hard predictions >>> preds = q.estimator_.predict(X) >>> q.aggregate(preds, classes=[0, 1]) {0: ..., 1: ...}
- aggregate(predictions, classes=None)[source]#
Aggregate predictions into class prevalence estimates.
- Parameters:
- predictionsndarray of shape (n_samples,) or (n_samples, n_classes)
Estimator predictions on test data. Can be probabilities (n_samples, n_classes) or class labels (n_samples,).
- classesarray-like of shape (n_classes,) or None, default=None
Class labels the output must report, in order. When given, every class appears in the result even if absent from
predictions(with prevalence 0). WhenNone, the classes seen duringfitare used; if the quantifier is unfitted, they are inferred from the predictions.
- Returns:
- ndarray of shape (n_classes,)
Class prevalence estimates.
Examples
>>> from mlquantify.counting import CC >>> import numpy as np >>> q = CC() >>> predictions = np.random.rand(200) >>> q.aggregate(predictions) {0: ..., 1: ...}
- fit(X, y, estimator_fitted=False, *args, **kwargs)[source]#
Fit the quantifier using the provided data and estimator.
- 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.
- set_fit_request(*, estimator_fitted: bool | None | str = '$UNCHANGED$') CC[source]#
Configure whether metadata should be requested to be passed to the
fitmethod.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(seesklearn.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 tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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:
- estimator_fittedstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
estimator_fittedparameter infit.
- 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.