Created
August 6, 2024 07:38
-
-
Save hrchu/ddb7504d876f1d71ea2b4876631c3672 to your computer and use it in GitHub Desktop.
pycon tw 2024 tx sales trend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
from statsmodels.tsa.holtwinters import ExponentialSmoothing | |
import numpy as np | |
# 1. 數據準備 | |
# 2024年銷售數據(從第8天開始) | |
sales_data_2024 = [34, 66, 68, 70, 73, 75, 79, 83, 88, 92, 94, 98, 102, 109, 112, 113, 117, 118, 120, 121, 122, 123, 128, 136, 140] | |
sales_data_2024.extend([142, 146, 149, 154, 155, 161, 164, 167, 171, 175, 180, 183, 187, 202, 209, 214, 219, 221, 221]) # Aug 8 added | |
# 2023年銷售數據 | |
sales_data_2023 = [3, 8, 8, 11, 12, 14, 14, 14, 22, 25, 27, 35, 36, 36, 36, 38, 40, 42, 43, 43, 44, 44, 46, 46, 49, 49, 49, 49, 51, 56, 61, 61, 62, 64, 65, 65, 67, 70, 74, 75, 75, 75, 79, 83, 86, 90, 92, 94, 96, 106, 121, 122, 125, 131, 135, 137, 151, 177, 207, 210, 225, 233, 235, 237, 248, 253, 257, 257, 262, 264, 327, 329, 330, 347, 351, 354, 365, 367, 367, 370, 372, 372, 372, 375, 391, 405, 405, 406, 406, 406, 407, 416, 419, 422, 426, 427, 428, 432, 440, 443, 446, 453, 458, 463, 464, 470, 479, 487, 491, 498, 502, 504, 505, 515, 521, 525, 547] | |
# 生成對應的天數 | |
days_2024 = list(range(8, len(sales_data_2024) + 8)) # 從第8天開始 | |
days_2023 = list(range(1, len(sales_data_2023) + 1)) | |
# 2. 模型訓練和預測 | |
# 構建指數平滑模型 | |
model = ExponentialSmoothing(sales_data_2024, trend='add', seasonal=None, seasonal_periods=None) | |
fit = model.fit() | |
# 預測未來29天的銷售量 | |
forecast_days = list(range(len(sales_data_2024) + 8, len(sales_data_2024) + 37)) # 調整預測天數 | |
sales_forecast_ewm = fit.forecast(len(forecast_days)) | |
# 3. 繪圖 | |
plt.figure(figsize=(12, 6)) | |
# 繪製2024年實際銷售數據 | |
plt.plot(days_2024, sales_data_2024, marker='o', linestyle='-', color='b', label='2024 Actual Sales') | |
# 繪製2024年預測銷售數據 | |
plt.plot(forecast_days, sales_forecast_ewm, marker='o', linestyle='--', color='r', label='2024 Forecast Sales (EWM)') | |
# 繪製2023年實際銷售數據 | |
plt.plot(days_2023, sales_data_2023, marker='o', linestyle='-', color='g', label='2023 Actual Sales') | |
# 標記2024年目標值 | |
target_value = 446 | |
target_day = 80 | |
plt.axvline(x=target_day, color='grey', linestyle='--') | |
plt.annotate(f'2024 Target: {target_value}', xy=(target_day, target_value), | |
xytext=(target_day + 1, target_value + 50), | |
arrowprops=dict(facecolor='black', shrink=0.05)) | |
# 設置圖表屬性 | |
plt.xlabel('Day') | |
plt.ylabel('Sales Quantity') | |
plt.title('Daily Sales Trend') | |
plt.grid(True) | |
plt.xticks(range(1, max(max(forecast_days), len(days_2023)) + 1, 5)) | |
plt.legend() | |
plt.tight_layout() | |
# 顯示圖表 | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment