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.07k stars 879 forks source link

backtests' example: AttributeError: 'numpy.float64' object has no attribute 'width' #244

Closed juanpabloaj closed 3 years ago

juanpabloaj commented 3 years ago

When I was trying to follow the article backtests' example

https://medium.com/unit8-machine-learning-publication/darts-time-series-made-easy-in-python-5ac2947a8878

I got this error

AttributeError                            Traceback (most recent call last)
<ipython-input-14-9213dab113d8> in <module>
      2 series.plot(label='data')
      3 for i, m in enumerate(models):
----> 4     err = mape(backtests[i], series)
      5     backtests[i].plot(lw=3, label='{}, MAPE={:.2f}%'.format(m, err))
      6 plt.title('Backtests with 3-months forecast horizon')

~/.pyenv/versions/3.8.3/envs/keras/lib/python3.8/site-packages/darts/metrics/metrics.py in wrapper_multivariate_support(*args, **kwargs)
     31         series2 = kwargs['series2'] if 'series2' in kwargs else args[0] if 'series1' in kwargs else args[1]
     32 
---> 33         raise_if_not(series1.width == series2.width, "The two TimeSeries instances must have the same width.", logger)
     34 
     35         num_series_in_args = int('series1' not in kwargs) + int('series2' not in kwargs)

AttributeError: 'numpy.float64' object has no attribute 'width'

To Reproduce

I used this part of the code

from darts.models import Prophet
models = [ExponentialSmoothing(), Prophet()]
backtests = [model.backtest(series,
                            start=pd.Timestamp('19550101'),
                            forecast_horizon=3)
             for model in models]

from darts.metrics import mape
series.plot(label='data')
for i, m in enumerate(models):
    err = mape(backtests[i], series)
    backtests[i].plot(lw=3, label='{}, MAPE={:.2f}%'.format(m, err))
plt.title('Backtests with 3-months forecast horizon')
plt.legend()

Expected behavior I would like to get the article's plot and results.

System (please complete the following information): u8darts==0.5.0 fbprophet==0.6 Python 3.8.3

Additional context

I don't know if this message is expected but in this part of the code

backtests = [model.backtest(series,
                            start=pd.Timestamp('19550101'),
                            forecast_horizon=3)
             for model in models]

I get this message many times

INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
pennfranc commented 3 years ago

Hi @juanpabloaj, nice catch! We recently updated the name of the backtest function to historical_forecasts, which is not yet reflected in the medium article. In the meantime you can just replace model.backtest with model.historical_forecasts and that should do the trick! If you still run into problems please let us know.

hrzn commented 3 years ago

The Medium article has been updated, thanks for raising the issue @juanpabloaj

juanpabloaj commented 3 years ago

thanks, maybe in some part of the article you could add the version of darts used in this demo to avoid future confusion.

thanks again for your fast answer, the project, and your work in general 😄