Cost-weighted service loss regressor¶
This section documents the cost-weighted service loss (CWSL) regressor used in model selection workflows.
The CWSL regressor supports learning and ranking models based on asymmetric service risk preferences.
eb_evaluation.model_selection.cwsl_regressor
¶
scikit-learn-style estimator wrapper for cost-aware model selection.
This module defines CWSLRegressor, a lightweight wrapper around ElectricBarometer
that exposes a familiar fit / predict / score API.
The estimator selects among a set of candidate models using Cost-Weighted Service Loss (CWSL) as the selection criterion. Candidate models are trained using their native objectives (e.g., squared error), but selection is performed using asymmetric operational costs.
The primary cost preference can be summarized by the cost ratio:
where \(c_u\) is the underbuild (shortfall) cost per unit and \(c_o\) is the overbuild (excess) cost per unit.
CWSLRegressor
¶
scikit-learn-style estimator that selects among candidate models using CWSL.
This class wraps ElectricBarometer and exposes:
fit(X, y): perform cost-aware model selectionpredict(X): predict using the selected best estimatorscore(X, y): return a sklearn-style score based on negative CWSL
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models
|
dict[str, Any]
|
Mapping of candidate model name to an unfitted estimator implementing:
Models may be scikit-learn regressors, pipelines, or EB adapters. |
required |
cu
|
float
|
Underbuild (shortfall) cost per unit. Must be strictly positive. |
2.0
|
co
|
float
|
Overbuild (excess) cost per unit. Must be strictly positive. |
1.0
|
tau
|
float
|
Tolerance parameter forwarded to ElectricBarometer for optional diagnostics (e.g., HR@tau). |
2.0
|
training_mode
|
selection_only
|
Training behavior; currently ElectricBarometer supports selection-only mode. |
"selection_only"
|
refit_on_full
|
bool
|
Refit behavior after selection.
|
True
|
selection_mode
|
(holdout, cv)
|
How selection is performed:
|
"holdout"
|
cv
|
int
|
Number of folds when |
3
|
validation_fraction
|
float
|
Fraction of samples used for validation when |
0.2
|
random_state
|
int | None
|
Seed used for internal shuffling and CV splitting. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
selector_ |
ElectricBarometer | None
|
Underlying selector instance used in the most recent fit. |
best_name_ |
str | None
|
Name of the winning model. |
best_estimator_ |
Any | None
|
Fitted winning estimator. |
results_ |
Any
|
Comparison table produced by ElectricBarometer (typically a pandas DataFrame). |
validation_cwsl_ |
float | None
|
CWSL score of the winning model on validation (holdout) or mean CV. |
validation_rmse_ |
float | None
|
RMSE score of the winning model on validation or mean CV. |
validation_wmape_ |
float | None
|
wMAPE score of the winning model on validation or mean CV. |
n_features_in_ |
int | None
|
Number of features observed during fit. |
Notes
score returns negative CWSL to align with sklearn conventions (higher is better).
r_
property
¶
Cost ratio.
Returns:
| Type | Description |
|---|---|
float
|
The ratio \(R = c_u / c_o\). |
fit(X, y, sample_weight=None)
¶
Fit CWSLRegressor on (X, y) by delegating selection to ElectricBarometer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like of shape (n_samples, n_features)
|
Feature matrix. |
required |
y
|
array-like of shape (n_samples,)
|
Target vector. |
required |
sample_weight
|
numpy.ndarray of shape (n_samples,)
|
Optional per-sample weights. In CV mode these are passed through to ElectricBarometer so validation folds are cost-weighted. In holdout mode, they are passed through to ElectricBarometer (behavior depends on EB). |
None
|
Returns:
| Type | Description |
|---|---|
CWSLRegressor
|
Fitted instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If X/y shapes are incompatible or sample_weight length mismatches. |
predict(X)
¶
Generate predictions from the selected best estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like of shape (n_samples, n_features)
|
|
required |
Returns:
| Type | Description |
|---|---|
numpy.ndarray of shape (n_samples,)
|
Predicted values. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called before |
score(X, y, sample_weight=None)
¶
Compute a sklearn-style score using negative CWSL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like of shape (n_samples, n_features)
|
Feature matrix. |
required |
y
|
array-like of shape (n_samples,)
|
Target vector. |
required |
sample_weight
|
numpy.ndarray of shape (n_samples,)
|
Optional per-sample weights passed to CWSL. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Negative CWSL on the provided data (higher is better). |
get_params(deep=True)
¶
Minimal sklearn-compatible get_params implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deep
|
bool
|
Included for sklearn compatibility. Nested parameter extraction is not performed in this lightweight implementation. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Parameter mapping. |
set_params(**params)
¶
Minimal sklearn-compatible set_params implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**params
|
Parameters to set on this instance. |
{}
|
Returns:
| Type | Description |
|---|---|
CWSLRegressor
|
Updated instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an invalid parameter name is provided. |