CSRateClassifier#

class empulse.models.CSRateClassifier(estimator, *, tp_cost=0.0, tn_cost=0.0, fn_cost=0.0, fp_cost=0.0, loss=None, pos_label=None)[source]#

Binary Classifier that sets the positive rate to optimize the cost-sensitive metric.

This classifier classifies the top fraction of samples (by predicted probability) as positive, where the fraction is determined by the optimal rate computed during fitting.

Read more in the User Guide.

See also

CSThresholdClassifier : Sets a per-sample decision threshold instead of a fixed rate, for when there is no fixed capacity constraint.

Parameters:
estimatorobject

A binary classifier that implements fit and predict_proba.

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 or predict method.

Note

It is not recommended to pass instance-dependent costs to the __init__ method. Instead, pass them to the fit or 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 fit or predict method.

Note

It is not recommended to pass instance-dependent costs to the __init__ method. Instead, pass them to the fit or 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 fit or predict method.

Note

It is not recommended to pass instance-dependent costs to the __init__ method. Instead, pass them to the fit or predict 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 fit or predict method.

Note

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

lossBaseMetric or None, default=None

The cost-sensitive metric to optimize.

  • If None, the optimal positive rate is computed based on tp_cost, tn_cost, fn_cost, and fp_cost.

  • If a BaseMetric, the optimal positive rate is computed based on the loss parameters provided to the fit or predict method.

pos_labelint, str, bool or None, default=None

The label of the positive class.

Attributes:
classes_numpy.ndarray of shape (n_classes,)

The class labels, taken from the wrapped estimator.

estimator_Estimator

The fitted classifier.

rate_float

The optimal positive rate determined during fitting.

property classes_#

The class labels, taken from the wrapped estimator.

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, *, tp_cost=Parameter.UNCHANGED, tn_cost=Parameter.UNCHANGED, fn_cost=Parameter.UNCHANGED, fp_cost=Parameter.UNCHANGED, **loss_params)#

Fit the model according to the given training data.

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

Training data.

yarray-like of shape (n_samples,)

Target values.

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.

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.

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.

**loss_paramsAny

Additional parameter to be passed to the loss function.

Returns:
self

Fitted estimator.

get_metadata_routing()#

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, **loss_params)#

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.

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.

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.

**loss_paramsdict

Additional keyword arguments to pass to the loss function if using a custom loss function.

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.

property rate_#

The optimal positive rate determined during fitting.

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_fit_request(*, fn_cost='$UNCHANGED$', fp_cost='$UNCHANGED$', tn_cost='$UNCHANGED$', tp_cost='$UNCHANGED$')#

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

Metadata routing for fn_cost parameter in fit.

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

Metadata routing for fp_cost parameter in fit.

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

Metadata routing for tn_cost parameter in fit.

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

Metadata routing for tp_cost parameter in fit.

Returns:
selfobject

The updated object.

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

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

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

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.