Skip to content

Commit

Permalink
Merge pull request #132 from decargroup/bugfix/131-score_trajectory-c…
Browse files Browse the repository at this point in the history
…an-return-score-worse-than-error_score

`score_trajectory` can return score worse than error score
  • Loading branch information
sdahdah authored Jan 18, 2023
2 parents a9f1f51 + 8d62291 commit 2bd980c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 DECAR Systems Group
Copyright (c) 2022 DECAR

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
15 changes: 13 additions & 2 deletions pykoop/koopman_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2821,7 +2821,9 @@ def make_scorer(
an error has occured in estimator fitting. If set to ``'raise'``, a
:class:`ValueError` is raised. If a numerical value is given, a
:class:`sklearn.exceptions.FitFailedWarning` warning is raised and
the specified score is returned.
the specified score is returned. The error score defines the worst
possible score. If a score is finite but lower than the error
score, the error score will be returned instead.
multistep : bool
If true, predict using :func:`predict_trajectory`. Otherwise,
Expand Down Expand Up @@ -3204,7 +3206,9 @@ def score_trajectory(
error has occured in estimator fitting. If set to ``'raise'``, a
:class:`ValueError` is raised. If a numerical value is given, a
:class:`sklearn.exceptions.FitFailedWarning` warning is raised and the
specified score is returned.
specified score is returned. The error score defines the worst possible
score. If a score is finite but lower than the error score, the error
score will be returned instead.
min_samples : int
Number of samples in initial condition.
Expand Down Expand Up @@ -3298,6 +3302,13 @@ def score_trajectory(
# Invert losses
if regression_metric not in greater_is_better:
score *= -1
# If score is worse than error score, return that.
if np.isfinite(error_score) and (score < error_score):
warnings.warn(
f'Score `score={score}` is finite, but is lower than error '
f'score `error_score={error_score}`. Returning error score.',
sklearn.exceptions.FitFailedWarning)
return error_score
return score


Expand Down
16 changes: 16 additions & 0 deletions tests/test_koopman_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,22 @@ class TestKoopmanPipelineScore:
False,
None,
),
# Finite score worse than error score should return error score.
(
np.array([
[1e-2, 1e-3],
]).T,
np.array([
[1e5, 1e6],
]).T,
None,
1,
'neg_mean_squared_error',
-100,
1,
False,
-100,
),
],
)
def test_score_trajectory(
Expand Down

0 comments on commit 2bd980c

Please sign in to comment.