{"id":3901,"date":"2025-08-20T11:13:19","date_gmt":"2025-08-20T03:13:19","guid":{"rendered":"http:\/\/viplao.com\/?p=3901"},"modified":"2025-09-13T23:18:04","modified_gmt":"2025-09-13T15:18:04","slug":"%e3%80%90python%e5%ae%9e%e8%b7%b5%e6%a1%88%e4%be%8b%e3%80%91%e7%94%b5%e5%95%86%e5%b9%b3%e5%8f%b0%e6%95%b0%e6%8d%ae%e5%88%86%e6%9e%90%e5%92%8c%e6%8c%96%e6%8e%98-%e5%b8%82%e5%9c%ba%e8%b6%8b%e5%8a%bf","status":"publish","type":"post","link":"http:\/\/viplao.com\/index.php\/2025\/08\/20\/%e3%80%90python%e5%ae%9e%e8%b7%b5%e6%a1%88%e4%be%8b%e3%80%91%e7%94%b5%e5%95%86%e5%b9%b3%e5%8f%b0%e6%95%b0%e6%8d%ae%e5%88%86%e6%9e%90%e5%92%8c%e6%8c%96%e6%8e%98-%e5%b8%82%e5%9c%ba%e8%b6%8b%e5%8a%bf\/","title":{"rendered":"\u3010Python\u5b9e\u8df5\u6848\u4f8b\u3011\u7535\u5546\u5e73\u53f0\u6570\u636e\u5206\u6790\u548c\u6316\u6398 &#8211; \u5e02\u573a\u8d8b\u52bf\u9884\u6d4b"},"content":{"rendered":"\n<p>\u5f00\u53d1\u601d\u8def &#8211; \u8be5\u811a\u672c\u5c06\u5305\u542b\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n\n\n\n<ol>\n<li>\u6a21\u62df\u751f\u6210\u5305\u542b\u65f6\u95f4\u5e8f\u5217\u4fe1\u606f\u7684\u5546\u54c1\u9500\u552e\u6570\u636e\u3002<\/li>\n\n\n\n<li>\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406\u548c\u63a2\u7d22\u6027\u6570\u636e\u5206\u6790\uff08EDA\uff09\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528ARIMA\uff08\u81ea\u56de\u5f52\u79ef\u5206\u6ed1\u52a8\u5e73\u5747\u6a21\u578b\uff09\u548cProphet\u4e24\u79cd\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\u6a21\u578b\u8fdb\u884c\u9500\u91cf\u9884\u6d4b\u3002<\/li>\n\n\n\n<li>\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u5e76\u6bd4\u8f83\u9884\u6d4b\u6548\u679c\u3002<\/li>\n\n\n\n<li>\u751f\u6210\u4e00\u4efd\u5305\u542b\u5206\u6790\u8fc7\u7a0b\u3001\u9884\u6d4b\u7ed3\u679c\u548c\u5546\u4e1a\u6d1e\u5bdf\u7684\u7efc\u5408\u62a5\u544a\u3002<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\nimport numpy as np\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom datetime import datetime, timedelta\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# \u5c1d\u8bd5\u5bfc\u5165Prophet\uff0c\u5982\u679c\u5931\u8d25\u5219\u6807\u8bb0\ntry:\n    from prophet import Prophet\n    PROPHET_AVAILABLE = True\nexcept ImportError:\n    print(\"\u8b66\u544a: Prophet\u5e93\u672a\u5b89\u88c5\u6216\u5bfc\u5165\u5931\u8d25\u3002\u5c06\u8df3\u8fc7Prophet\u6a21\u578b\u90e8\u5206\u3002\u8bf7\u8fd0\u884c 'pip install prophet' \u5b89\u88c5\u3002\")\n    PROPHET_AVAILABLE = False\n\n# \u5c1d\u8bd5\u5bfc\u5165pmdarima\u7528\u4e8eARIMA\uff0c\u5982\u679c\u5931\u8d25\u5219\u6807\u8bb0\ntry:\n    from pmdarima import auto_arima\n    PMDARIMA_AVAILABLE = True\nexcept ImportError:\n    print(\"\u8b66\u544a: pmdarima\u5e93\u672a\u5b89\u88c5\u3002ARIMA\u6a21\u578b\u5c06\u4f7f\u7528\u56fa\u5b9a\u53c2\u6570\u3002\u8bf7\u8fd0\u884c 'pip install pmdarima' \u5b89\u88c5\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6548\u679c\u3002\")\n    PMDARIMA_AVAILABLE = False\n    from statsmodels.tsa.arima.model import ARIMA # \u57fa\u7840ARIMA\n\n# --- \u914d\u7f6e ---\nNUM_DAYS = 730  # 2\u5e74\u7684\u65e5\u9500\u91cf\u6570\u636e\nREPORT_PREFIX = '\u7535\u5546\u5546\u54c1\u8d8b\u52bf\u9884\u6d4b\u62a5\u544a'\nRANDOM_SEED = 42\n\n# --- \u6570\u636e\u751f\u6210 ---\n\ndef generate_sample_sales_data(n_days):\n    \"\"\"\u751f\u6210\u6a21\u62df\u7684\u5546\u54c1\u65e5\u9500\u91cf\u6570\u636e\uff0c\u5305\u542b\u8d8b\u52bf\u3001\u5b63\u8282\u6027\u548c\u566a\u58f0\"\"\"\n    print(\"--- \u6b63\u5728\u751f\u6210\u6a21\u62df\u5546\u54c1\u9500\u552e\u6570\u636e ---\")\n    np.random.seed(RANDOM_SEED)\n    \n    start_date = datetime.now() - timedelta(days=n_days-1)\n    dates = pd.date_range(start=start_date, periods=n_days, freq='D')\n    \n    # 1. \u957f\u671f\u8d8b\u52bf (\u7ebf\u6027\u589e\u957f)\n    trend_slope = np.random.uniform(0.5, 2.0) # \u6bcf\u5929\u5e73\u5747\u589e\u957f0.5\u52302\u4e2a\u5355\u4f4d\n    trend = np.arange(n_days) * trend_slope\n    \n    # 2. \u5b63\u8282\u6027 (\u5e74\u5ea6\u548c\u5468\u5ea6)\n    # \u5e74\u5ea6\u5b63\u8282\u6027 (\u7b80\u5316\u4e3a\u6b63\u5f26\u6ce2)\n    annual_seasonality = 50 * np.sin(2 * np.pi * np.arange(n_days) \/ 365.25)\n    \n    # \u5468\u5ea6\u5b63\u8282\u6027 (\u5468\u672b\u9500\u91cf\u9ad8)\n    day_of_week = pd.Series(dates).dt.dayofweek\n    weekly_seasonality = np.where(day_of_week.isin(&#91;5, 6]), 30, 0) # \u5468\u516d\u3001\u65e5\u589e\u52a030\n    \n    # 3. \u8282\u5047\u65e5\u6548\u5e94 (\u6a21\u62df\u51e0\u4e2a\u5927\u4fc3\u8282\u65e5)\n    holidays_effect = np.zeros(n_days)\n    # \u6a21\u62df618 (6\u670818\u65e5)\n    june_18_indices = &#91;i for i, date in enumerate(dates) if date.month == 6 and date.day == 18]\n    for idx in june_18_indices:\n        holidays_effect&#91;idx] = np.random.uniform(200, 400)\n        if idx > 0: holidays_effect&#91;idx-1] += np.random.uniform(100, 200) # \u524d\u4e00\u5929\u9884\u70ed\n        if idx &lt; n_days-1: holidays_effect&#91;idx+1] += np.random.uniform(50, 150) # \u540e\u4e00\u5929\u8fd4\u573a\n    \n    # \u6a21\u62df\u53cc11 (11\u670811\u65e5)\n    nov_11_indices = &#91;i for i, date in enumerate(dates) if date.month == 11 and date.day == 11]\n    for idx in nov_11_indices:\n        holidays_effect&#91;idx] = np.random.uniform(500, 800)\n        if idx > 0: holidays_effect&#91;idx-1] += np.random.uniform(200, 400)\n        if idx &lt; n_days-1: holidays_effect&#91;idx+1] += np.random.uniform(100, 300)\n\n    # 4. \u968f\u673a\u566a\u58f0\n    noise = np.random.normal(0, 10, n_days) # \u5747\u503c0\uff0c\u6807\u51c6\u5dee10\u7684\u566a\u58f0\n    \n    # 5. \u7ec4\u5408\u6240\u6709\u6210\u5206\n    base_level = 200 # \u57fa\u7840\u9500\u91cf\u6c34\u5e73\n    sales = base_level + trend + annual_seasonality + weekly_seasonality + holidays_effect + noise\n    sales = np.maximum(sales, 0) # \u9500\u91cf\u4e0d\u80fd\u4e3a\u8d1f\n    \n    df = pd.DataFrame({\n        'date': dates,\n        'sales': np.round(sales, 0)\n    })\n    \n    csv_filename = f'{REPORT_PREFIX}_\u6a21\u62df\u9500\u552e\u6570\u636e.csv'\n    df.to_csv(csv_filename, index=False, encoding='utf-8-sig')\n    print(f\"\u6a21\u62df\u9500\u552e\u6570\u636e\u5df2\u751f\u6210\u5e76\u4fdd\u5b58\u81f3: {csv_filename}\")\n    return df\n\n# --- \u6570\u636e\u9884\u5904\u7406\u4e0eEDA ---\n\ndef preprocess_and_eda(df):\n    \"\"\"\u6570\u636e\u9884\u5904\u7406\u548c\u63a2\u7d22\u6027\u6570\u636e\u5206\u6790\"\"\"\n    print(\"\\n--- \u6b63\u5728\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406\u4e0eEDA ---\")\n    df_processed = df.copy()\n    df_processed.set_index('date', inplace=True)\n    df_processed.sort_index(inplace=True)\n    \n    # \u68c0\u67e5\u7f3a\u5931\u503c\n    if df_processed.isnull().sum().sum() > 0:\n        print(\"\u8b66\u544a: \u53d1\u73b0\u7f3a\u5931\u503c\uff0c\u6b63\u5728\u8fdb\u884c\u586b\u5145...\")\n        df_processed&#91;'sales'].fillna(method='ffill', inplace=True) # \u524d\u5411\u586b\u5145\n    \n    # \u7ed8\u5236\u65f6\u95f4\u5e8f\u5217\u56fe\n    plt.figure(figsize=(15, 6))\n    plt.plot(df_processed.index, df_processed&#91;'sales'], linewidth=1)\n    plt.title('\u5546\u54c1\u65e5\u9500\u91cf\u65f6\u95f4\u5e8f\u5217')\n    plt.xlabel('\u65e5\u671f')\n    plt.ylabel('\u9500\u91cf')\n    plt.grid(True)\n    ts_plot_path = f'{REPORT_PREFIX}_\u65f6\u95f4\u5e8f\u5217\u56fe.png'\n    plt.savefig(ts_plot_path)\n    plt.close()\n    print(f\"\u65f6\u95f4\u5e8f\u5217\u56fe\u5df2\u4fdd\u5b58\u81f3: {ts_plot_path}\")\n    \n    return df_processed\n\n# --- ARIMA \u6a21\u578b\u9884\u6d4b ---\n\ndef forecast_with_arima(df, forecast_periods=30):\n    \"\"\"\u4f7f\u7528ARIMA\u6a21\u578b\u8fdb\u884c\u9884\u6d4b\"\"\"\n    print(\"\\n--- \u6b63\u5728\u4f7f\u7528ARIMA\u6a21\u578b\u8fdb\u884c\u9884\u6d4b ---\")\n    \n    train_size = int(len(df) * 0.8)\n    train, test = df.iloc&#91;:train_size], df.iloc&#91;train_size:]\n    \n    # \u4f7f\u7528auto_arima\u81ea\u52a8\u9009\u62e9\u6700\u4f73\u53c2\u6570\uff0c\u6216\u4f7f\u7528\u56fa\u5b9a\u53c2\u6570\n    if PMDARIMA_AVAILABLE:\n        print(\"\u4f7f\u7528auto_arima\u81ea\u52a8\u9009\u62e9ARIMA\u53c2\u6570...\")\n        model_auto = auto_arima(train&#91;'sales'], seasonal=False, trace=False, error_action='ignore', suppress_warnings=True)\n        print(f\"auto_arima\u9009\u62e9\u7684\u6700\u4f73\u6a21\u578b: ARIMA{model_auto.order}\")\n        model = model_auto\n    else:\n        print(\"pmdarima\u4e0d\u53ef\u7528\uff0c\u4f7f\u7528\u56fa\u5b9a\u53c2\u6570 ARIMA(2,1,2)...\")\n        model = ARIMA(train&#91;'sales'], order=(2,1,2))\n        model = model.fit()\n    \n    # \u62df\u5408\u6a21\u578b\n    model_fit = model.fit() if not PMDARIMA_AVAILABLE else model\n    \n    # \u9884\u6d4b\u6d4b\u8bd5\u96c6\n    test_forecast = model_fit.forecast(steps=len(test))\n    \n    # \u9884\u6d4b\u672a\u6765\n    future_forecast = model_fit.forecast(steps=forecast_periods)\n    future_dates = pd.date_range(start=df.index&#91;-1] + timedelta(days=1), periods=forecast_periods, freq='D')\n    \n    # \u8bc4\u4f30 (\u4ec5\u5728\u6709\u6d4b\u8bd5\u96c6\u65f6)\n    if len(test) > 0:\n        mae = mean_absolute_error(test&#91;'sales'], test_forecast)\n        rmse = np.sqrt(mean_squared_error(test&#91;'sales'], test_forecast))\n        print(f\"ARIMA\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u7684\u8868\u73b0:\")\n        print(f\"  - MAE: {mae:.2f}\")\n        print(f\"  - RMSE: {rmse:.2f}\")\n    else:\n        mae, rmse = None, None\n    \n    # \u7ed8\u5236\u9884\u6d4b\u7ed3\u679c\n    plt.figure(figsize=(15, 6))\n    plt.plot(df.index, df&#91;'sales'], label='\u5386\u53f2\u9500\u91cf', linewidth=1)\n    if len(test) > 0:\n        plt.plot(test.index, test_forecast, label='ARIMA\u6d4b\u8bd5\u96c6\u9884\u6d4b', linestyle='--', alpha=0.8)\n    plt.plot(future_dates, future_forecast, label=f'ARIMA\u672a\u6765{forecast_periods}\u5929\u9884\u6d4b', linestyle='--', marker='o', markersize=3)\n    plt.axvline(x=df.index&#91;train_size-1], color='black', linestyle=':', alpha=0.7, label='\u8bad\u7ec3\/\u6d4b\u8bd5\u5206\u5272\u70b9')\n    plt.title('ARIMA\u6a21\u578b\u9500\u91cf\u9884\u6d4b')\n    plt.xlabel('\u65e5\u671f')\n    plt.ylabel('\u9500\u91cf')\n    plt.legend()\n    plt.grid(True)\n    arima_plot_path = f'{REPORT_PREFIX}_ARIMA\u9884\u6d4b\u56fe.png'\n    plt.savefig(arima_plot_path)\n    plt.close()\n    print(f\"ARIMA\u9884\u6d4b\u56fe\u5df2\u4fdd\u5b58\u81f3: {arima_plot_path}\")\n    \n    results = {\n        'model': 'ARIMA',\n        'predictions': future_forecast,\n        'future_dates': future_dates,\n        'metrics': {'MAE': mae, 'RMSE': rmse} if mae is not None else {},\n        'plot_path': arima_plot_path\n    }\n    return results\n\n# --- Prophet \u6a21\u578b\u9884\u6d4b ---\n\ndef forecast_with_prophet(df, forecast_periods=30):\n    \"\"\"\u4f7f\u7528Prophet\u6a21\u578b\u8fdb\u884c\u9884\u6d4b\"\"\"\n    if not PROPHET_AVAILABLE:\n        print(\"Prophet\u6a21\u578b\u8df3\u8fc7\uff0c\u56e0\u4e3a\u5e93\u4e0d\u53ef\u7528\u3002\")\n        return None\n        \n    print(\"\\n--- \u6b63\u5728\u4f7f\u7528Prophet\u6a21\u578b\u8fdb\u884c\u9884\u6d4b ---\")\n    \n    # Prophet\u9700\u8981\u7279\u5b9a\u7684\u5217\u540d\n    df_prophet = df.reset_index()&#91;&#91;'date', 'sales']].rename(columns={'date': 'ds', 'sales': 'y'})\n    \n    # \u521b\u5efa\u5e76\u62df\u5408\u6a21\u578b\n    model = Prophet()\n    # \u53ef\u4ee5\u5728\u8fd9\u91cc\u6dfb\u52a0\u8282\u5047\u65e5\n    # model.add_country_holidays(country_name='CN') # \u6dfb\u52a0\u4e2d\u56fd\u8282\u5047\u65e5\n    \n    model.fit(df_prophet)\n    \n    # \u521b\u5efa\u672a\u6765\u65e5\u671f\u6846\u67b6\n    future = model.make_future_dataframe(periods=forecast_periods)\n    \n    # \u8fdb\u884c\u9884\u6d4b\n    forecast = model.predict(future)\n    \n    # \u7ed8\u5236\u9884\u6d4b\u7ed3\u679c\n    fig1 = model.plot(forecast)\n    plt.title('Prophet\u6a21\u578b\u9500\u91cf\u9884\u6d4b')\n    prophet_plot_path = f'{REPORT_PREFIX}_Prophet\u9884\u6d4b\u56fe.png'\n    plt.savefig(prophet_plot_path)\n    plt.close(fig1)\n    \n    # \u7ed8\u5236\u6a21\u578b\u6210\u5206\n    fig2 = model.plot_components(forecast)\n    prophet_comp_plot_path = f'{REPORT_PREFIX}_Prophet\u6210\u5206\u56fe.png'\n    plt.savefig(prophet_comp_plot_path)\n    plt.close(fig2)\n    print(f\"Prophet\u9884\u6d4b\u56fe\u5df2\u4fdd\u5b58\u81f3: {prophet_plot_path}\")\n    print(f\"Prophet\u6210\u5206\u56fe\u5df2\u4fdd\u5b58\u81f3: {prophet_comp_plot_path}\")\n    \n    # \u63d0\u53d6\u672a\u6765\u9884\u6d4b\u503c\n    future_forecast = forecast&#91;&#91;'ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(forecast_periods)\n    \n    results = {\n        'model': 'Prophet',\n        'forecast_df': future_forecast,\n        'metrics': {}, # Prophet\u5185\u90e8\u6709\u4ea4\u53c9\u9a8c\u8bc1\uff0c\u8fd9\u91cc\u7b80\u5316\u5904\u7406\n        'plot_path': prophet_plot_path,\n        'comp_plot_path': prophet_comp_plot_path\n    }\n    return results\n\n# --- \u62a5\u544a\u751f\u6210 ---\n\ndef generate_trend_forecast_report(arima_res, prophet_res):\n    \"\"\"\u751f\u6210\u6700\u7ec8\u7684\u8d8b\u52bf\u9884\u6d4b\u5206\u6790\u62a5\u544a\"\"\"\n    print(\"\\n--- \u6b63\u5728\u751f\u6210\u5546\u54c1\u5e02\u573a\u8d8b\u52bf\u9884\u6d4b\u62a5\u544a ---\")\n    from datetime import datetime\n    report_filename = f\"{REPORT_PREFIX}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt\"\n    \n    with open(report_filename, 'w', encoding='utf-8') as f:\n        f.write(\"=\" * 50 + \"\\n\")\n        f.write(\"        \u7535\u5546\u5e73\u53f0\u5546\u54c1\u5e02\u573a\u8d8b\u52bf\u9884\u6d4b\u62a5\u544a\\n\")\n        f.write(f\"        \u751f\u6210\u65f6\u95f4: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\\n\")\n        f.write(\"=\" * 50 + \"\\n\\n\")\n\n        f.write(\"--- 1. \u9879\u76ee\u6982\u8ff0 ---\\n\")\n        f.write(\"\u672c\u9879\u76ee\u65e8\u5728\u901a\u8fc7\u5bf9\u7535\u5546\u5e73\u53f0\u7279\u5b9a\u5546\u54c1\u7684\u5386\u53f2\u9500\u552e\u6570\u636e\u8fdb\u884c\u5206\u6790\uff0c\u9884\u6d4b\u5176\u672a\u6765\u7684\u5e02\u573a\u8d8b\u52bf\u3002\\n\")\n        f.write(\"\u8fd9\u6709\u52a9\u4e8e\u5e93\u5b58\u7ba1\u7406\u3001\u8425\u9500\u8d44\u6e90\u5206\u914d\u548c\u5236\u5b9a\u9500\u552e\u7b56\u7565\u3002\\n\\n\")\n\n        f.write(\"--- 2. \u6570\u636e\u6982\u89c8 ---\\n\")\n        f.write(\"\u6570\u636e\u6765\u6e90: \u6a21\u62df\u751f\u6210\u7684\u7535\u5546\u5e73\u53f0\u5546\u54c1\u65e5\u9500\u91cf\u6570\u636e\u3002\\n\")\n        f.write(\"\u6570\u636e\u89c4\u6a21: 2\u5e74 (730\u5929) \u7684\u65e5\u9500\u91cf\u8bb0\u5f55\u3002\\n\")\n        f.write(\"\u6570\u636e\u7279\u5f81: \u5305\u542b\u957f\u671f\u589e\u957f\u8d8b\u52bf\u3001\u5e74\u5ea6\u5b63\u8282\u6027\u3001\u5468\u5ea6\u6ce2\u52a8\u548c\u8282\u5047\u65e5\u6548\u5e94\u3002\\n\")\n        f.write(\"\u539f\u59cb\u6570\u636e\u5df2\u4fdd\u5b58\u4e3a CSV \u6587\u4ef6\u3002\\n\\n\")\n\n        f.write(\"--- 3. \u63a2\u7d22\u6027\u6570\u636e\u5206\u6790 (EDA) ---\\n\")\n        f.write(\"\u5bf9\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u8fdb\u884c\u4e86\u53ef\u89c6\u5316\u5206\u6790\uff0c\u4ee5\u8bc6\u522b\u5176\u6a21\u5f0f\u548c\u7279\u5f81\u3002\\n\")\n        f.write(\"- \u8d8b\u52bf: \u6570\u636e\u663e\u793a\u603b\u4f53\u5448\u4e0a\u5347\u8d8b\u52bf\u3002\\n\")\n        f.write(\"- \u5b63\u8282\u6027: \u5b58\u5728\u660e\u663e\u7684\u5e74\u5ea6\u548c\u5468\u5ea6\u5b63\u8282\u6027\u6a21\u5f0f\u3002\\n\")\n        f.write(\"- \u5468\u671f\u6027\u4e8b\u4ef6: \u53ef\u4ee5\u89c2\u5bdf\u5230\u7c7b\u4f3c'618'\u3001'\u53cc11'\u7b49\u5927\u4fc3\u8282\u65e5\u5e26\u6765\u7684\u9500\u91cf\u9ad8\u5cf0\u3002\\n\")\n        f.write(\"\u65f6\u95f4\u5e8f\u5217\u56fe\u5df2\u751f\u6210\u3002\\n\\n\")\n\n        f.write(\"--- 4. \u9884\u6d4b\u6a21\u578b ---\\n\")\n        f.write(\"\u91c7\u7528\u4e86\u4e24\u79cd\u4e3b\u6d41\u7684\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\u6a21\u578b\u8fdb\u884c\u5bf9\u6bd4\u5206\u6790\uff1a\\n\")\n        f.write(\"1. ARIMA (\u81ea\u56de\u5f52\u79ef\u5206\u6ed1\u52a8\u5e73\u5747\u6a21\u578b): \u4e00\u79cd\u7ecf\u5178\u7684\u7edf\u8ba1\u6a21\u578b\uff0c\u9002\u7528\u4e8e\u5355\u53d8\u91cf\u65f6\u95f4\u5e8f\u5217\u3002\\n\")\n        f.write(\"2. Prophet (\u7531Facebook\u5f00\u53d1): \u4e00\u79cd\u7075\u6d3b\u7684\u6a21\u578b\uff0c\u80fd\u8f83\u597d\u5730\u5904\u7406\u7f3a\u5931\u6570\u636e\u3001\u8d8b\u52bf\u53d8\u5316\u548c\u8282\u5047\u65e5\u6548\u5e94\u3002\\n\\n\")\n        \n        f.write(\"--- 5. ARIMA\u6a21\u578b\u9884\u6d4b\u7ed3\u679c ---\\n\")\n        if arima_res:\n            f.write(f\"\u6a21\u578b: {arima_res&#91;'model']}\\n\")\n            if arima_res&#91;'metrics']:\n                f.write(\"\u6a21\u578b\u8bc4\u4f30 (\u5728\u6d4b\u8bd5\u96c6\u4e0a):\\n\")\n                for metric, value in arima_res&#91;'metrics'].items():\n                    f.write(f\"  - {metric}: {value:.2f}\\n\")\n            f.write(\"\u672a\u676530\u5929\u9884\u6d4b\u503c\u9884\u89c8:\\n\")\n            forecast_df_arima = pd.DataFrame({'date': arima_res&#91;'future_dates'], 'predicted_sales': arima_res&#91;'predictions']})\n            f.write(forecast_df_arima.head(10).round(2).to_string(index=False))\n            f.write(f\"\\n\u9884\u6d4b\u56fe\u8868: {arima_res&#91;'plot_path']}\\n\\n\")\n        else:\n            f.write(\"ARIMA\u6a21\u578b\u6267\u884c\u5931\u8d25\u6216\u88ab\u8df3\u8fc7\u3002\\n\\n\")\n\n        f.write(\"--- 6. Prophet\u6a21\u578b\u9884\u6d4b\u7ed3\u679c ---\\n\")\n        if prophet_res:\n            f.write(f\"\u6a21\u578b: {prophet_res&#91;'model']}\\n\")\n            f.write(\"\u672a\u676530\u5929\u9884\u6d4b\u503c\u9884\u89c8 (\u5305\u542b\u7f6e\u4fe1\u533a\u95f4):\\n\")\n            forecast_summary = prophet_res&#91;'forecast_df']&#91;&#91;'ds', 'yhat', 'yhat_lower', 'yhat_upper']].rename(columns={'ds': 'date', 'yhat': 'predicted_sales', 'yhat_lower': 'lower_bound', 'yhat_upper': 'upper_bound'})\n            f.write(forecast_summary.head(10).round(2).to_string(index=False))\n            f.write(f\"\\n\u9884\u6d4b\u56fe\u8868: {prophet_res&#91;'plot_path']}\\n\")\n            f.write(f\"\u8d8b\u52bf\u4e0e\u5b63\u8282\u6027\u6210\u5206\u56fe: {prophet_res&#91;'comp_plot_path']}\\n\\n\")\n        else:\n            f.write(\"Prophet\u6a21\u578b\u56e0\u5e93\u4e0d\u53ef\u7528\u800c\u88ab\u8df3\u8fc7\u3002\\n\\n\")\n\n        f.write(\"--- 7. \u5546\u4e1a\u6d1e\u5bdf\u4e0e\u5efa\u8bae ---\\n\")\n        f.write(\"1. \u5e93\u5b58\u7ba1\u7406: \u6839\u636e\u9884\u6d4b\u7684\u9500\u91cf\u9ad8\u5cf0\uff08\u5982\u8282\u5047\u65e5\uff09\u63d0\u524d\u5907\u8d27\uff0c\u907f\u514d\u7f3a\u8d27\u3002\\n\")\n        f.write(\"2. \u8425\u9500\u89c4\u5212: \u5728\u9884\u6d4b\u9500\u91cf\u8f83\u4f4e\u7684\u65f6\u671f\u7b56\u5212\u4fc3\u9500\u6d3b\u52a8\uff0c\u4ee5\u63d0\u5347\u9500\u91cf\u3002\\n\")\n        f.write(\"3. \u8d44\u6e90\u5206\u914d: \u5c06\u66f4\u591a\u8425\u9500\u9884\u7b97\u6295\u5165\u5230\u9884\u6d4b\u9500\u91cf\u589e\u957f\u7684\u65f6\u671f\u3002\\n\")\n        f.write(\"4. \u6a21\u578b\u8fed\u4ee3: \u5b9a\u671f\u7528\u6700\u65b0\u7684\u9500\u552e\u6570\u636e\u91cd\u65b0\u8bad\u7ec3\u6a21\u578b\uff0c\u4ee5\u4fdd\u6301\u9884\u6d4b\u7684\u51c6\u786e\u6027\u3002\\n\")\n        f.write(\"5. \u98ce\u9669\u9884\u8b66: \u76d1\u63a7\u5b9e\u9645\u9500\u91cf\u4e0e\u9884\u6d4b\u503c\u7684\u504f\u5dee\uff0c\u53ca\u65f6\u53d1\u73b0\u5e02\u573a\u5f02\u5e38\u53d8\u5316\u3002\\n\\n\")\n\n        f.write(\"=\" * 50 + \"\\n\")\n        f.write(\"                    \u62a5\u544a\u7ed3\u675f\\n\")\n        f.write(\"=\" * 50 + \"\\n\")\n\n    print(f\"\u5546\u54c1\u5e02\u573a\u8d8b\u52bf\u9884\u6d4b\u62a5\u544a\u5df2\u751f\u6210: {report_filename}\")\n\n# --- \u4e3b\u51fd\u6570 ---\n\ndef main():\n    \"\"\"\u4e3b\u51fd\u6570\"\"\"\n    # 1. \u751f\u6210\u6570\u636e\n    df_sales = generate_sample_sales_data(NUM_DAYS)\n    \n    # 2. \u6570\u636e\u9884\u5904\u7406\u4e0eEDA\n    df_processed = preprocess_and_eda(df_sales)\n    \n    # 3. ARIMA\u9884\u6d4b\n    arima_results = forecast_with_arima(df_processed)\n    \n    # 4. Prophet\u9884\u6d4b\n    prophet_results = forecast_with_prophet(df_processed) if PROPHET_AVAILABLE else None\n    \n    # 5. \u751f\u6210\u62a5\u544a\n    generate_trend_forecast_report(arima_results, prophet_results)\n    \n    print(\"\\n\u5546\u54c1\u5e02\u573a\u8d8b\u52bf\u9884\u6d4b\u5206\u6790\u6d41\u7a0b\u5b8c\u6210\u3002\")\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f00\u53d1\u601d\u8def &#8211; \u8be5\u811a\u672c\u5c06\u5305\u542b\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[28],"views":347,"_links":{"self":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts\/3901"}],"collection":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/comments?post=3901"}],"version-history":[{"count":1,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts\/3901\/revisions"}],"predecessor-version":[{"id":3902,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts\/3901\/revisions\/3902"}],"wp:attachment":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/media?parent=3901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/categories?post=3901"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/tags?post=3901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}