{"id":974795,"date":"2024-12-27T06:08:04","date_gmt":"2024-12-26T22:08:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/974795.html"},"modified":"2024-12-27T06:08:06","modified_gmt":"2024-12-26T22:08:06","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8xgboost%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/974795.html","title":{"rendered":"Python\u5982\u4f55\u8c03\u7528xgboost\u7b97\u6cd5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200334\/62e7f66e-9c18-455b-8779-63f4cd46be9e.webp\" alt=\"Python\u5982\u4f55\u8c03\u7528xgboost\u7b97\u6cd5\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528XGBoost\u7b97\u6cd5\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u5b89\u88c5XGBoost\u5e93\u3001\u5bfc\u5165\u6570\u636e\u3001\u521b\u5efaDMatrix\u6570\u636e\u7ed3\u6784\u3001\u8bbe\u7f6e\u53c2\u6570\u3001\u8bad\u7ec3\u6a21\u578b\u3001\u8fdb\u884c\u9884\u6d4b\u548c\u8bc4\u4f30\u6a21\u578b\u6027\u80fd<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528XGBoost\u7b97\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u4e2a\u4eba\u7ecf\u9a8c\u548c\u89c1\u89e3\uff0c\u5e2e\u52a9\u8bfb\u8005\u6df1\u5165\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e00\u5f3a\u5927\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5de5\u5177\u3002\u5176\u4e2d\uff0c<strong>\u8bbe\u7f6e\u5408\u9002\u7684\u53c2\u6570<\/strong>\u662f\u5f71\u54cd\u6a21\u578b\u6027\u80fd\u7684\u5173\u952e\u56e0\u7d20\u4e4b\u4e00\u3002\u5728\u8fd9\u4e00\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6839\u636e\u6570\u636e\u7279\u6027\u548c\u95ee\u9898\u7684\u5177\u4f53\u9700\u6c42\uff0c\u8c03\u6574\u53c2\u6570\u5982\u5b66\u4e60\u7387\u3001\u6811\u7684\u6700\u5927\u6df1\u5ea6\u548c\u5b50\u6837\u672c\u6bd4\u4f8b\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5XGBoost\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528XGBoost\u7b97\u6cd5\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5728Python\u73af\u5883\u4e2d\u5b89\u88c5XGBoost\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install xgboost<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u5b89\u88c5\u6210\u529f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165XGBoost\u5e93\u8fdb\u884c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5bfc\u5165\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528XGBoost\u8fdb\u884c\u5efa\u6a21\u4e4b\u524d\uff0c\u9700\u8981\u5bfc\u5165\u548c\u51c6\u5907\u6570\u636e\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u53ef\u4ee5\u5b58\u50a8\u5728CSV\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u4f7f\u7528pandas\u5e93\u8bfb\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u6570\u636e\u540e\uff0c\u8fd8\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u6bd4\u5982\u7f3a\u5931\u503c\u5904\u7406\u3001\u7279\u5f81\u7f16\u7801\u7b49\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u8d28\u91cf\u548c\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u521b\u5efaDMatrix\u6570\u636e\u7ed3\u6784<\/p>\n<\/p>\n<p><p>XGBoost\u4f7f\u7528\u4e00\u79cd\u540d\u4e3aDMatrix\u7684\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u6570\u636e\uff0c\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002\u56e0\u6b64\uff0c\u5728\u8bad\u7ec3\u6a21\u578b\u4e4b\u524d\uff0c\u9700\u8981\u5c06\u6570\u636e\u8f6c\u6362\u4e3aDMatrix\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xgboost as xgb<\/p>\n<h2><strong>\u5206\u79bb\u7279\u5f81\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>X = data.iloc[:, :-1]<\/p>\n<p>y = data.iloc[:, -1]<\/p>\n<h2><strong>\u521b\u5efaDMatrix<\/strong><\/h2>\n<p>dtr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n = xgb.DMatrix(X, label=y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>DMatrix\u4e0d\u4ec5\u80fd\u591f\u5b58\u50a8\u7279\u5f81\u548c\u6807\u7b7e\uff0c\u8fd8\u652f\u6301\u5206\u5e03\u5f0f\u8ba1\u7b97\u548c\u7a00\u758f\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u8bbe\u7f6e\u53c2\u6570<\/p>\n<\/p>\n<p><p>XGBoost\u63d0\u4f9b\u4e86\u591a\u79cd\u53c2\u6570\u6765\u63a7\u5236\u6a21\u578b\u7684\u8bad\u7ec3\u8fc7\u7a0b\u3002\u5e38\u7528\u7684\u53c2\u6570\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>eta<\/code>\uff1a\u5b66\u4e60\u7387\uff0c\u63a7\u5236\u6bcf\u68f5\u6811\u7684\u8d21\u732e\u3002<\/li>\n<li><code>max_depth<\/code>\uff1a\u6811\u7684\u6700\u5927\u6df1\u5ea6\uff0c\u63a7\u5236\u6a21\u578b\u7684\u590d\u6742\u5ea6\u3002<\/li>\n<li><code>subsample<\/code>\uff1a\u6bcf\u6b21\u8fed\u4ee3\u65f6\u4f7f\u7528\u7684\u6570\u636e\u6bd4\u4f8b\u3002<\/li>\n<li><code>objective<\/code>\uff1a\u5b9a\u4e49\u4f18\u5316\u7684\u635f\u5931\u51fd\u6570\uff0c\u6bd4\u5982\u56de\u5f52\u95ee\u9898\u7684<code>reg:squarederror<\/code>\u3002<\/li>\n<\/ul>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u53c2\u6570\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">params = {<\/p>\n<p>    &#39;eta&#39;: 0.1,<\/p>\n<p>    &#39;max_depth&#39;: 6,<\/p>\n<p>    &#39;subsample&#39;: 0.8,<\/p>\n<p>    &#39;objective&#39;: &#39;reg:squarederror&#39;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u8bad\u7ec3\u6a21\u578b<\/p>\n<\/p>\n<p><p>\u8bbe\u7f6e\u597d\u53c2\u6570\u540e\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u8bad\u7ec3XGBoost\u6a21\u578b\u3002\u53ef\u4ee5\u4f7f\u7528<code>train<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bad\u7ec3\u6a21\u578b<\/p>\n<p>num_round = 100<\/p>\n<p>bst = xgb.train(params, dtrain, num_round)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>evals<\/code>\u53c2\u6570\u6765\u76d1\u63a7\u6a21\u578b\u5728\u9a8c\u8bc1\u96c6\u4e0a\u7684\u8868\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u8fdb\u884c\u9884\u6d4b<\/p>\n<\/p>\n<p><p>\u8bad\u7ec3\u597d\u6a21\u578b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u578b\u5bf9\u65b0\u6570\u636e\u8fdb\u884c\u9884\u6d4b\u3002\u9996\u5148\u9700\u8981\u5c06\u65b0\u6570\u636e\u8f6c\u6362\u4e3aDMatrix\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528<code>predict<\/code>\u51fd\u6570\u8fdb\u884c\u9884\u6d4b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efaDMatrix<\/p>\n<p>dtest = xgb.DMatrix(X_test)<\/p>\n<h2><strong>\u8fdb\u884c\u9884\u6d4b<\/strong><\/h2>\n<p>predictions = bst.predict(dtest)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u8bc4\u4f30\u6a21\u578b\u6027\u80fd<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6307\u6807\uff0c\u6bd4\u5982\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u3001\u5747\u65b9\u6839\u8bef\u5dee\uff08RMSE\uff09\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import mean_squared_error<\/p>\n<h2><strong>\u8ba1\u7b97MSE<\/strong><\/h2>\n<p>mse = mean_squared_error(y_test, predictions)<\/p>\n<p>rmse = mse  0.5<\/p>\n<p>print(f&#39;RMSE: {rmse}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u8c03\u53c2\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528XGBoost\u7684\u8fc7\u7a0b\u4e2d\uff0c\u8c03\u53c2\u662f\u63d0\u5347\u6a21\u578b\u6027\u80fd\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u53ef\u4ee5\u4f7f\u7528\u7f51\u683c\u641c\u7d22\uff08Grid Search\uff09\u6216\u968f\u673a\u641c\u7d22\uff08Random Search\uff09\u6765\u81ea\u52a8\u5316\u8fd9\u4e00\u8fc7\u7a0b\u3002\u6b64\u5916\uff0c\u4ea4\u53c9\u9a8c\u8bc1\uff08Cross-validation\uff09\u4e5f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u7279\u5f81\u91cd\u8981\u6027\u5206\u6790<\/p>\n<\/p>\n<p><p>XGBoost\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7279\u5f81\u91cd\u8981\u6027\u5206\u6790\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u6bcf\u4e2a\u7279\u5f81\u5bf9\u6a21\u578b\u7684\u8d21\u732e\u3002\u53ef\u4ee5\u901a\u8fc7<code>plot_importance<\/code>\u51fd\u6570\u53ef\u89c6\u5316\u7279\u5f81\u91cd\u8981\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53ef\u89c6\u5316\u7279\u5f81\u91cd\u8981\u6027<\/p>\n<p>xgb.plot_importance(bst)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u5206\u6790\u53ef\u4ee5\u6307\u5bfc\u6211\u4eec\u5728\u7279\u5f81\u9009\u62e9\u548c\u7279\u5f81\u5de5\u7a0b\u9636\u6bb5\u505a\u51fa\u66f4\u660e\u667a\u7684\u51b3\u7b56\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u8c03\u7528XGBoost\u7b97\u6cd5\u7684\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5\u5e93\u3001\u5bfc\u5165\u6570\u636e\u3001\u521b\u5efaDMatrix\u3001\u8bbe\u7f6e\u53c2\u6570\u3001\u8bad\u7ec3\u6a21\u578b\u3001\u8fdb\u884c\u9884\u6d4b\u548c\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u3002\u901a\u8fc7\u5408\u7406\u7684\u53c2\u6570\u8bbe\u7f6e\u548c\u8c03\u53c2\u4f18\u5316\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u6a21\u578b\u7684\u6027\u80fd\u3002\u7279\u5f81\u91cd\u8981\u6027\u5206\u6790\u5219\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u6df1\u5165\u7406\u89e3\u6570\u636e\u7684\u5de5\u5177\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u8bfb\u8005\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u5e94\u7528XGBoost\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5XGBoost\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528XGBoost\u7b97\u6cd5\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5XGBoost\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u8fdb\u884c\u5b89\u88c5\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install xgboost<\/code>\uff0c\u5373\u53ef\u5feb\u901f\u5b8c\u6210\u5b89\u88c5\u3002\u786e\u4fdd\u5728\u5b89\u88c5\u4e4b\u524d\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u548cpip\u3002<\/p>\n<p><strong>XGBoost\u4e0e\u5176\u4ed6\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u76f8\u6bd4\uff0c\u6709\u54ea\u4e9b\u4f18\u52bf\uff1f<\/strong><br \/>XGBoost\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u8868\u73b0\u51fa\u8272\uff0c\u5177\u6709\u9ad8\u6548\u7684\u8ba1\u7b97\u901f\u5ea6\u548c\u8f83\u4f4e\u7684\u5185\u5b58\u6d88\u8017\u3002\u5b83\u91c7\u7528\u4e86\u68af\u5ea6\u63d0\u5347\u6846\u67b6\uff0c\u80fd\u591f\u6709\u6548\u5730\u5904\u7406\u7f3a\u5931\u503c\uff0c\u5e76\u63d0\u4f9b\u4e86\u591a\u79cd\u6b63\u5219\u5316\u65b9\u6cd5\u6765\u9632\u6b62\u8fc7\u62df\u5408\u3002\u6b64\u5916\uff0cXGBoost\u652f\u6301\u5e76\u884c\u8ba1\u7b97\uff0c\u80fd\u591f\u5145\u5206\u5229\u7528\u591a\u6838\u5904\u7406\u5668\uff0c\u63d0\u5347\u6a21\u578b\u8bad\u7ec3\u901f\u5ea6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528XGBoost\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\u548c\u9884\u6d4b\uff1f<\/strong><br \/>\u5728\u4f7f\u7528XGBoost\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\u65f6\uff0c\u60a8\u9700\u8981\u51c6\u5907\u6570\u636e\u96c6\uff0c\u5e76\u5c06\u5176\u62c6\u5206\u4e3a\u7279\u5f81\u548c\u76ee\u6807\u53d8\u91cf\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528<code>xgboost.XGBClassifier<\/code>\u6216<code>xgboost.XGBRegressor<\/code>\u521b\u5efa\u6a21\u578b\u5b9e\u4f8b\u3002\u901a\u8fc7\u8c03\u7528<code>fit<\/code>\u65b9\u6cd5\u6765\u8bad\u7ec3\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528<code>predict<\/code>\u65b9\u6cd5\u8fdb\u884c\u9884\u6d4b\u3002\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u7528\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528XGBoost\u7b97\u6cd5\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u5b89\u88c5XGBoost\u5e93\u3001\u5bfc\u5165\u6570\u636e\u3001\u521b\u5efaDMatrix\u6570\u636e\u7ed3\u6784\u3001 [&hellip;]","protected":false},"author":3,"featured_media":974801,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974795"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=974795"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974795\/revisions"}],"predecessor-version":[{"id":974803,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974795\/revisions\/974803"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/974801"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=974795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=974795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=974795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}