unit8co / darts

A python library for user-friendly forecasting and anomaly detection on time series.
https://unit8co.github.io/darts/
Apache License 2.0
8.14k stars 886 forks source link

[BUG] `RegressionModel` returning an instance of `self` when calling `fit` instead of training #879

Closed yngtdd closed 2 years ago

yngtdd commented 2 years ago

Describe the bug After instantiating a BayesianRidge regression model within the RegressionModel and calling fit, the method is returning an instance of self without training. lt looks like the self._fit_model method is not being called.

To Reproduce

I found this when training a BayesianRidge regression model within the Darts RegressionModel on my own data. I thought that I had missed something so I went back to try 00-quickstart.ipynb, and found that I can reproduce this behavior within that example. The particular part of that notebook that reproduces this:

import numpy as np

from sklearn.linear_model import BayesianRidge

from darts import concatenate
from darts.models import RegressionModel
from darts.dataprocessing.transformers import Scaler
from darts.datasets import AirPassengersDataset, MonthlyMilkDataset
from darts.utils.timeseries_generation import datetime_attribute_timeseries as dt_attr

series_air = AirPassengersDataset().load().astype(np.float32)
series_milk = MonthlyMilkDataset().load().astype(np.float32)

# set aside last 36 months of each series as validation set:
train_air, val_air = series_air[:-36], series_air[-36:]
train_milk, val_milk = series_milk[:-36], series_milk[-36:]

scaler = Scaler()
train_air_scaled, train_milk_scaled = scaler.fit_transform([train_air, train_milk])

air_covs = concatenate(
    [
        dt_attr(series_air.time_index, "month", dtype=np.float32) / 12,
        (dt_attr(series_air.time_index, "year", dtype=np.float32) - 1948) / 12,
    ],
    axis="component",
)

milk_covs = concatenate(
    [
        dt_attr(series_milk.time_index, "month", dtype=np.float32) / 12,
        (dt_attr(series_milk.time_index, "year", dtype=np.float32) - 1962) / 13,
    ],
    axis="component",
)

model = RegressionModel(lags=72, lags_future_covariates=[-6, 0], model=BayesianRidge())

model.fit(
    [train_air_scaled, train_milk_scaled], future_covariates=[air_covs, milk_covs]
)

Returns:

<darts.models.forecasting.regression_model.RegressionModel at 0x17eb6bfa0>

Expected behavior The regression model should fit on the given series rather than returning an instance of self.

System (please complete the following information):

dennisbader commented 2 years ago

Hi @yngtdd , this is intended and is mainly for allowing chained calls such as model.fit(...).predict(...). The model is still trained behind the scenes.

yngtdd commented 2 years ago

Oh! Sorry for the noise, I didn't realize that model was being trained!