LoginSignup
2
3

More than 3 years have passed since last update.

Prophetお試し 備忘録

Last updated at Posted at 2020-09-22

概要

時系列データ処理の調べものをしてたときにProphetを見つけた。動作確認したときの備忘録
いつか使う気がするから…
- 実施期間: 2020年9月
- 環境:Ubuntu18.04LTS

動作確認用Conda仮想環境作成

Miniconda Install 備忘録の手順で新しい動作確認用仮想環境を作成しておく
その上で必要な下記パケージをインストールする

ProphetはPystanに依存しているのでPystanをインストールするが、Pystanも下記に依存するのでそれらを先にインストールする。
まず、c++コンパイラ周りのインストールを仮想環境で実行する(但し書き注意)

conda install gcc_linux-64 gxx_linux-64 -c anaconda

新しいターミナルを開いて、環境変数の設定

export CC=gcc_linux-64
export CXX=g++_linux-64
resorse ~/.bashrc

但し、"export -p"コマンドを実行し、いまの環境変数に上記の2つが含まれていたらコンパイラのインストールは不要

仮想環境のターミナルに戻り、以降全てのパケージをインストール

conda install -c conda-forge arviz
conda install -c conda-forge pystan
conda install -c conda-forge fbprophet
conda install -c conda-forge plotly

ちなみにconda仮想環境で、fbprophetをpipでインストールすると、盛大にエラーが発生した
conda-forgeのパケージでインストールすること

サンプルコードの実行

Quick StartのコードをJupyterで確認する。
はじめに、使用するサンプルデータをexample_wp_log_peyton_manning.csvとしてローカルに保存しておく
Prophetが扱うデータはPandas型であることが仕様

import pandas as pd
from fbprophet import Prophet
df = pd.read_csv('/home/hoge/example_wp_log_peyton_manning.csv')

Prophetのモデルを定義し

m = Prophet(daily_seasonality=True)
m.fit(df)

なお、Plotlyをインストールする前に上記を実行すると下記のエラーが出た
Importing plotly failed. Interactive plots will not work.
下記をインポートすることで回避可能だが、Plotlyが原因で発生したのかわからない

from matplotlib import pyplot as plt
from matplotlib.dates import MonthLocator, num2date
from matplotlib.ticker import FuncFormatter

予測結果(yhat)を入れるdataframeを作成し、そこに予測結果を入れる

future = m.make_future_dataframe(periods=365)
future.tail()
forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()

Screenshot from 2020-09-22 16-17-29.png
予測結果(青線)と実際の値(ドット)が重ねて表示できる

fig1 = m.plot(forecast)

Screenshot from 2020-09-22 16-19-36.png
Plotlyを使うと、かっこいいインタラクティブなチャート表示が可能
(Plotlyって知らなかった)

from fbprophet.plot import plot_plotly, plot_components_plotly
plot_plotly(m, forecast)

Screenshot from 2020-09-22 16-22-52.png

終わり
来週はtslearnを試してみる

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3