Skip to content

ディープテック経済

市場が追いついたら、もう遅い。

Menu
  • Entertainment
  • お問い合わせ
  • アカウント
  • パスワードのリセット
  • プロファイル
  • ログイン
  • 一つ一つ
  • 登録
Menu

線形回帰を使ったシンプルな需要予測

Posted on 2023年8月31日2023年8月31日 by DeepRecommend

線形回帰

$y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \dots + \beta_p x_p + \epsilon$

ここで:

  • $y$は目的変数
  • $x_1,x_2,…,x_p$は説明変数
  • $\beta_0はy軸の切片$
  • $\beta_1,\beta_2,…,\beta_p$は各説明変数の係数
  • $\epsilonは誤差項$

線形回帰の目的は、観測データに最も適合する線(または高次元の場合は超平面)を見つけることです。

実際のy値とモデルによって予測されるy値との差の二乗和を最小化することによって見つけられます。

$\text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i – \hat{y}_i)^2 $

ここで:

  • $MSE$は平均二乗誤差
  • $y_i$は$i$番目の実際の$y$値
  • $\hat{y}_i$は$i$番目の予測された$y$値
  • $n$はデータポイントの数

$MSE$を最小化することで、
$\beta$の値(係数)を最適化できます。

予測モデルを使用して未来の需要を予測する関数

Parameters:

  • data: 過去の需要データ。リストまたはNumPy配列を想定。
  • days_ahead: 何日後の需要を予測するか。

Returns:

  • forecasted_value: 予測された需要値
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

def demand_forecast(data, days_ahead):
    X = [data[i: i+5] for i in range(len(data)-5)]
    y = data[5:]

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

    model = LinearRegression()
    model.fit(X_train, y_train)

    predictions = model.predict(X_test)
    mse = mean_squared_error(y_test, predictions)
    print(f"Test MSE: {mse}")

    forecasted_input = data[-5:] # 最後の5日間のデータ
    for _ in range(days_ahead):
        next_day = model.predict([forecasted_input[-5:]])
        forecasted_input.append(next_day[0])

    forecasted_value = forecasted_input[-1]

    return forecasted_value

data = [100, 110, 105, 120, 130, 140, 135, 145, 160, 170, 175, 180]
forecast = demand_forecast(data, 1)
print(f"Forecasted demand for next day: {forecast}")

実行結果

Test MSE: 35.25317352782804
Forecasted demand for next day: 192.21572958313527

厳密な実装

・前処理
・モデル選択
・特徴量エンジニアリング

コメントを残す コメントをキャンセル

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です

カテゴリー

  • Business (39)
    • Consulting (8)
    • Finance (6)
    • Sales_Marketing (6)
  • Human Resources (4)
  • Marketing (38)
    • Design (8)
    • Music (15)
    • Video (2)
  • News (32)
  • Operation (3)
  • Q&A (5)
  • Technology (205)
    • AI (101)
    • Brain (49)
    • Quantum (21)
  • Value (159)
  • アーカイブ (4,163)

アーカイブ

  • 2025年12月 (1)
  • 2025年11月 (1)
  • 2025年10月 (2)
  • 2025年9月 (1)
  • 2025年7月 (1)
  • 2025年6月 (3)
  • 2025年5月 (3)
  • 2025年4月 (1)
  • 2025年3月 (2)
  • 2024年12月 (4)
  • 2024年11月 (5)
  • 2024年10月 (2)
  • 2024年8月 (1)
  • 2024年7月 (3)
  • 2024年6月 (35)
  • 2024年5月 (98)
  • 2024年4月 (16)
  • 2024年3月 (9)
  • 2024年2月 (3)
  • 2023年10月 (1)
  • 2023年9月 (13)
  • 2023年8月 (10)
  • 2023年7月 (77)
  • 2023年6月 (23)
  • 2023年5月 (7)
  • 2023年4月 (26)
  • 2023年3月 (22)
  • 2023年2月 (21)
  • 2023年1月 (53)
  • 2022年12月 (17)
  • 2022年11月 (1)
© 2025 ディープテック経済 | Powered by Minimalist Blog WordPress Theme