TX#

class mlquantify.adjust_counting.TX(learner=None, threshold=0.5, strategy='ovr', n_jobs=None)[source]#

Threshold X method — threshold where \(\text{TPR} + \text{FPR} = 1\).

This method selects the classification threshold at which the sum of the true positive rate (TPR) and false positive rate (FPR) equals one. This threshold choice balances errors in a specific way improving quantification.

Parameters:
learnerestimator, optional

A supervised learning model with fit and predict_proba methods.

thresholdfloat, default=0.5

Classification threshold in [0, 1] for applying in the CC output.

References

[1]

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

aggregate(predictions, y_train)[source]#

Aggregate predictions and apply matrix- or rate-based bias correction.

Parameters:
predictionsndarray of shape (n_samples, n_classes)

Learner predictions on test data. Can be probabilities (n_samples, n_classes) or class labels (n_samples,).

train_predictionsndarray of shape (n_samples, n_classes)

Learner predictions on training data. Can be probabilities (n_samples, n_classes) or class labels (n_samples,).

y_trainndarray of shape (n_samples,)

True class labels of the training data.

Returns:
ndarray of shape (n_classes,)

Class prevalence estimates.

Examples

>>> from mlquantify.adjust_counting import AC
>>> import numpy as np
>>> q = AC()
>>> predictions = np.random.rand(200)
>>> train_predictions = np.random.rand(200) # generated via cross-validation
>>> y_train = np.random.randint(0, 2, 200)
>>> q.aggregate(predictions, train_predictions, y_train)
{0: 0.51, 1: 0.49}
fit(X, y)[source]#

Fit the quantifier using the provided data and learner.

Parameters:
Xarray-like of shape (n_samples, n_features)

Training data.

yarray-like of shape (n_samples,)

True labels.

learner_fittedbool, optional

If True, the learner is already fitted, by default False.

cvint, optional

Number of cross-validation folds, by default 5.

stratifiedbool, optional

Whether to stratify the cross-validation, by default True.

random_stateint, optional

Random state for reproducibility, by default None.

shufflebool, optional

Whether to shuffle the data, by default False.

Returns:
selfBaseAdjustCount

Fitted quantifier.

fit_predict(X, y, X_test)[source]#

Fit and predict class prevalences without storing models.

get_best_threshold(thresholds, tprs, fprs)[source]#

Select the best threshold according to the specific method.

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(*, cv: bool | None | str = '$UNCHANGED$', learner_fitted: bool | None | str = '$UNCHANGED$', random_state: bool | None | str = '$UNCHANGED$', shuffle: bool | None | str = '$UNCHANGED$', stratified: bool | None | str = '$UNCHANGED$') TX[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:
cvstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for cv parameter in fit.

learner_fittedstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for learner_fitted parameter in fit.

random_statestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for random_state parameter in fit.

shufflestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for shuffle parameter in fit.

stratifiedstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

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