10000 TST invalid init parameters for losses by lorentzenchr · Pull Request #22407 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

TST invalid init parameters for losses #22407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions sklearn/_loss/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# - SGDRegressor, SGDClassifier
# - Replace link module of GLMs.

import numbers
import numpy as np
from scipy.special import xlogy
from ._loss import (
Expand All @@ -34,6 +35,7 @@
LogitLink,
MultinomialLogit,
)
from ..utils import check_scalar
from ..utils._readonly_array_wrapper import ReadonlyArrayWrapper
from ..utils.stats import _weighted_percentile

Expand Down Expand Up @@ -604,11 +606,14 @@ class PinballLoss(BaseLoss):
need_update_leaves_values = True

def __init__(self, sample_weight=None, quantile=0.5):
if quantile <= 0 or quantile >= 1:
raise ValueError(
"PinballLoss aka quantile loss only accepts "
f"0 < quantile < 1; {quantile} was given."
)
check_scalar(
quantile,
"quantile",
target_type=numbers.Real,
min_val=0,
max_val=1,
include_boundaries="neither",
)
super().__init__(
closs=CyPinballLoss(quantile=float(quantile)),
link=IdentityLink(),
Expand Down Expand Up @@ -725,6 +730,14 @@ class HalfTweedieLoss(BaseLoss):
"""

def __init__(self, sample_weight=None, power=1.5):
check_scalar(
power,
"power",
target_type=numbers.Real,
include_boundaries="neither",
min_val=-np.inf,
max_val=np.inf,
)
super().__init__(
closs=CyHalfTweedieLoss(power=float(power)),
link=LogLink(),
Expand Down
36 changes: 36 additions & 0 deletions sklearn/_loss/tests/test_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,42 @@ def test_init_gradient_and_hessian_raises(loss, params, err_msg):
gradient, hessian = loss.init_gradient_and_hessian(n_samples=5, **params)


@pytest.mark.parametrize(
"loss, params, err_type, err_msg",
[
(
PinballLoss,
{"quantile": None},
TypeError,
"quantile must be an instance of float, not NoneType.",
),
(
PinballLoss,
{"quantile": 0},
ValueError,
"quantile == 0, must be > 0.",
),
(PinballLoss, {"quantile": 1.1}, ValueError, "quantile == 1.1, must be < 1."),
(
HalfTweedieLoss,
{"power": None},
TypeError,
"power must be an instance of float, not NoneType.",
),
(
HalfTweedieLoss,
{"power": np.inf},
ValueError,
"power == inf, must be < inf.",
),
],
)
def test_loss_init_parameter_validation(loss, params, err_type, err_msg):
"""Test that loss raises errors for invalid input."""
with pytest.raises(err_type, match=err_msg):
loss(**params)


@pytest.mark.parametrize("loss", LOSS_INSTANCES, ids=loss_instance_name)
def test_loss_pickle(loss):
"""Test that losses can be pickled."""
Expand Down
0