{"id":400,"date":"2026-03-06T09:17:26","date_gmt":"2026-03-06T01:17:26","guid":{"rendered":"https:\/\/www.liaoxinghui.com\/?p=400"},"modified":"2026-03-06T09:17:26","modified_gmt":"2026-03-06T01:17:26","slug":"xgboost-sales-forecast-airflow-pipeline","status":"publish","type":"post","link":"https:\/\/www.liaoxinghui.com\/?p=400","title":{"rendered":"\u4f7f\u7528XGBoost\u8fdb\u884c\u9500\u552e\u9884\u6d4b\u5e76\u96c6\u6210\u5230Airflow\u81ea\u52a8\u5316\u6d41\u6c34\u7ebf"},"content":{"rendered":"<h1>\u4f7f\u7528XGBoost\u8fdb\u884c\u9500\u552e\u9884\u6d4b\u5e76\u96c6\u6210\u5230Airflow\u81ea\u52a8\u5316\u6d41\u6c34\u7ebf<\/h1>\n<h2>1. \u4e1a\u52a1\u573a\u666f\u4e0e\u76ee\u6807<\/h2>\n<p>\u96f6\u552e\u4f01\u4e1a\u9700\u6bcf\u5468\u9884\u6d4b\u672a\u67657\u5929\u7684\u9500\u552e\u989d\uff0c\u4ee5\u4f18\u5316\u5e93\u5b58\u548c\u8425\u9500\u7b56\u7565\u3002\u624b\u52a8\u8fd0\u884c\u6a21\u578b\u8017\u65f6\u4e14\u6613\u51fa\u9519\uff0c\u76ee\u6807\u662f\u6784\u5efa\u81ea\u52a8\u5316\u6d41\u6c34\u7ebf\uff1a\u6bcf\u5468\u4e00\u81ea\u52a8\u62c9\u53d6\u6700\u65b0\u6570\u636e\uff0c\u8bad\u7ec3XGBoost\u6a21\u578b\uff0c\u9884\u6d4b\u672a\u6765\u9500\u552e\u989d\uff0c\u5e76\u5c06\u7ed3\u679c\u5199\u5165\u6570\u636e\u5e93\u4f9b\u4e1a\u52a1\u7cfb\u7edf\u67e5\u8be2\u3002\u4efb\u52a1\u7c7b\u578b\u4e3a\u56de\u5f52\u9884\u6d4b\uff0c\u9884\u6d4b\u8fde\u7eed\u9500\u552e\u989d\u6570\u503c\u3002<\/p>\n<h2>2. \u73af\u5883\u51c6\u5907<\/h2>\n<p>\u4f7f\u7528uv\u7ba1\u7406Python\u73af\u5883\uff0c\u786e\u4fdd\u4f9d\u8d56\u4e00\u81f4\u3002\u521b\u5efa<code>requirements.txt<\/code>\u6587\u4ef6\u5e76\u5b89\u88c5\uff1a<\/p>\n<pre><code class=\"lang-bash language-bash bash\"># \u521b\u5efa\u865a\u62df\u73af\u5883\nuv venv .venv\nsource .venv\/bin\/activate  # Linux\/Mac\n# .venv\\Scripts\\activate  # Windows\n\n# \u5b89\u88c5\u4f9d\u8d56\nuv pip install xgboost==2.1.0 pandas==2.2.0 scikit-learn==1.5.0 apache-airflow==2.10.0 sqlalchemy==2.0.30<\/code><\/pre>\n<h2>3. \u6570\u636e\u8bf4\u660e<\/h2>\n<p>\u4f7f\u7528\u6a21\u62df\u6570\u636e\u6a21\u62df\u96f6\u552e\u9500\u552e\u8bb0\u5f55\uff0c\u5305\u542b\u65e5\u671f\u3001\u5e97\u94faID\u3001\u4ea7\u54c1\u7c7b\u522b\u3001\u4fc3\u9500\u6807\u5fd7\u548c\u5386\u53f2\u9500\u552e\u989d\u7b49\u7279\u5f81\u3002\u6570\u636e\u751f\u6210\u903b\u8f91\uff1a\u57fa\u4e8e\u65f6\u95f4\u5e8f\u5217\u548c\u968f\u673a\u56e0\u7d20\u751f\u6210\u8fc7\u53bb365\u5929\u7684\u6bcf\u65e5\u9500\u552e\u6570\u636e\uff0c\u672a\u67657\u5929\u4f5c\u4e3a\u9884\u6d4b\u76ee\u6807\u3002<\/p>\n<pre><code class=\"lang-python language-python python\">import pandas as pd\nimport numpy as np\nfrom datetime import datetime, timedelta\n\n# \u751f\u6210\u6a21\u62df\u6570\u636e\nnp.random.seed(42)\ndates = pd.date_range(start=&#039;2023-01-01&#039;, end=&#039;2023-12-31&#039;, freq=&#039;D&#039;)\ndata = []\nfor date in dates:\n    for store_id in range(1, 6):\n        for category in [&#039;A&#039;, &#039;B&#039;, &#039;C&#039;]:\n            base_sales = 100 + 10 * store_id + np.random.normal(0, 20)\n            promotion = np.random.choice([0, 1], p=[0.7, 0.3])\n            if promotion:\n                base_sales *= 1.5\n            sales = max(0, base_sales + np.random.normal(0, 10))\n            data.append({\n                &#039;date&#039;: date,\n                &#039;store_id&#039;: store_id,\n                &#039;category&#039;: category,\n                &#039;promotion&#039;: promotion,\n                &#039;sales&#039;: sales\n            })\ndf = pd.DataFrame(data)\nprint(f&quot;\u6570\u636e\u5f62\u72b6: {df.shape}&quot;)\nprint(df.head())<\/code><\/pre>\n<h2>4. \u8bad\u7ec3\/\u5b9e\u73b0\u6b65\u9aa4<\/h2>\n<p>\u5b8c\u6574\u4ee3\u7801\u5305\u62ec\u6570\u636e\u9884\u5904\u7406\u3001\u7279\u5f81\u5de5\u7a0b\u3001XGBoost\u6a21\u578b\u8bad\u7ec3\u548c\u8bc4\u4f30\u3002<\/p>\n<pre><code class=\"lang-python language-python python\">import xgboost as xgb\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error\nfrom sklearn.preprocessing import LabelEncoder\n\n# \u6570\u636e\u9884\u5904\u7406\ndf[&#039;date&#039;] = pd.to_datetime(df[&#039;date&#039;])\ndf[&#039;day_of_week&#039;] = df[&#039;date&#039;].dt.dayofweek\ndf[&#039;month&#039;] = df[&#039;date&#039;].dt.month\nle = LabelEncoder()\ndf[&#039;category_encoded&#039;] = le.fit_transform(df[&#039;category&#039;])\n\n# \u7279\u5f81\u548c\u76ee\u6807\nfeatures = [&#039;store_id&#039;, &#039;category_encoded&#039;, &#039;promotion&#039;, &#039;day_of_week&#039;, &#039;month&#039;]\nX = df[features]\ny = df[&#039;sales&#039;]\n\n# \u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# \u8bad\u7ec3XGBoost\u6a21\u578b\nmodel = xgb.XGBRegressor(n_estimators=100, learning_rate=0.1, max_depth=6, random_state=42)\nmodel.fit(X_train, y_train)\n\n# \u9884\u6d4b\u548c\u8bc4\u4f30\ny_pred = model.predict(X_test)\nmae = mean_absolute_error(y_test, y_pred)\nrmse = np.sqrt(mean_squared_error(y_test, y_pred))\nprint(f&quot;MAE: {mae:.2f}, RMSE: {rmse:.2f}&quot;)<\/code><\/pre>\n<h2>5. \u8c03\u7528\u65b9\u5f0f<\/h2>\n<p>\u652f\u6301\u79bb\u7ebf\u6279\u91cf\u9884\u6d4b\u548c\u5355\u6761\u793a\u4f8b\u8c03\u7528\u3002<\/p>\n<ul>\n<li>\u79bb\u7ebf\u6279\u91cf\uff1a\u8bfb\u53d6\u65b0\u6570\u636e\u6587\u4ef6\uff0c\u6279\u91cf\u9884\u6d4b\u5e76\u4fdd\u5b58\u7ed3\u679c\u3002\n<pre><code class=\"lang-python language-python python\"># \u5047\u8bbe\u6709\u65b0\u6570\u636e\u6587\u4ef6 new_data.csv\nnew_df = pd.read_csv(&#039;new_data.csv&#039;)\nnew_df[&#039;date&#039;] = pd.to_datetime(new_df[&#039;date&#039;])\nnew_df[&#039;day_of_week&#039;] = new_df[&#039;date&#039;].dt.dayofweek\nnew_df[&#039;month&#039;] = new_df[&#039;date&#039;].dt.month\nnew_df[&#039;category_encoded&#039;] = le.transform(new_df[&#039;category&#039;])\nX_new = new_df[features]\npredictions = model.predict(X_new)\nnew_df[&#039;predicted_sales&#039;] = predictions\nnew_df.to_csv(&#039;predictions.csv&#039;, index=False)\nprint(&quot;\u6279\u91cf\u9884\u6d4b\u5b8c\u6210\uff0c\u7ed3\u679c\u4fdd\u5b58\u5230 predictions.csv&quot;)<\/code><\/pre><\/li>\n<li>\u5355\u6761\u793a\u4f8b\uff1a\u8f93\u5165\u5355\u6761\u6570\u636e\u5b57\u5178\uff0c\u8fd4\u56de\u9884\u6d4b\u503c\u3002\n<pre><code class=\"lang-python language-python python\">single_data = {&#039;store_id&#039;: 3, &#039;category&#039;: &#039;B&#039;, &#039;promotion&#039;: 1, &#039;date&#039;: &#039;2024-01-01&#039;}\nsingle_df = pd.DataFrame([single_data])\nsingle_df[&#039;date&#039;] = pd.to_datetime(single_df[&#039;date&#039;])\nsingle_df[&#039;day_of_week&#039;] = single_df[&#039;date&#039;].dt.dayofweek\nsingle_df[&#039;month&#039;] = single_df[&#039;date&#039;].dt.month\nsingle_df[&#039;category_encoded&#039;] = le.transform(single_df[&#039;category&#039;])\nX_single = single_df[features]\npred = model.predict(X_single)\nprint(f&quot;\u9884\u6d4b\u9500\u552e\u989d: {pred[0]:.2f}&quot;)<\/code><\/pre><\/li>\n<\/ul>\n<h2>6. \u6307\u6807\u5c0f\u767d\u8bf4\u660e<\/h2>\n<p>\u672c\u4efb\u52a1\u4e3a\u56de\u5f52\u9884\u6d4b\uff0c\u4f7f\u7528MAE\uff08\u5e73\u5747\u7edd\u5bf9\u8bef\u5dee\uff09\u548cRMSE\uff08\u5747\u65b9\u6839\u8bef\u5dee\uff09\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u3002<\/p>\n<ul>\n<li>MAE\uff1a\u9884\u6d4b\u503c\u4e0e\u771f\u5b9e\u503c\u7edd\u5bf9\u5dee\u7684\u5e73\u5747\u503c\uff0c\u5355\u4f4d\u4e0e\u9500\u552e\u989d\u76f8\u540c\uff08\u5982\u5143\uff09\uff0c\u76f4\u89c2\u53cd\u6620\u5e73\u5747\u8bef\u5dee\u5927\u5c0f\uff0c\u5bf9\u5f02\u5e38\u503c\u4e0d\u654f\u611f\u3002\u4f8b\u5982MAE=15\u8868\u793a\u5e73\u5747\u9884\u6d4b\u504f\u5dee15\u5143\u3002<\/li>\n<li>RMSE\uff1a\u9884\u6d4b\u8bef\u5dee\u5e73\u65b9\u7684\u5e73\u5747\u503c\u7684\u5e73\u65b9\u6839\uff0c\u540c\u6837\u5355\u4f4d\uff0c\u4f46\u66f4\u60e9\u7f5a\u5927\u8bef\u5dee\uff0c\u9002\u7528\u4e8e\u4e1a\u52a1\u4e2d\u9700\u907f\u514d\u4e25\u91cd\u504f\u5dee\u7684\u573a\u666f\u3002\u4f8b\u5982RMSE=20\u8868\u793a\u8bef\u5dee\u5206\u5e03\u66f4\u5e7f\u3002<\/li>\n<li>\u9002\u7528\u573a\u666f\uff1a\u56de\u5f52\u4efb\u52a1\u5982\u9500\u552e\u989d\u3001\u623f\u4ef7\u9884\u6d4b\uff0cAUC\/F1\u7528\u4e8e\u5206\u7c7b\u4efb\u52a1\u4e0d\u9002\u7528\u3002<\/li>\n<\/ul>\n<h2>7. \u4e0a\u7ebf\u540e\u8bc4\u4f30<\/h2>\n<ul>\n<li>\u79bb\u7ebf\u76d1\u63a7\uff1a\u6bcf\u5468\u8fd0\u884c\u6d41\u6c34\u7ebf\u540e\uff0c\u8bb0\u5f55MAE\u548cRMSE\u5230\u65e5\u5fd7\uff0c\u8bbe\u7f6e\u9608\u503c\uff08\u5982MAE&gt;30\u89e6\u53d1\u544a\u8b66\uff09\u3002<\/li>\n<li>\u7ebf\u4e0a\u6307\u6807\uff1a\u5bf9\u6bd4\u9884\u6d4b\u9500\u552e\u989d\u4e0e\u5b9e\u9645\u9500\u552e\u989d\u7684\u504f\u5dee\u7387\uff0c\u4e1a\u52a1\u5b9a\u4e49\u53ef\u63a5\u53d7\u8303\u56f4\uff08\u5982\u00b110%\uff09\u3002<\/li>\n<li>\u91cd\u8bad\u89e6\u53d1\u6761\u4ef6\uff1a\u5f53\u8fde\u7eed3\u5468MAE\u4e0a\u5347\u8d85\u8fc75%\u6216\u6570\u636e\u5206\u5e03\u663e\u8457\u53d8\u5316\uff08\u5982\u65b0\u589e\u5e97\u94fa\uff09\u65f6\uff0c\u89e6\u53d1\u6a21\u578b\u91cd\u8bad\u3002<\/li>\n<\/ul>\n<h2>8. \u5e38\u89c1\u5751\u4e0e\u6392\u67e5<\/h2>\n<ol>\n<li>\u6570\u636e\u6e90\u53d8\u66f4\u5bfc\u81f4\u6d41\u6c34\u7ebf\u5931\u8d25\uff1a\u5728Airflow DAG\u4e2d\u6dfb\u52a0\u6570\u636e\u6821\u9a8c\u6b65\u9aa4\uff0c\u68c0\u67e5\u5217\u540d\u548c\u6570\u636e\u7c7b\u578b\uff0c\u5931\u8d25\u65f6\u53d1\u9001\u544a\u8b66\u3002<\/li>\n<li>\u6a21\u578b\u8fc7\u62df\u5408\u9884\u6d4b\u4e0d\u51c6\uff1a\u4f7f\u7528\u4ea4\u53c9\u9a8c\u8bc1\u8c03\u6574\u8d85\u53c2\u6570\uff08\u5982<code>max_depth<\/code>\u51cf\u5c11\u6811\u6df1\u5ea6\uff09\uff0c\u6dfb\u52a0\u6b63\u5219\u5316\u9879\uff08<code>reg_alpha<\/code>, <code>reg_lambda<\/code>\uff09\u3002<\/li>\n<li>Airflow\u4efb\u52a1\u4f9d\u8d56\u7ba1\u7406\u590d\u6742\uff1a\u4f7f\u7528<code>ExternalTaskSensor<\/code>\u6216\u660e\u786e\u8bbe\u7f6e<code>depends_on_past<\/code>\uff0c\u7b80\u5316DAG\u7ed3\u6784\uff0c\u907f\u514d\u73af\u5f62\u4f9d\u8d56\u3002<\/li>\n<li>\u7c7b\u522b\u7279\u5f81\u7f16\u7801\u4e0d\u4e00\u81f4\uff1a\u4fdd\u5b58LabelEncoder\u5bf9\u8c61\u6216\u4f7f\u7528One-Hot\u7f16\u7801\uff0c\u786e\u4fdd\u8bad\u7ec3\u548c\u9884\u6d4b\u65f6\u5904\u7406\u65b9\u5f0f\u76f8\u540c\u3002<\/li>\n<\/ol>","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5b9e\u6218\u6f14\u793a\u5982\u4f55\u7528XGBoost\u6784\u5efa\u9500\u552e\u9884\u6d4b\u6a21\u578b\uff0c\u5e76\u901a\u8fc7Airflow DAG\u5b9e\u73b0\u81ea\u52a8\u5316\u8bad\u7ec3\u548c\u9884\u6d4b\uff0c\u8986\u76d6\u6570\u636e\u9884\u5904\u7406\u3001\u6a21\u578b\u8bad\u7ec3\u3001\u8bc4\u4f30\u53ca\u6d41\u6c34\u7ebf\u96c6\u6210\u5168\u6d41\u7a0b\u3002<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85],"tags":[100,16,101],"class_list":["post-400","post","type-post","status-publish","format-standard","hentry","category-85","tag-ai","tag-linux","tag-xgboost"],"views":126,"_links":{"self":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=400"}],"version-history":[{"count":2,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/400\/revisions"}],"predecessor-version":[{"id":402,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/400\/revisions\/402"}],"wp:attachment":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}