FM#

class mlquantify.adjust_counting.FM(learner=None)[source]#

Friedman Method for quantification adjustment.

This class implements the Friedman (2015) matrix-based quantification adjustment, which formulates the quantification problem as a constrained optimization problem. It adjusts the estimated class prevalences by minimizing the difference between predicted and expected prevalences, subject to valid prevalence constraints.

The confusion matrix is computed by applying estimated posterior probabilities over true labels, enabling accurate correction of prevalence estimates under concept drift.

The confusion matrix is estimated for each class \(k\) by: applying thresholding on posterior probabilities against prior prevalence, as described in the FM algorithm. This enables the correction using a quadratic optimization approach.

The method solves:

\[\min_{\hat{\pi}_F} \| \mathbf{C} \hat{\pi}_F - \mathbf{p} \|^2\]

subject to constraints:

\[\hat{\pi}_F \geq 0, \quad \sum_k \hat{\pi}_{F,k} = 1\]

where \(\mathbf{C}\) is the confusion matrix, \(\mathbf{p}\) is the vector of predicted prevalences.

Parameters:
learnerestimator, optional

Base classifier with fit and predict_proba methods. If None, a default estimator will be used.

Attributes:
CMndarray of shape (n_classes, n_classes)

Confusion matrix used for correction.

References

[1]

Friedman, J. H., et al. (2015). “Detecting and Dealing with Concept Drift”, Proceedings of the IEEE, 103(11), 1522-1541.

Examples

>>> from mlquantify.adjust_counting import FM
>>> import numpy as np
>>> X = np.random.randn(50, 4)
>>> y = np.random.randint(0, 2, 50)
>>> fm = FM(learner=LogisticRegression())
>>> fm.fit(X, y)
>>> fm.predict(X)
{0: 0.5, 1: 0.5}
aggregate(predictions, train_predictions, y_train_values)[source]#

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

fit(X, y, learner_fitted=False, cv=10, stratified=True, random_state=None, shuffle=True)[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(*, 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$') FM[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.