NeuralProphet 0.2 : ノートブック : 休日と特殊イベントのモデリング (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 07/22/2021 (Beta 0.2.7)
* 本ページは、NeuralProphet の以下のドキュメントを翻訳した上で適宜、補足説明したものです:
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
- 人工知能研究開発支援
- 人工知能研修サービス(経営者層向けオンサイト研修)
- テクニカルコンサルティングサービス
- 実証実験(プロトタイプ構築)
- アプリケーションへの実装
- 人工知能研修サービス
- PoC(概念実証)を失敗させないための支援
- テレワーク & オンライン授業を支援
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
- ウェビナー運用には弊社製品「ClassCat® Webinar」を利用しています。
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション |
E-Mail:sales-info@classcat.com ; WebSite: https://www.classcat.com/ ; Facebook |
NeuralProphet 0.2 : ノートブック : 休日と特殊イベントのモデリング
モデル化したい休日や他の繰り返しイベントを持つ場合、それらからデータフレームを作成しなければなりません。それは 2 つのカラム (event と ds) と休日の各発生のための行を持ちます。それは過去 (履歴データが続く限り後方に) と未来 (予測が成されている限り) の両者の休日の総ての発生を含まなければなりません。それらが未来に繰り返されない場合、Prophet はそれらをモデル化してから予測に含ません。
イベントは加法的か乗法的コンポーネントのいずれかとして追加できます。
更にイベントの前後の日を含めるためにウィンドウを定義できます。
例として Peyton Manning の Wikipedia のログ daily ページビューの時系列を使用します。最初に、データをロードします :
if 'google.colab' in str(get_ipython()):
!pip install git+https://github.com/ourownstory/neural_prophet.git # may take a while
#!pip install neuralprophet # much faster, but may not have the latest upgrades/bugfixes
data_location = "https://raw.githubusercontent.com/ourownstory/neural_prophet/master/"
else:
data_location = "../"
import pandas as pd
from neuralprophet import NeuralProphet
df = pd.read_csv(data_location + "example_data/wp_log_peyton_manning.csv")
ここで過去のイベントと未来のイベントを含む、Peyton Manning のプレーオフ出場の総ての日付を含むデータフレームを作成します :
## user specified events
# history events
playoffs = pd.DataFrame({
'event': 'playoff',
'ds': pd.to_datetime([
'2008-01-13', '2009-01-03', '2010-01-16',
'2010-01-24', '2010-02-07', '2011-01-08',
'2013-01-12', '2014-01-12', '2014-01-19',
'2014-02-02', '2015-01-11', '2016-01-17',
'2016-01-24', '2016-02-07',
]),
})
superbowls = pd.DataFrame({
'event': 'superbowl',
'ds': pd.to_datetime([
'2010-02-07', '2012-02-05', '2014-02-02',
'2016-02-07',
]),
})
events_df = pd.concat((playoffs, superbowls))
加法的イベント
ひとたびテーブルが作成されれば、イベント効果は add_events 関数でそれらを追加することでモデルに含まれます。
デフォルトでは、イベントは加法的イベントとしてモデル化されます。ここでは、プレーオフの両者を加法的イベントとしてモデル化します。
更に loss_func を ‘MSE’ に変更します、重みに適合しているイベントが幾つかの稀な外れ値であるためです。通常はモデルを少ない外れ値で歪めることを望まないので、デフォルトの損失関数は ‘Huber’ です。
# NeuralProphet Object
m = NeuralProphet(loss_func="MSE")
# set the model to expect these events
m = m.add_events(["playoff", "superbowl"])
# create the data df with events
history_df = m.create_df_with_events(df, events_df)
# fit the model
metrics = m.fit(history_df, freq="D")
# forecast with events
future = m.make_future_dataframe(history_df, events_df, periods=30, n_historic_predictions=len(df))
forecast = m.predict(df=future)
イベント効果は予測データフレームで確認することができます :
events = forecast[(forecast['event_playoff'].abs() + forecast['event_superbowl'].abs()) > 0]
events.tail()
イベント効果はプロットにも現れます、そこではスーパーボウルのための特に大きなスパイクとともに、プレーオフ出場の前後の日々にスパイクがあることを見ます :
## plotting forecasts
fig = m.plot(forecast)
## plotting components
fig_comp = m.plot_components(forecast)
## plotting parameters
fig_param = m.plot_parameters()
イベント・ウィンドウ
引数 lower_window と upper_window を含めることもできます、これらは休日を日付前後の [lower_window, upper_window] days に拡張します。
例えば、クリスマスに加えてクリスマスイブを含めることを望む場合、lower_window=-1, upper_window=0 を含めます。感謝祭に加えてブラックフライデーを使用したい場合、lower_window=0, upper_window=1 を含めます。
m = NeuralProphet(loss_func="MSE")
# set event configs to NeuralProphet object with windows
m = m.add_events(["playoff"], upper_window=1)
m = m.add_events(["superbowl"], lower_window=-1, upper_window=1)
# create the data df with events
history_df = m.create_df_with_events(df, events_df)
# fit the model
metrics = m.fit(history_df, freq="D")
# make future dataframe with events known in future
future = m.make_future_dataframe(df=history_df, events_df=events_df, periods=365, n_historic_predictions=len(df))
forecast = m.predict(df=future)
## plotting parameters
fig = m.plot(forecast)
fig_param = m.plot_parameters()
乗法的イベント
m = NeuralProphet(loss_func="MSE")
# set event configs to NeuralProphet object with windows
m = m.add_events(["playoff"], upper_window=1)
m = m.add_events(["superbowl"], lower_window=-1, upper_window=1, mode="multiplicative")
# create the data df with events
history_df = m.create_df_with_events(df, events_df)
# fit the model
metrics = m.fit(history_df, freq="D")
# make future dataframe with events known in future
future = m.make_future_dataframe(history_df, events_df, periods=30, n_historic_predictions=len(df))
forecast = m.predict(df=future)
## plotting components
fig_comp = m.plot(forecast)
# plot parameters
fig_param = m.plot_parameters()
国固有の休日
m = NeuralProphet()
# add the country specific holidays
m = m.add_country_holidays("US")
# fit the model
metrics = m.fit(df, freq="D")
# make future dataframe with events known in future
future = m.make_future_dataframe(df=df, periods=30, n_historic_predictions=len(df))
forecast = m.predict(df=future)
## plotting components
fig = m.plot(forecast)
fig_param = m.plot_parameters()
以上