quantificationlib.multiclass.em module

class EM(estimator_train=None, estimator_test=None, verbose=0, epsilon=0.0001, max_iter=1000)[source]

Bases: UsingClassifiers

EM method

The main idea of this method is to estimate the prevalences applying EM algorithm.

This class (as every other class based on using a classifier) works in two different ways:

  1. Two estimators are used to classify training examples and testing examples in order to compute the distribution of both sets. Estimators can be already trained

  2. You can directly provide the predictions for the examples in the fit/predict methods. This is useful for synthetic/artificial experiments

The goal in both cases is to guarantee that all methods based on classifier are using exactly the same predictions when you compare this kind of quantifiers. In the first case, estimators are only trained once and can be shared for several quantifiers of this kind

Theoretically, it also works for multiclass quantification.

Parameters:
  • estimator_train (estimator object (default=None)) – An estimator object implementing fit and predict_proba. It is used to classify the examples of the training set and to compute the distribution of each class individually

  • estimator_test (estimator object (default=None)) – An estimator object implementing fit and predict_proba. It is used to classify the examples of the testing set and to compute the distribution of the whole testing set. For some experiments both estimator_train and estimator_test could be the same

  • verbose (int, optional, (default=0)) – The verbosity level. The default value, zero, means silent mode

  • epsilon (float, (default=1e-04)) – EM algorithm (its loop) stops when the difference between the prevalences of two consecutive iterations is lower than epsilon

  • max_iter (int (default=1000)) – The maximum number of iterations for the loop of the EM algorithm

estimator_train

Estimator used to classify the examples of the training set

Type:

estimator

estimator_test

Estimator used to classify the examples of the testing bag

Type:

estimator

predictions_train_

Predictions of the examples in the training set

Type:

ndarray, shape (n_examples, n_classes) (probabilities)

predictions_test_

Predictions of the examples in the testing bag

Type:

ndarray, shape (n_examples, n_classes) (probabilities)

needs_predictions_train

It is True because SORD quantifiers need to estimate the training distribution

Type:

bool, True

probabilistic_predictions

This means that predictions_train_/predictions_test_ contain probabilistic predictions

Type:

bool, True

classes_

Class labels

Type:

ndarray, shape (n_classes, )

y_ext_

Repmat of true labels of the training set. When CV_estimator is used with averaged_predictions=False, predictions_train_ will have a larger dimension (factor=n_repetitions * n_folds of the underlying CV) than y. In other cases, y_ext_ == y. y_ext_ is used in fit/predict method whenever the true labels of the training set are needed, instead of y

Type:

ndarray, shape(len(predictions_train_, 1)

verbose

The verbosity level

Type:

int

epsilon_

EM algorithm (its loop) stops when the mean absolute error between the prevalences of two consecutive iterations is lower than epsilon

Type:

float

max_iter_

The maximum number of iterations for the loop of the EM algorithm

Type:

int

prevalences_train_

Prevalence of each class in the training dataset

Type:

ndarray, shape (n_classes, )

Notes

Notice that at least one between estimator_train/predictions_train and estimator_test/predictions_test must be not None. If both are None a ValueError exception will be raised. If both are not None, predictions_train/predictions_test are used

References

Marco Saerens, Patrice Latinne, Christine Decaestecker: Adjusting the outputs of a classifier to new a priori probabilities: a simple procedure. Neural computation, 14(1) (2002), 21-41.

fit(X, y, predictions_train=None)[source]

This method performs the following operations: 1) fits the estimators for the training set and the testing set (if needed), and 2) computes predictions_train_ (probabilities) if needed. Both operations are performed by the fit method of its superclass.

After that, the method just computes the prevalences in the training set

Parameters:
  • X (array-like, shape (n_examples, n_features)) – Data

  • y (array-like, shape (n_examples, )) – True classes

  • predictions_train (ndarray, shape (n_examples, n_classes)) – Predictions of the examples in the training set

Raises:

ValueError – When estimator_train and predictions_train are at the same time None or not None

predict(X, predictions_test=None)[source]

Predict the class distribution of a testing bag

First, predictions_test_ are computed (if needed, when predictions_test parameter is None) by super().predict() method.

After that, the method applies EM algorithm. In the E step the new posterior are computed based on the estimated prevalences

P_tst(y|x) = P_tst(y)/P_tr(y) P_tr(y|x)

and are normalized to sum 1 Then, in the M step new prevalences are computed as the mean of the posteriors obtained in the E step

The loop stops when the max number of iterations is reached or when the difference between the prevalences of two consecutive iterations is lower than epsilon

Parameters:
  • X (array-like, shape (n_examples, n_features)) – Testing bag

  • predictions_test (ndarray, shape (n_examples, n_classes) (default=None)) –

    They must be probabilities (the estimator used must have a predict_proba method)

    If estimator_test is None then predictions_test can not be None. If predictions_test is None, predictions for the testing examples are computed using the predict_proba method of estimator_test (it must be an actual estimator)

Raises:

ValueError – When estimator_test and predictions_test are at the same time None or not None

Returns:

prevalences – Contains the predicted prevalence for each class

Return type:

ndarray, shape(n_classes, )

set_fit_request(*, predictions_train='$UNCHANGED$')

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
  • predictions_train (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for predictions_train parameter in fit.

  • self (EM) –

Returns:

self – The updated object.

Return type:

object

set_predict_request(*, predictions_test='$UNCHANGED$')

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict 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 predict.

  • 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.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
  • predictions_test (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for predictions_test parameter in predict.

  • self (EM) –

Returns:

self – The updated object.

Return type:

object