Skip to content
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

Fix: Using gridsearch with use_fitted_values=True raises unexpected error #2222

Merged
merged 9 commits into from
Feb 26, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ but cannot always guarantee backwards compatibility. Changes that may **break co
- Fixed a bug in probabilistic `LinearRegressionModel.fit()`, where the `model` attribute was not pointing to all underlying estimators. [#2205](https://github.com/unit8co/darts/pull/2205) by [Antoine Madrona](https://github.com/madtoinou).
- Raise an error in `RegressionEsembleModel` when the `regression_model` was created with `multi_models=False` (not supported). [#2205](https://github.com/unit8co/darts/pull/2205) by [Antoine Madrona](https://github.com/madtoinou).
- Fixed a bug in `coefficient_of_variaton()` with `intersect=True`, where the coefficient was not computed on the intersection. [#2202](https://github.com/unit8co/darts/pull/2202) by [Antoine Madrona](https://github.com/madtoinou).
- Fixed a bug in `gridsearch()` with `use_fitted_values=True`, where the model was not propely instantiated for sanity checks. [#2222](https://github.com/unit8co/darts/pull/2222) by [Antoine Madrona](https://github.com/madtoinou).

### For developers of the library:

Expand Down
22 changes: 20 additions & 2 deletions darts/models/forecasting/forecasting_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1483,10 +1483,28 @@ def gridsearch(
logger,
)

if not isinstance(parameters, dict):
raise_log(
ValueError(
f"`parameters` should be a dictionary, received a: {type(parameters)}."
)
)

if not all(isinstance(params, list) for params in parameters.values()):
madtoinou marked this conversation as resolved.
Show resolved Hide resolved
raise_log(
ValueError(
"Every value in the `parameters` dictionary should be a list (possibly with only one element)."
),
logger,
)

if use_fitted_values:
raise_if_not(
hasattr(model_class(), "fitted_values"),
"The model must have a fitted_values attribute to compare with the train TimeSeries",
hasattr(
model_class(**{k: v[0] for k, v in parameters.items()}),
"fitted_values",
),
"The model must have a fitted_values attribute to compare with the train TimeSeries (local models)",
logger,
)

Expand Down
2 changes: 1 addition & 1 deletion darts/tests/models/forecasting/test_4theta.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_best_model(self):
linear_series = lt(length=50)
series = sine_series + linear_series
train_series, val_series = series.split_before(series.time_index[-10])
thetas = np.linspace(-3, 3, 30)
thetas = list(np.linspace(-3, 3, 30))
best_model, _, _ = FourTheta.select_best_model(train_series, thetas)
model = FourTheta(
random.choice(thetas),
Expand Down
Loading