[PYTHON] Visualize Prophet's time series forecasts more clearly with Plotly

Overview

Even if you don't have any knowledge about Facebook's time series prediction library Prophet and time series prediction, it is wonderful to be able to plot day of the week fluctuations, overall trend trends, etc. Regarding the plot, I just wrote in the official manual that "I can visualize it more interactively using plotly", but after playing around with it, I got the following points.

I hope it helps someone, and I will share the solution of the addiction point.

environment

macOS 10.15 Python 3.7.6 Prophet 0.6 Plotly 4.6.0

Notebook for this article

It has been uploaded below. https://github.com/nekodango/prophet_plotly_sample

Use Plotly on plots

As per the Official Tutorial, here /examples/example_wp_log_peyton_manning.csv) Download and load "example_wp_log_peyton_manning.csv" from.


import pandas as pd
from fbprophet import Prophet

df = pd.read_csv('example_wp_log_peyton_manning.csv')
m = Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=365)
forecast = m.predict(future)

The following code will display a normal plot.

fig = m.plot(forecast)

download.png

In this state

from fbprophet.plot import plot_plotly, plot_components_plotly
import plotly.offline as py
py.init_notebook_mode()

after

fig = plot_plotly(m, forecast)
py.iplot(fig)

Then, the plot using Plotly is displayed. スクリーンショット 2020-04-30 17.38.07.png

Similarly, plot_components

fig = m.plot_components(forecast)

To

fig = plot_components_plotly(m, forecast) 
py.iplot(fig)

If you rewrite it to, the display using Plotly will appear. スクリーンショット 2020-04-30 17.39.01.png

When I'm muzzling the plot, I'm wondering if such a useful thing is really free. Prophet terrifying.

Fitting point

1. Trend line and change point are not displayed unless specified as an option

Must be specified with the trend and changepoints options when calling plot_plotly.

plot_plotly(m, forecast, trend=True, changepoints=True)

スクリーンショット 2020-04-30 17.40.13.png

2. Plotly is not displayed on Google Colaboratory

I referred to the post on stackoverflow. https://stackoverflow.com/a/57903750

Import plotly.io and set renderers.default to colab.

from fbprophet.plot import plot_plotly, plot_components_plotly

import plotly.io as pio
pio.renderers.default = "colab"

So, when drawing, fig.show ().

fig = plot_plotly(m, forecast)
fig.show()

We have uploaded a notebook below so that you can try it out on Colab right away. https://github.com/nekodango/prophet_plotly_sample/blob/master/prophet_plotly_colab.ipynb

bonus

If you want to give your Plotly plot a title, you can:

fig.update_layout(title="plpt_plotly")

スクリーンショット 2020-04-30 17.41.30.png

I tried it later and was surprised, but even if I set Japanese as the title, it was displayed normally without any garbled characters. Prophet terrifying. (I said it twice because it's important)

Summary

It is convenient to be able to easily muzzle time series data on Colab. Please do try that out.

Recommended Posts

Visualize Prophet's time series forecasts more clearly with Plotly
[Time series with plotly] Dynamic visualization with plotly [python, stock price]
Easy time series prediction with Prophet
Forecasting time series data with Simplex Projection
Predict time series data with neural network
Create an animated time series map of coronavirus infection status with python + plotly
Visualize coronavirus infection status with Plotly [For beginners]
View details of time series data with Remotte