# 時系列予測

時系列予測は、過去のタイムスタンプ付きデータにモデルをフィッティングして将来の値を予測するタスクです。このノートブックは、[FLAML](https://microsoft.github.io/FLAML/)を使用して時系列モデルをトレーニングし、将来の値を予測します。サポートされているモデルは次のとおりです:

* [Random Forest](https://en.wikipedia.org/wiki/Random_forest)
* [Extra Trees](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesRegressor.html)
* [LightGBM](https://lightgbm.readthedocs.io/)
* [XGBoost](https://en.wikipedia.org/wiki/XGBoost)
* [Prophet](https://facebook.github.io/prophet)
* [ARIMA](https://en.wikipedia.org/wiki/Autoregressive_integrated_moving_average)
* [SARIMAX](https://www.statsmodels.org/stable/generated/statsmodels.tsa.statespace.sarimax.SARIMAX.html)


このノートブックは、追加のEDAステップとホールドアウトテストも実行します。

### 想定される入力テーブル

このノートブックは、トレーニングの入力として次のテーブル形式を想定しています。

| **tstamp** | **..** | **value** |
|  --- | --- | --- |
| 2022/04/21 10:00 | .. | 50 |
| 2022/04/21 10:00 | .. | 30 |
| 2022/04/21 11:00 | .. | 70 |
| 2022/04/21 11:00 | .. | 30 |
| 2022/04/21 12:00 | .. | 100 |
| 2022/04/21 12:00 | .. | 30 |


デフォルトでは、tstamp_column="tstamp" および target_column="value" を想定していますが、任意のカラム名を指定することができます。

オプションで、[外生変数](https://timeseriesreasoning.com/contents/exogenous-and-endogenous-variables/)を提供することができます。例えば、[ドラッグストアチェーンの日次店舗売上](https://www.kaggle.com/competitions/rossmann-store-sales/)を予測する場合、exogenous_columns: weather、promotions、store_type を日次売上を説明する補助特徴量として指定することができます。

| **tstamp** | **weather** | **promotions** | **store_type** | **sales** |
|  --- | --- | --- | --- | --- |
| 1960-12-01 | cloudy | 2 | city_large | 459 |
| 1961-01-01 | sunny | 1 | contry_small | 935 |
| ... | ... |  |  |  |
|  |  |  |  |  |
| ... |  |  |  |  |
| 1965-12-01 | rainy | 0 | city_small | 886 |


### サンプル出力

forecast_length=30 が指定されている場合、トレーニングデータに対して+30件の追加レコードが予測されます。一方、test_tableが提供されている場合は、テストデータの予測が行われます。test_tableには、少なくともtstamp_column(デフォルト設定では"tstamp")が必要です。target_column(デフォルトでは"value")は、output_tableに付加されます。

tstamp_columnに有効な日時値がない場合は、pesudo_tstampが使用され、それらに加えて結果に追加されます。

| **tstamp** | **value** |
|  --- | --- |
| 1960-12-01 | 0.29304519295692444 |
| 1961-01-01 | 0.00487339636310935 |
| ... | ... |
| 1965-12-01 | 0.5266873240470886 |


予測結果の可視化は次のとおりです:
![](/assets/72061491.f86a6dc8602721837e43b7c44d7179d7564b702ff905e73ef0ada9babe5af4c1.3cb60505.png)

![](/assets/72061490.61770e0241024ecabb344fea215601a93a19e55b47898848f5c0eda98eaaec88.3cb60505.png)

ワークフローの例

サンプルワークフローは[Treasure Boxesのこちら](https://github.com/treasure-data/treasure-boxes/blob/automl/machine-learning-box/automl/ts_forecast.dig)をご覧ください。

+run_ts_forecast:
ipynb>:
notebook: ts_forecast
train_table: ml_datasets.ts_airline
tstamp_column: period
forecast_length: 30
output_table: ml_test.ts_airline_predicted

### パラメータ

| パラメータ名 | コンソール上のパラメータ | 説明 | デフォルト値 |
|  --- | --- | --- | --- |
| docker.task_mem | Docker Task Mem | タスクのメモリサイズ。契約しているティアに応じて、64g、128g(デフォルト)、256g、384g、または512gの値を指定可能 | 128g |
| train_table | Train Table | トレーニングに使用するTDテーブルをdbname.table_name形式で指定 | - |
| forecast_length | Forecast Length | 予測出力の長さ。test_tableまたはforecast_lengthのいずれかが必要 | - |
| forecast_freq | Forecast Freq | 予測の明示的な頻度。使用可能な値: D - 日次、W - 週次、M - 月次、Q - 四半期、Y - 年次。指定されていない場合、値はデータから推測されます | - |
| test_table | Test Table | 予測に使用するTDテーブル名。test_tableまたはforecast_lengthのいずれかが必要 | - |
| tstamp_column | Tstamp Column | 時系列データをソートするためのタイムスタンプカラム | tstamp |
| target_column | Target Column | ラベルに使用するカラム名 | value |
| output_table | Output Table | 予測結果をエクスポートするTDテーブル名 | - |
| output_mode | Output Mode | output_tableをエクスポートする際の出力モード: overwrite/replace または append。通常は指定不要で、準リアルタイム予測の場合は"append"を指定 | overwrite |
| exogenous_columns | Exogenous Columns | 予測入力として使用できるカラム。train_table内のすべてのカラムを選択する場合は"*"を使用可能 | - |
| ignore_columns | Ignore Columns | 外生変数として無視するカラム | time |
| estimators | Estimators | 時系列予測に使用するエスティメーター。サポートされているエスティメーター: prophet、arima、lgbm | prophet,arima,lgbm,xgboost,xgb_limitdepth |
| time_limit | Time Limit | トレーニング時間予算のソフトリミット(秒単位) | 60 * 60 |
| sampling_threshold | Sampling Threshold | トレーニングデータのサンプリングに使用する閾値 | 10_000_000 |
| hide_table_contents | Hide Table Contents | テーブルコンテンツの表示を抑制 | false |
| calibration | Calibration | trueの場合、出力値が調整されます | false |