PriorCalibratedClassifier#

class imbcalibrate.PriorCalibratedClassifier(estimator: ClassifierMixin | Pipeline | None = None, weight: float | None = None)#

A meta-estimator which analytically calibrates the output of a binary classifier to account for class balancing techniques.

Parameters:
estimatorestimator instance, default=None

The classifier whose output need to be calibrated to provide more accurate predict_proba outputs. If estimator is a Pipeline, the last step of the pipeline must be a classifier. If estimator is an imbalanced-learn Pipeline, only RandomUnderSampler or RandomOverSampler are recognised. If estimator is None, a default LogisticRegression classifier will be used.

weightfloat, default=None

The weight to be used for the prior calibration. If None, the weight will be inferred from the estimator’s scale_pos_weight or class_weight attributes if available, or the resampler’s sampling strategy when applicable. When provided, weight acts as an override and will be used instead of the estimator’s attributes. If the estimator does not have these attributes and weight is None, a warning is issued and the weight will default to 1.0 (i.e. no effect).

Attributes:
estimator_estimator instance

The fitted (uncalibrated) estimator.

classes_ndarray, shape (n_classes,)

The classes seen at fit().

n_features_in_int

Number of features seen during fit.

feature_names_in_ndarray of shape (n_features_in_,)

Names of features seen during fit. Defined only when X has feature names that are all strings.

Examples

>>> from sklearn.datasets import make_classification
>>> from sklearn.linear_model import LogisticRegression
>>> from imbcalibrate import PriorCalibratedClassifier
>>>
>>> X, y = make_classification(
...     n_samples=1000, weights=[0.9, 0.1], random_state=0
... )
>>> classifier = PriorCalibratedClassifier(
...     estimator=LogisticRegression(class_weight="balanced", random_state=0)
... )
>>> classifier.fit(X, y)
PriorCalibratedClassifier(...)
>>> probabilities = classifier.predict_proba(X[:5])
>>> probabilities.shape
(5, 2)

Methods

fit(X, y, **fit_params)

Fit the model according to the given training data.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

predict(X)

Predict the target of new samples.

predict_proba(X)

Calibrated probabilities of classification.

score(X, y[, sample_weight])

Return accuracy on provided data and labels.

set_params(**params)

Set the parameters of this estimator.

set_score_request(*[, sample_weight])

Configure whether metadata should be requested to be passed to the score method.

fit(X, y, **fit_params)#

Fit the model according to the given training data.

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

Training vector, where n_samples is the number of samples and n_features is the number of features.

yarray-like, shape (n_samples,)

Target vector relative to X.

**fit_paramskwargs

Additional fit parameters to pass to the underlying estimator’s fit method.

Returns:
selfobject

Fitted estimator.

get_metadata_routing()#

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)#

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)#

Predict the target of new samples.

The predicted class is the class that has the highest probability, and can thus be different from the prediction of the uncalibrated classifier.

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

The samples, as accepted by estimator.predict_proba.

Returns:
yndarray, shape (n_samples,)

The predicted class.

predict_proba(X)#

Calibrated probabilities of classification.

This function returns calibrated probabilities of classification according to each class on an array of test vectors X.

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

The samples, as accepted by estimator.predict_proba.

Returns:
Cndarray, shape (n_samples, n_classes)

The array of calibrated probabilities of classification according to each class.

score(X, y, sample_weight=None)#

Return accuracy on provided data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

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

Test samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

Mean accuracy of self.predict(X) w.r.t. y.

set_params(**params)#

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.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') PriorCalibratedClassifier#

Configure whether metadata should be requested to be passed to the score 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 score 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 score.

  • 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:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.