quantificationlib.decomposition.multiclass module¶
Generic quantifiers based on multiclass decompositions
- class OneVsRestQuantifier(base_quantifier, estimator_train=None, estimator_test=None, verbose=0)[source]¶
Bases:
UsingClassifiers
Implements a One Vs Rest Multiclass Quantifier given any base quantifier
Trains one quantifier per class that predicts the prevalence on such class. The aggregation strategy just normalizes these prevalences to sum 1
The class works both with quantifiers that require classifiers or not. In the former case, the estimator used for the training distribution and the testing distribution must be a OneVsRestQuantifier
- Parameters:
base_quantifier (quantifier object) – The base quantifier used to build the One versus Rest decomposition. Any quantifier can be used
estimator_train (estimator object, optional, (default=None)) – An estimator object implementing fit and one of predict or predict_proba. It is used to classify the examples of the training set and to obtain their distribution when the base quantifier is an instance of the class UsingClassifiers. Notice that some quantifiers of this kind, namely CC and PCC, do not require an estimator for the training distribution
estimator_test (estimator object, optional, (default=None)) – An estimator object implementing fit and one of predict or predict_proba. It is used to classify the examples of the testing bag and to obtain their distribution when the base quantifier is an instance of the class UsingClassifiers. For some experiments both estimators could be the same
verbose (int, optional, (default=0)) – The verbosity level. The default value, zero, means silent mode
- base_quantifier¶
The base quantifier used to build the One versus Rest decomposition
- Type:
quantifier object
- 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
- needs_predictions_train¶
True if the base quantifier needs to estimate the training distribution
- Type:
bool, (default=True)
- probabilistic_predictions¶
Not used
- Type:
bool
- predictions_train_¶
Predictions of the examples in the training set
- Type:
ndarray, shape (n_examples, n_classes) (probabilistic)
- predictions_test_¶
Predictions of the examples in the testing bag
- Type:
ndarray, shape (n_examples, n_classes) (probabilistic)
- quantifiers_¶
List of quantifiers, one for each class
- Type:
ndarray, shape (n_classes, )
- 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_estimator) than y. In other cases, y_ext_ == y. y_ext_ must be used in fit/predict methods whenever the true labels of the training set are needed, instead of y
- Type:
ndarray, shape(len(predictions_train_), )
- verbose¶
The verbosity level
- Type:
int
- fit(X, y, predictions_train=None)[source]¶
Fits all the quanfifiers of a OneVsRest decomposition
First, the method fits the estimators (estimator_train and estimator_test) (if needed) using the fit method of its superclass
Then, it creates (using deepcopy) the set on quantifiers_, one per class, and fit them
- Parameters:
X (array-like, shape (n_examples, n_features)) – Data
y (array-like, shape (n_examples, )) – True classes
predictions_train (ndarray, optional, shape (n_examples, n_classes) (probs)) – Predictions of the examples in the training set
- Raises:
ValueError – When estimator_train or estimator_test are not instances of OneVsRestClassifier
- predict(X, predictions_test=None)[source]¶
Aggregates the prevalences of the quantifiers_ to compute the final prediction
Just one aggregation strategy is implemented. It normalizes the prevalences given by each quantifier to sum 1
- Parameters:
X ((sparse) array-like, shape (n_examples, n_features)) – Data
predictions_test (ndarray, shape (n_examples, n_classes) (default=None)) – Predictions for the testing bag
- 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
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if 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.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 infit
.self (OneVsRestQuantifier) –
- 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
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed topredict
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it topredict
.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 inpredict
.self (OneVsRestQuantifier) –
- Returns:
self – The updated object.
- Return type:
object