prophet icon indicating copy to clipboard operation
prophet copied to clipboard

How can I use numerical values instead of yhat values in a plot?

Open binoooh opened this issue 9 years ago • 8 comments

Hello fellow prophets I have a stock data which contains the following price data..

`ds,y 03/23/2016,742 03/28/2016,755 03/29/2016,742 03/30/2016,734.5

03/31/2016,750`

I know that forecast1[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail() produces the predicted values that are used when you do a plot. However, is there a way to use predicted numerical values when plotting?

If I do this..

np.exp(forecast[['yhat','yhat_lower','yhat_upper']].tail())

I get the predicted numerical values instead of yhat values..how can I use that when I do a plot?

Thanks in advance.

binoooh avatar Mar 30 '17 05:03 binoooh

I have absolutely no idea of what the values in the forecast mean... Even with np.exp() the values are not correct : they are in a good scale, but for real input data, the result of np.exp() is quite different.

You were so near to answer to my question in #139

guiled avatar Mar 30 '17 15:03 guiled

For what causes you don't like np.exp()? np.log it is reverse operation for it.

beloteloff avatar Apr 03 '17 08:04 beloteloff

Well, as input data, I have for example August 2016 : 2176 In the forecast I can find august 2016 : 7.715123 So I have a data for august 2016 : 2241 ... So the forecasted data for a past data is different than the original data... how can I presume the forecasted data in the future ?

guiled avatar Apr 04 '17 19:04 guiled

@arvindacumos if I understand correctly you have log-transformed your data, fit with Prophet, and now want to plot the forecast in the real (exponentiated) space. There unfortunately isn't any way to do this automatically. Built-in support for log-transforms is on the roadmap for the next version.

In the meantime, the code for plotting is pretty simple https://github.com/facebookincubator/prophet/blob/b999dd4aa4ff91b2f6501979c192743a7fc5e9b9/python/fbprophet/forecaster.py#L848-L876 If you replace self with m and put exp around things then it should work!

bletham avatar Apr 04 '17 22:04 bletham

@bletham is this correct?

` def plot(m, fcst, uncertainty=True, xlabel='ds', ylabel='y'):

ax.plot(np.exp(m.history['ds'].values), np.exp(m.history['y']), 'k.') `

Please confirm

binoooh avatar Apr 06 '17 11:04 binoooh

Just exponents around the y's. Something like this:

def plot(m, fcst, uncertainty=True, xlabel='ds', ylabel='y'):
    fig = plt.figure(facecolor='w', figsize=(10, 6))
    ax = fig.add_subplot(111)
    ax.plot(m.history['ds'].values, np.exp(m.history['y']), 'k.')
    ax.plot(fcst['ds'].values, np.exp(fcst['yhat']), ls='-', c='#0072B2')
    if 'cap' in fcst:
        ax.plot(fcst['ds'].values, np.exp(fcst['cap']), ls='--', c='k')
    if uncertainty:
        ax.fill_between(fcst['ds'].values, np.exp(fcst['yhat_lower']),
                        np.exp(fcst['yhat_upper']), color='#0072B2',
                        alpha=0.2)
    ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    fig.tight_layout()
    return fig

bletham avatar Apr 07 '17 03:04 bletham

@bletham thanks allot will try that one out.

binoooh avatar Apr 07 '17 06:04 binoooh

@bletham that worked thanks allot!!! am closing the issue

binoooh avatar Apr 08 '17 06:04 binoooh