| name | scientific-time-series-forecasting |
| description | ML 時系列予測スキル。Prophet/NeuralProphet・N-BEATS・
Temporal Fusion Transformer (TFT)・時系列特徴量エンジニアリング・
バックテスト・多段階予測・アンサンブル予測。
|
| tu_tools | [{"key":"biotools","name":"bio.tools","description":"時系列予測ツール検索"}] |
Scientific Time Series Forecasting
深層学習・ML ベースの時系列予測パイプラインを提供し、
Prophet から Transformer まで最新手法を網羅する。
When to Use
- Prophet/NeuralProphet で季節性時系列を予測するとき
- 深層学習 (N-BEATS/TFT) で高精度予測するとき
- 時系列特徴量エンジニアリングでラグ・ローリング特徴を生成するとき
- バックテストで予測性能を厳密に評価するとき
- 複数モデルのアンサンブル予測をするとき
- 多変量・多段階予測をするとき
Note: 古典時系列 (ARIMA/STL/FFT) は scientific-time-series を参照。
Quick Start
1. Prophet / NeuralProphet
import numpy as np
import pandas as pd
def prophet_forecast(df, date_col, value_col, periods=30,
freq="D", yearly=True, weekly=True,
changepoint_prior=0.05):
"""
Prophet 時系列予測。
Parameters:
df: pd.DataFrame — 時系列データ
date_col: str — 日付カラム
value_col: str — 値カラム
periods: int — 予測期間
freq: str — 頻度 ("D" / "H" / "M")
yearly: bool — 年次季節性
weekly: bool — 週次季節性
changepoint_prior: float — 変化点感度
"""
from prophet import Prophet
prophet_df = df[[date_col, value_col]].rename(
columns={date_col: "ds", value_col: "y"})
model = Prophet(
yearly_seasonality=yearly,
weekly_seasonality=weekly,
changepoint_prior_scale=changepoint_prior)
model.fit(prophet_df)
future = model.make_future_dataframe(periods=periods, freq=freq)
forecast = model.predict(future)
merged = forecast.merge(prophet_df, on="ds", how="left")
valid = merged.dropna(subset=["y"])
mae = np.mean(np.abs(valid["y"] - valid["yhat"]))
mape = np.mean(np.abs((valid["y"] - valid["yhat"]) / valid["y"])) * 100
fig1 = model.plot(forecast)
fig1.savefig("prophet_forecast.png", dpi=150, bbox_inches="tight")
fig2 = model.plot_components(forecast)
fig2.savefig("prophet_components.png", dpi=150, bbox_inches="tight")
print(f"Prophet: {periods} periods, MAE={mae:.4f}, MAPE={mape:.1f}%")
return {"forecast": forecast, "model": model,
"mae": mae, "mape": mape}
def neuralprophet_forecast(df, date_col, value_col, periods=30,
n_lags=60, n_forecasts=30):
"""
NeuralProphet 時系列予測 (AR-Net)。
Parameters:
df: pd.DataFrame — 時系列データ
date_col: str — 日付カラム
value_col: str — 値カラム
periods: int — 予測期間
n_lags: int — 自己回帰ラグ数
n_forecasts: int — 多段階予測ステップ
"""
from neuralprophet import NeuralProphet
np_df = df[[date_col, value_col]].rename(
columns={date_col: "ds", value_col: "y"})
model = NeuralProphet(
n_lags=n_lags, n_forecasts=n_forecasts,
yearly_seasonality=True, weekly_seasonality=True,
learning_rate=0.01, epochs=100)
metrics = model.fit(np_df, freq="D")
future = model.make_future_dataframe(np_df, periods=periods, n_historic_predictions=True)
forecast = model.predict(future)
fig = model.plot(forecast)
fig.savefig("neuralprophet_forecast.png", dpi=150, bbox_inches="tight")
print(f"NeuralProphet: lags={n_lags}, forecasts={n_forecasts}")
return {"forecast": forecast, "model": model, "metrics": metrics}
2. 時系列特徴量エンジニアリング
def create_ts_features(df, date_col, value_col,
lags=None, rolling_windows=None):
"""
時系列特徴量エンジニアリング。
Parameters:
df: pd.DataFrame — 時系列データ
date_col: str — 日付カラム
value_col: str — 値カラム
lags: list[int] | None — ラグ特徴量 (e.g., [1,7,14,28])
rolling_windows: list[int] | None — ローリング窓 (e.g., [7,14,30])
"""
if lags is None:
lags = [1, 3, 7, 14, 28]
if rolling_windows is None:
rolling_windows = [7, 14, 30]
result = df.copy()
result[date_col] = pd.to_datetime(result[date_col])
result = result.sort_values(date_col)
result["dayofweek"] = result[date_col].dt.dayofweek
result["dayofyear"] = result[date_col].dt.dayofyear
result["month"] = result[date_col].dt.month
result["quarter"] = result[date_col].dt.quarter
result["is_weekend"] = (result[date_col].dt.dayofweek >= 5).astype(int)
result["sin_day"] = np.sin(2 * np.pi * result["dayofyear"] / 365.25)
result["cos_day"] = np.cos(2 * np.pi * result["dayofyear"] / 365.25)
result["sin_week"] = np.sin(2 * np.pi * result["dayofweek"] / 7)
result["cos_week"] = np.cos( * np.pi * result[] / )
lag lags:
result[] = result[value_col].shift(lag)
window rolling_windows:
result[] = result[value_col].rolling(window).mean()
result[] = result[value_col].rolling(window).std()
result[] = result[value_col].rolling(window).()
result[] = result[value_col].rolling(window).()
result[] = result[value_col].diff()
result[] = result[value_col].diff()
n_features = (result.columns) - (df.columns)
(
)
result
():
sklearn.metrics mean_absolute_error, mean_squared_error
sorted_df = df.sort_values(date_col).reset_index(drop=)
n = (sorted_df)
fold_size = (n - horizon) // n_splits
results = []
i (n_splits):
train_end = fold_size * (i + )
test_start = train_end + gap
test_end = (test_start + horizon, n)
test_end > n:
train_df = sorted_df.iloc[:train_end]
test_df = sorted_df.iloc[test_start:test_end]
forecast = model_fn(train_df)
y_true = test_df[value_col].values[:(forecast)]
y_pred = forecast[:(y_true)]
mae = mean_absolute_error(y_true, y_pred)
rmse = np.sqrt(mean_squared_error(y_true, y_pred))
mape = np.mean(np.((y_true - y_pred) / (y_true + ))) *
results.append({
: i, : train_end,
: test_end - test_start,
: mae, : rmse, : mape})
results_df = pd.DataFrame(results)
(
)
results_df
パイプライン統合
time-series → time-series-forecasting → model-monitoring
(古典解析) (ML 予測) (監視)
│ │ ↓
spectral-signal ────┘ anomaly-detection
(周波数解析) (異常検知)
パイプライン出力
| ファイル | 説明 | 次スキル |
|---|
prophet_forecast.png | Prophet 予測結果 | → presentation |
ts_features.csv | 時系列特徴量 | → ml-regression |
backtest_results.csv | バックテスト結果 | → model selection |
ToolUniverse 連携
| TU Key | ツール名 | 連携内容 |
|---|
biotools | bio.tools | 時系列予測ツール検索 |