CSThresholdClassifier#

class empulse.models.CSThresholdClassifier(estimator, *, calibrator='sigmoid', pos_label=None, random_state=None, tp_cost=0.0, tn_cost=0.0, fn_cost=0.0, fp_cost=0.0)[source]#

Classifier that sets the decision threshold to optimize the instance-dependent cost loss.

Parameters:
estimatorobject

A classifier with a predict_proba method.

calibrator{‘sigmoid’, ‘isotonic’}, Estimator or None, default=’sigmoid’

The calibrator to use.

  • If ‘sigmoid’, then a CalibratedClassifierCV with method=’sigmoid’ and ensemble=False is used.

  • If ‘isotonic’, then a CalibratedClassifierCV with method=’isotonic’ and ensemble=False is used.

  • If an Estimator, then it should have a fit and predict_proba method.

  • If None, probabilities are assumed to be well-calibrated.

pos_labelint, str, ‘boolean’ or None, default=None

The positive label. If None, the positive label is assumed to be 1.

random_stateint or None, default=None

Random state for the calibrator. Ignored when calibrator is an Estimator.

tp_costfloat or array-like, shape=(n_samples,), default=0.0

Cost of true positives. If float, then all true positives have the same cost. If array-like, then it is the cost of each true positive classification. Is overwritten if another tp_cost is passed to the predict method.

Note

It is not recommended to pass instance-dependent costs to the __init__ method. Instead, pass them to the predict method.

tp_costfloat or array-like, shape=(n_samples,), default=0.0

Cost of true positives. If float, then all true positives have the same cost. If array-like, then it is the cost of each true positive classification. Is overwritten if another tp_cost is passed to the fit method.

Note

It is not recommended to pass instance-dependent costs to the __init__ method. Instead, pass them to the fit method.

fp_costfloat or array-like, shape=(n_samples,), default=0.0

Cost of false positives. If float, then all false positives have the same cost. If array-like, then it is the cost of each false positive classification. Is overwritten if another fp_cost is passed to the predict method.

Note

It is not recommended to pass instance-dependent costs to the __init__ method. Instead, pass them to the predict method.

tn_costfloat or array-like, shape=(n_samples,), default=0.0

Cost of true negatives. If float, then all true negatives have the same cost. If array-like, then it is the cost of each true negative classification. Is overwritten if another tn_cost is passed to the predict method.

Note

It is not recommended to pass instance-dependent costs to the __init__ method. Instead, pass them to the predict method.

fn_costfloat or array-like, shape=(n_samples,), default=0.0

Cost of false negatives. If float, then all false negatives have the same cost. If array-like, then it is the cost of each false negative classification. Is overwritten if another fn_cost is passed to the predict method.

Note

It is not recommended to pass instance-dependent costs to the __init__ method. Instead, pass them to the predict method.

Attributes:
classes_numpy.ndarray of shape (n_classes,)

Classes labels.

estimator_Estimator

The fitted classifier.

Notes

The optimal threshold is computed as [1]:

\[t^*_i = \frac{C_i(1|0) - C_i(0|0)}{C_i(1|0) - C_i(0|0) + C_i(0|1) - C_i(1|1)}\]

Note

The optimal decision threshold is only accurate when the probabilities are well-calibrated. Therefore, it is recommended to use a calibrator when the probabilities are not well-calibrated. See scikit-learn’s user guide for more information.

References

[1]

Höppner, S., Baesens, B., Verbeke, W., & Verdonck, T. (2022). Instance-dependent cost-sensitive learning for detecting transfer fraud. European Journal of Operational Research, 297(1), 291-300.

property classes_#

Classes labels.

decision_function(X)#

Decision function for samples in X using the fitted estimator.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

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

Returns:
decisionsndarray of shape (n_samples,)

The decision function computed the fitted estimator.

fit(X, y, **params)#

Fit the classifier.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

Training data.

yarray-like of shape (n_samples,)

Target values.

**paramsdict

Parameters to pass to the fit method of the underlying classifier.

Returns:
selfobject

Returns an instance of self.

get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRouter

A sklearn.utils.metadata_routing.MetadataRouter 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, tp_cost=Parameter.UNCHANGED, tn_cost=Parameter.UNCHANGED, fn_cost=Parameter.UNCHANGED, fp_cost=Parameter.UNCHANGED)[source]#

Predict the target of new samples.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

The samples, as accepted by estimator.predict.

tp_costfloat or array-like, shape=(n_samples,), default=$UNCHANGED$

Cost of true positives. If float, then all true positives have the same cost. If array-like, then it is the cost of each true positive classification.

fp_costfloat or array-like, shape=(n_samples,), default=$UNCHANGED$

Cost of false positives. If float, then all false positives have the same cost. If array-like, then it is the cost of each false positive classification.

tn_costfloat or array-like, shape=(n_samples,), default=$UNCHANGED$

Cost of true negatives. If float, then all true negatives have the same cost. If array-like, then it is the cost of each true negative classification.

fn_costfloat or array-like, shape=(n_samples,), default=$UNCHANGED$

Cost of false negatives. If float, then all false negatives have the same cost. If array-like, then it is the cost of each false negative classification.

Returns:
class_labelsndarray of shape (n_samples,)

The predicted class.

Notes

If all costs are zero, then fp_cost=1 and fn_cost=1 are used to avoid division by zero.

predict_log_proba(X)#

Predict logarithm class probabilities for X using the fitted estimator.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

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

Returns:
log_probabilitiesndarray of shape (n_samples, n_classes)

The logarithm class probabilities of the input samples.

predict_proba(X)#

Predict class probabilities for X using the fitted estimator.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

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

Returns:
probabilitiesndarray of shape (n_samples, n_classes)

The class probabilities of the input samples.

score(X, y, sample_weight=None)#

Return the mean accuracy on the given test 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_predict_request(*, fn_cost='$UNCHANGED$', fp_cost='$UNCHANGED$', tn_cost='$UNCHANGED$', tp_cost='$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.

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

Metadata routing for fn_cost parameter in predict.

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

Metadata routing for fp_cost parameter in predict.

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

Metadata routing for tn_cost parameter in predict.

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

Metadata routing for tp_cost parameter in predict.

Returns:
selfobject

The updated object.

set_score_request(*, sample_weight='$UNCHANGED$')#

Request metadata passed to the score 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 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.

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