LogCost#
- class empulse.metrics.LogCost[source]#
Strategy for the Expected Log Cost metric.
The expected log cost is closely related to (weighted) cross-entropy / log loss: it replaces the predicted probability
sinCost’s linear cost function withlog(s)andlog(1 - s). Whentp_cost = tn_cost = -1andfp_cost = fn_cost = 0, the expected log cost reduces to the standard log loss.See also
expected_log_cost_loss: the underlying metric function.- gradient_boost_objective(y_true, y_score, **parameters)[source]#
Compute the gradient and hessian of the metric with respect to gradient boosting instances.
Unlike
Cost, the log-cost per-sample loss is not linear in the predicted probability, so the gradient and hessian are recomputed directly from the current (raw) boosting scores on every call, rather than through a precomputed constant.- Parameters:
- y_true: array-like of shape (n_samples,)
The ground truth labels.
- y_score: array-like of shape (n_samples,)
The current (raw, pre-sigmoid) boosting scores.
- parameters: float or array-like of shape (n_samples,)
The parameter values for the costs and benefits defined in the metric. If any parameter is a stochastic variable, you should pass values for their distribution parameters. You can set the parameter values for either the symbol names or their aliases.
If
float, the same value is used for all samples (class-dependent).If
array-like, the values are used for each sample (instance-dependent).
- Returns:
- gradientNDArray of shape (n_samples,)
The gradient of the metric loss with respect to the gradient boosting weights.
- hessianNDArray of shape (n_samples,)
The hessian of the metric loss with respect to the gradient boosting weights.
- logit_objective(features, y_true, C, l1_ratio, soft_threshold, fit_intercept, **parameters)[source]#
Build an object which computes the metric value and the gradient wrt logistic coefficients.
- Parameters:
- featuresNDArray of shape (n_samples, n_features)
The features of the samples.
- y_trueNDArray of shape (n_samples,)
The ground truth labels.
- Cfloat
Regularization strength parameter. Smaller values specify stronger regularization.
- l1_ratiofloat
The Elastic-Net mixing parameter, with range 0 <= l1_ratio <= 1. l1_ratio=0 corresponds to L2 penalty, l1_ratio=1 to L1 penalty.
- soft_thresholdbool
Indicator of whether soft thresholding is applied during optimization.
- fit_interceptbool
Specifies if an intercept should be included in the model.
- parametersfloat or NDArray of shape (n_samples,)
The parameter values for the costs and benefits defined in the metric.
If
float, the same value is used for all samples (class-dependent).If
array-like, the values are used for each sample (instance-dependent).
- Returns:
- logistic_objectiveLogCostLogitObjective
An object implementing the
LogitObjectiveinterface.
- optimal_rate(y_true, y_score, **parameters)[source]#
Compute the predicted positive rate to optimize the metric value.
- Parameters:
- y_true: array-like of shape (n_samples,)
The ground truth labels.
- y_score: array-like of shape (n_samples,)
The predicted (calibrated) probabilities.
- parameters: float or array-like of shape (n_samples,)
The parameter values for the costs and benefits defined in the metric. If any parameter is a stochastic variable, you should pass values for their distribution parameters. You can set the parameter values for either the symbol names or their aliases.
If
float, the same value is used for all samples (class-dependent).If
array-like, the values are used for each sample (instance-dependent).
- Returns:
- optimal_rate: float
The optimal predicted positive rate.
- optimal_threshold(y_true, y_score, **parameters)[source]#
Compute the classification threshold(s) to optimize the metric value.
The optimal threshold only depends on the (linear) cost matrix, not on the loss used to train the classifier, so it is identical to
Cost’s optimal threshold.- Parameters:
- y_true: array-like of shape (n_samples,)
The ground truth labels.
- y_score: array-like of shape (n_samples,)
The predicted (calibrated) probabilities.
- parameters: float or array-like of shape (n_samples,)
The parameter values for the costs and benefits defined in the metric. If any parameter is a stochastic variable, you should pass values for their distribution parameters. You can set the parameter values for either the symbol names or their aliases.
If
float, the same value is used for all samples (class-dependent).If
array-like, the values are used for each sample (instance-dependent).
- Returns:
- optimal_threshold: float | FloatNDArray
The optimal classification threshold(s).
- prepare_boost_objective(y_true, **parameters)#
Compute the gradient’s constant term of the metric wrt gradient boost.
- Parameters:
- y_trueNDArray of shape (n_samples,)
The ground truth labels.
- parametersfloat or NDArray of shape (n_samples,)
The parameter values for the costs and benefits defined in the metric. If any parameter is a stochastic variable, you should pass values for their distribution parameters. You can set the parameter values for either the symbol names or their aliases.
If
float, the same value is used for all samples (class-dependent).If
array-like, the values are used for each sample (instance-dependent).
- Returns:
- gradient_constNDArray of shape (n_samples, n_features)
The constant term of the gradient.
- property requires_dynamic_boost_objective#
LogCost’s per-sample loss is non-linear in the predicted probability.
- score(y_true, y_score, **parameters)[source]#
Compute the metric expected log cost loss.
- Parameters:
- y_true: array-like of shape (n_samples,)
The ground truth labels.
- y_score: array-like of shape (n_samples,)
The predicted (calibrated) probabilities.
- parameters: float or array-like of shape (n_samples,)
The parameter values for the costs and benefits defined in the metric. If any parameter is a stochastic variable, you should pass values for their distribution parameters. You can set the parameter values for either the symbol names or their aliases.
If
float, the same value is used for all samples (class-dependent).If
array-like, the values are used for each sample (instance-dependent).
- Returns:
- score: float
The expected log cost loss.