unit8co / darts

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

[BUG]: 'numpy.ndarray' object has no attribute 'get_color' while plotting NaiveSessional() forecast #1051

Closed iamMaverick closed 2 years ago

iamMaverick commented 2 years ago

Describe the bug A clear and concise description of what the bug is.

While plotting a naive forecast an attributeError occured: 'numpy.ndarray object has no attribute get_color'

To Reproduce Steps to reproduce the behavior, preferably code snippet. I have a subset of len 6188 'train' on which I trained the darts.NaiveSessional model.

naive_model = models.NaiveSeasonal(K=1) naive_model.fit(train) naive_forecast = naive_model.predict(1) naive_forecast.plot(label='Forecast')


AttributeError Traceback (most recent call last) c:\Users\Itcomplex\Documents\scripts\Predictive Analytics\Work\crypto_value_prediction_hourly.ipynb Cell 25' in <cell line: 4>() 2 naive_model.fit(train) 3 naive_forecast = naive_model.predict(1) ----> 4 naive_forecast.plot(label='Forecast')

File c:\Users\Itcomplex\anaconda3\envs\env\lib\site-packages\darts\timeseries.py:2450, in TimeSeries.plot(self, new_plot, central_quantile, low_quantile, high_quantile, *args, *kwargs) 2447 kwargs["label"] = label_to_use 2449 p = central_series.plot(args, **kwargs) -> 2450 color_used = p[0].get_color() 2451 kwargs["alpha"] = alpha if alpha is not None else alpha_confidence_intvls 2453 # Optionally show confidence intervals

AttributeError: 'numpy.ndarray' object has no attribute 'get_color'

Expected behavior A clear and concise description of what you expected to happen.

Should have plotted the predicted timeseries.

System (please complete the following information):

Additional context Add any other context about the problem here.

hrzn commented 2 years ago

Thanks for flagging. Indeed, there's a bug when plotting a series of length 1.

iamMaverick commented 2 years ago

Thanks for flagging. Indeed, there's a bug when plotting a series of length 1.

Thank you Sir!

martinb-ai commented 2 years ago

Hi there @hrzn , is there any resolution to this bug? 😄 Still seeing it.

image

hrzn commented 2 years ago

It's not fixed yet - for now I would recommend not plotting series of length 1 (that's kind of awkward anyway, no? we might raise an exception instead of trying to plot).

martinb-ai commented 2 years ago

We are only allowing a forecast of 2 or greater days.

But maybe I am not following...wouldn't training a forecasting model with an output of next day event be useful? If so , we would have to plot next day for both predictions and historical backtesting.

Currently our default is for next 5 days but if we switch to only next day, it will create this error.

hrzn commented 2 years ago

Obtaining a forecast of length 1 (e.g. for next day) is definitely useful. But plotting this forecast in isolation would result in a point (not a line); not sure we want to support this kind of plots. If you backtest a model for day-ahead forecasts, even if the horizon is 1, the result of the historical forecasts will typically be a longer series, so this is not an issue. Does this clarify things?

martinb-ai commented 2 years ago

Oh I see, so the problem is plotting the prediction. I will have to come up with an elegant way avoid that issue. Thanks!

hrzn commented 2 years ago

If you use historical_forecasts() with last_points_only=True you should be fine.

hrzn commented 2 years ago

Closing as duplicate of https://github.com/unit8co/darts/issues/650

martinb-ai commented 2 years ago

Hi @hrzn still the same issue:

image
historical_fcast = best_model.historical_forecasts(
            ticker_series,
            start=train_split,
            forecast_horizon=forecast_horizon,
            retrain=False,
            verbose=True,
            last_points_only=True,
        )

Did I forget something?

hrzn commented 2 years ago

Hi @martinb-bb, our current intent was not to fix it (although we could definitely improve the error message), although given how recurring of an issue this is, I'm having second thoughts. We can maybe plot it as a simple marker 'o' or something (let me know if you have thoughts on this).

martinb-ai commented 2 years ago

Hi @hrzn ! Yes, normally I wouldn't push on a specific bug but I have been getting many requests on providing a 1 day forecast.

A simple marker would completely suffice. ☺️ After all, it is simply only one point.

I am also open to other options!

hrzn commented 2 years ago

I'm proposing something like this: https://github.com/unit8co/darts/pull/1209

martinb-ai commented 2 years ago

Interesting. So it will plot a single point with arrows to showcase the range? What is the series is not probabilistic?

That is quite cool.

Question: Historical forecast will remain the same correct? It works perfect with single day forecast.

hrzn commented 2 years ago

Interesting. So it will plot a single point with arrows to showcase the range? What is the series is not probabilistic?

Yes the bars are meant to show the low/high quantiles, similar to series of length > 1. If the series is not probabilistic it only shows the point, without the range.

Question: Historical forecast will remain the same correct? It works perfect with single day forecast.

Yes this PR only affects plotting, nothing else.

martinb-ai commented 2 years ago

This is great! Amazing PR.

Thank you @hrzn ☺️