{"id":981147,"date":"2024-12-27T07:00:14","date_gmt":"2024-12-26T23:00:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981147.html"},"modified":"2024-12-27T07:00:16","modified_gmt":"2024-12-26T23:00:16","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8xgboost","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/981147.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5229\u7528xgboost"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210205\/4b90c725-3ae5-4f76-b6f7-165bf51da7dd.webp\" alt=\"python\u4e2d\u5982\u4f55\u5229\u7528xgboost\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5229\u7528XGBoost\u8fdb\u884c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5efa\u6a21\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<strong>XGBoost\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u901f\u5ea6\u5feb\u3001\u6027\u80fd\u597d\u3001\u652f\u6301\u5e76\u884c\u8ba1\u7b97\u548c\u5206\u5e03\u5f0f\u8ba1\u7b97\u3001\u63d0\u4f9b\u6b63\u5219\u5316\u4ee5\u51cf\u5c11\u8fc7\u62df\u5408<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u901f\u5ea6\u5feb\u548c\u6027\u80fd\u597d<\/strong>\u662fXGBoost\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u7684\u663e\u8457\u4f18\u52bf\u3002\u901f\u5ea6\u4e0a\u7684\u63d0\u5347\u4e3b\u8981\u662f\u56e0\u4e3aXGBoost\u4f7f\u7528\u4e86\u68af\u5ea6\u63d0\u5347\u51b3\u7b56\u6811\uff08GBDT\uff09\u7b97\u6cd5\u7684\u4f18\u5316\u5b9e\u73b0\uff0c\u901a\u8fc7\u5bf9\u635f\u5931\u51fd\u6570\u7684\u4e8c\u9636\u5bfc\u6570\u8fdb\u884c\u8fd1\u4f3c\uff0c\u63d0\u5347\u4e86\u6a21\u578b\u7684\u62df\u5408\u80fd\u529b\u548c\u8bad\u7ec3\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>XGBoost\u7684\u901f\u5ea6\u548c\u6027\u80fd\u4f18\u5316\u4e0d\u4ec5\u5f97\u76ca\u4e8e\u7b97\u6cd5\u672c\u8eab\u7684\u6539\u8fdb\uff0c\u8fd8\u4f9d\u8d56\u4e8e\u5176\u5bf9\u786c\u4ef6\u8d44\u6e90\u7684\u9ad8\u6548\u5229\u7528\u3002\u5b83\u652f\u6301\u591a\u7ebf\u7a0b\u5e76\u884c\u8ba1\u7b97\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u5229\u7528\u591a\u6838CPU\u52a0\u5feb\u6a21\u578b\u8bad\u7ec3\u3002\u540c\u65f6\uff0cXGBoost\u8fd8\u652f\u6301\u5206\u5e03\u5f0f\u8ba1\u7b97\uff0c\u53ef\u5728\u591a\u53f0\u673a\u5668\u4e0a\u8bad\u7ec3\u5927\u89c4\u6a21\u6570\u636e\u96c6\u3002\u6b64\u5916\uff0cXGBoost\u5b9e\u73b0\u4e86\u7279\u5f81\u5e76\u884c\u3001\u6570\u636e\u5757\u538b\u7f29\u3001\u7f13\u5b58\u4f18\u5316\u7b49\u6280\u672f\uff0c\u8fdb\u4e00\u6b65\u63d0\u5347\u4e86\u8ba1\u7b97\u901f\u5ea6\u3002\u8fd9\u4e9b\u4f18\u5316\u4f7f\u5f97XGBoost\u6210\u4e3a\u5904\u7406\u6d77\u91cf\u6570\u636e\u4efb\u52a1\u7684\u5229\u5668\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5feb\u901f\u8fed\u4ee3\u548c\u5b9e\u65f6\u51b3\u7b56\u7684\u573a\u666f\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528XGBoost\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u76f8\u5173\u7684Python\u5e93\u3002XGBoost\u5e93\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install xgboost<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528XGBoost\u5e93\u6765\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\u548c\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528XGBoost\u8fdb\u884c\u5efa\u6a21\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5bfc\u5165\u4ee5\u4e0b\u51e0\u4e2a\u91cd\u8981\u7684Python\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xgboost as xgb<\/p>\n<p>from sklearn.model_selection import tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/p>\n<p>from sklearn.metrics import accuracy_score<\/p>\n<p>import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>xgboost<\/code>\uff1a\u7528\u4e8e\u52a0\u8f7dXGBoost\u6a21\u578b\u3002<\/li>\n<li><code>train_test_split<\/code>\uff1a\u7528\u4e8e\u5c06\u6570\u636e\u96c6\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002<\/li>\n<li><code>accuracy_score<\/code>\uff1a\u7528\u4e8e\u8bc4\u4f30\u6a21\u578b\u7684\u51c6\u786e\u7387\u3002<\/li>\n<li><code>pandas<\/code>\u548c<code>numpy<\/code>\uff1a\u7528\u4e8e\u5904\u7406\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><h4>2. \u52a0\u8f7d\u6570\u636e\u5e76\u8fdb\u884c\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528XGBoost\u8fdb\u884c\u5efa\u6a21\u4e4b\u524d\uff0c\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u96c6\u3002\u8fd9\u91cc\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u6f14\u793a\u5982\u4f55\u52a0\u8f7d\u6570\u636e\u5e76\u8fdb\u884c\u9884\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7d\u6570\u636e\u96c6<\/p>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u7279\u5f81\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>X = data.drop(&#39;target&#39;, axis=1)<\/p>\n<p>y = data[&#39;target&#39;]<\/p>\n<h2><strong>\u5206\u5272\u6570\u636e\u96c6\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pandas<\/code>\u52a0\u8f7d\u6570\u636e\u96c6\uff0c\u5e76\u5c06\u5176\u5206\u4e3a\u7279\u5f81<code>X<\/code>\u548c\u6807\u7b7e<code>y<\/code>\u3002\u7136\u540e\u4f7f\u7528<code>train_test_split<\/code>\u5c06\u6570\u636e\u96c6\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\uff0c\u5176\u4e2d\u6d4b\u8bd5\u96c6\u5360\u6570\u636e\u96c6\u768420%\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6a21\u578b\u8bad\u7ec3\u4e0e\u9884\u6d4b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528XGBoost\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\u548c\u9884\u6d4b\u7684\u8fc7\u7a0b\u76f8\u5bf9\u7b80\u5355\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u4f7f\u7528\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efaDMatrix<\/h4>\n<\/p>\n<p><p>XGBoost\u4e2d\u7684DMatrix\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u96c6\u3002\u5b83\u53ef\u4ee5\u52a0\u901f\u6a21\u578b\u8bad\u7ec3\u7684\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efaDMatrix<\/p>\n<p>dtrain = xgb.DMatrix(X_train, label=y_train)<\/p>\n<p>dtest = xgb.DMatrix(X_test, label=y_test)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbe\u7f6e\u53c2\u6570<\/h4>\n<\/p>\n<p><p>XGBoost\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u53c2\u6570\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u95ee\u9898\u8fdb\u884c\u8c03\u6574\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u53c2\u6570<\/p>\n<p>params = {<\/p>\n<p>    &#39;booster&#39;: &#39;gbtree&#39;,<\/p>\n<p>    &#39;objective&#39;: &#39;binary:logistic&#39;,  # \u76ee\u6807\u51fd\u6570<\/p>\n<p>    &#39;eval_metric&#39;: &#39;logloss&#39;,        # \u8bc4\u4f30\u6307\u6807<\/p>\n<p>    &#39;max_depth&#39;: 6,                  # \u6811\u7684\u6700\u5927\u6df1\u5ea6<\/p>\n<p>    &#39;eta&#39;: 0.3,                      # \u5b66\u4e60\u7387<\/p>\n<p>    &#39;gamma&#39;: 0,                      # \u6700\u5c0f\u635f\u5931\u51cf\u5c11<\/p>\n<p>    &#39;subsample&#39;: 1,                  # \u968f\u673a\u9009\u62e9\u6837\u672c\u6bd4\u4f8b<\/p>\n<p>    &#39;colsample_bytree&#39;: 1            # \u968f\u673a\u9009\u62e9\u7279\u5f81\u6bd4\u4f8b<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>booster<\/code>\uff1a\u6307\u5b9a\u4f7f\u7528\u54ea\u79cd\u63d0\u5347\u5668\uff0c\u6709<code>gbtree<\/code>\u3001<code>gblinear<\/code>\u548c<code>dart<\/code>\u3002<\/li>\n<li><code>objective<\/code>\uff1a\u5b9a\u4e49\u5b66\u4e60\u4efb\u52a1\u53ca\u76f8\u5e94\u7684\u5b66\u4e60\u76ee\u6807\u3002<\/li>\n<li><code>eval_metric<\/code>\uff1a\u6307\u5b9a\u8bc4\u4f30\u6307\u6807\u3002<\/li>\n<li><code>max_depth<\/code>\uff1a\u63a7\u5236\u6811\u7684\u6700\u5927\u6df1\u5ea6\u3002<\/li>\n<li><code>eta<\/code>\uff1a\u63a7\u5236\u6a21\u578b\u66f4\u65b0\u7684\u6b65\u957f\u3002<\/li>\n<li><code>gamma<\/code>\uff1a\u6307\u5b9a\u9700\u8981\u51cf\u5c11\u7684\u6700\u5c0f\u635f\u5931\u3002<\/li>\n<li><code>subsample<\/code>\u548c<code>colsample_bytree<\/code>\uff1a\u7528\u4e8e\u63a7\u5236\u8fc7\u62df\u5408\u3002<\/li>\n<\/ul>\n<p><h4>3. \u8bad\u7ec3\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>xgb.train<\/code>\u65b9\u6cd5\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\uff0c\u5e76\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u6dfb\u52a0\u9a8c\u8bc1\u96c6\u8fdb\u884c\u8bc4\u4f30\u3002<\/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, evals=[(dtest, &#39;eval&#39;), (dtrain, &#39;train&#39;)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>num_round<\/code>\u8868\u793a\u8bad\u7ec3\u7684\u8f6e\u6570\uff0c<code>evals<\/code>\u7528\u4e8e\u6307\u5b9a\u9a8c\u8bc1\u96c6\uff0c\u4ee5\u4fbf\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u8bc4\u4f30\u3002<\/p>\n<\/p>\n<p><h4>4. \u6a21\u578b\u9884\u6d4b<\/h4>\n<\/p>\n<p><p>\u6a21\u578b\u8bad\u7ec3\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>predict<\/code>\u65b9\u6cd5\u5bf9\u6d4b\u8bd5\u96c6\u8fdb\u884c\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6a21\u578b\u9884\u6d4b<\/p>\n<p>y_pred = bst.predict(dtest)<\/p>\n<p>predictions = [round(value) for value in y_pred]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6a21\u578b\u8bc4\u4f30\u4e0e\u4f18\u5316<\/h3>\n<\/p>\n<p><p>XGBoost\u63d0\u4f9b\u4e86\u4e00\u4e9b\u8bc4\u4f30\u6307\u6807\u6765\u8861\u91cf\u6a21\u578b\u7684\u6027\u80fd\uff0c\u5e76\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u6765\u4f18\u5316\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h4>1. \u6a21\u578b\u8bc4\u4f30<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>accuracy_score<\/code>\u6765\u8bc4\u4f30\u6a21\u578b\u7684\u51c6\u786e\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6a21\u578b\u8bc4\u4f30<\/p>\n<p>accuracy = accuracy_score(y_test, predictions)<\/p>\n<p>print(f&quot;Accuracy: {accuracy * 100.0:.2f}%&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u53c2\u6570\u8c03\u4f18<\/h4>\n<\/p>\n<p><p>XGBoost\u53c2\u6570\u8c03\u4f18\u7684\u76ee\u6807\u662f\u627e\u5230\u4e00\u7ec4\u6700\u4f18\u53c2\u6570\uff0c\u4f7f\u5f97\u6a21\u578b\u5728\u9a8c\u8bc1\u96c6\u4e0a\u7684\u8868\u73b0\u6700\u4f73\u3002\u53c2\u6570\u8c03\u4f18\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u7f51\u683c\u641c\u7d22\u3001\u968f\u673a\u641c\u7d22\u548c\u8d1d\u53f6\u65af\u4f18\u5316\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import GridSearchCV<\/p>\n<p>param_grid = {<\/p>\n<p>    &#39;max_depth&#39;: [3, 5, 7],<\/p>\n<p>    &#39;min_child_weight&#39;: [1, 3, 5],<\/p>\n<p>    &#39;subsample&#39;: [0.6, 0.8, 1.0],<\/p>\n<p>    &#39;colsample_bytree&#39;: [0.6, 0.8, 1.0],<\/p>\n<p>    &#39;eta&#39;: [0.01, 0.1, 0.3]<\/p>\n<p>}<\/p>\n<p>grid_search = GridSearchCV(estimator=xgb.XGBClassifier(use_label_encoder=False), <\/p>\n<p>                           param_grid=param_grid, scoring=&#39;accuracy&#39;, n_jobs=-1, cv=5)<\/p>\n<p>grid_search.fit(X_train, y_train)<\/p>\n<p>best_params = grid_search.best_params_<\/p>\n<p>print(f&quot;Best parameters: {best_params}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7f51\u683c\u641c\u7d22\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u4e00\u7ec4\u6700\u4f18\u7684\u53c2\u6570\u7ec4\u5408\u6765\u63d0\u5347\u6a21\u578b\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7279\u5f81\u91cd\u8981\u6027\u4e0e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u4e86\u89e3\u7279\u5f81\u7684\u91cd\u8981\u6027\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u6a21\u578b\u7684\u51b3\u7b56\u8fc7\u7a0b\uff0c\u5e76\u53ef\u80fd\u4e3a\u7279\u5f81\u5de5\u7a0b\u63d0\u4f9b\u6307\u5bfc\u3002<\/p>\n<\/p>\n<p><h4>1. \u7279\u5f81\u91cd\u8981\u6027<\/h4>\n<\/p>\n<p><p>XGBoost\u63d0\u4f9b\u4e86<code>get_score<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u7279\u5f81\u91cd\u8981\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">importance = bst.get_score(importance_type=&#39;weight&#39;)<\/p>\n<p>importance = sorted(importance.items(), key=lambda x: x[1], reverse=True)<\/p>\n<p>print(&quot;Feature importance:&quot;, importance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u6765\u53ef\u89c6\u5316\u7279\u5f81\u91cd\u8981\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>xgb.plot_importance(bst)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u53ef\u89c6\u5316\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u89c2\u5730\u770b\u5230\u54ea\u4e9b\u7279\u5f81\u5bf9\u6a21\u578b\u7684\u9884\u6d4b\u7ed3\u679c\u5f71\u54cd\u6700\u5927\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001XGBoost\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>XGBoost\u9664\u4e86\u57fa\u672c\u7528\u6cd5\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u6a21\u578b\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5904\u7406\u4e0d\u5e73\u8861\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u5e73\u8861\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574<code>scale_pos_weight<\/code>\u53c2\u6570\u6765\u5e73\u8861\u6b63\u8d1f\u6837\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">params[&#39;scale_pos_weight&#39;] = sum(y_train == 0) \/ sum(y_train == 1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u81ea\u5b9a\u4e49\u635f\u5931\u51fd\u6570<\/h4>\n<\/p>\n<p><p>XGBoost\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u635f\u5931\u51fd\u6570\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_loss(y_true, y_pred):<\/p>\n<p>    grad = y_pred - y_true<\/p>\n<p>    hess = np.ones_like(y_true)<\/p>\n<p>    return grad, hess<\/p>\n<p>bst = xgb.train(params, dtrain, num_round, obj=custom_loss)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528GPU\u52a0\u901f<\/h4>\n<\/p>\n<p><p>XGBoost\u652f\u6301GPU\u52a0\u901f\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u5927\u89c4\u6a21\u6570\u636e\u96c6\u7684\u8bad\u7ec3\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">params[&#39;tree_method&#39;] = &#39;gpu_hist&#39;<\/p>\n<p>bst = xgb.train(params, dtrain, num_round)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>XGBoost\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u96c6\u548c\u4efb\u52a1\u3002\u5728\u4f7f\u7528XGBoost\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u3001\u7279\u5f81\u9009\u62e9\u3001\u5904\u7406\u4e0d\u5e73\u8861\u6570\u636e\u3001\u4f7f\u7528GPU\u52a0\u901f\u7b49\u65b9\u6cd5\u6765\u63d0\u5347\u6a21\u578b\u6027\u80fd\u3002\u901a\u8fc7\u4e0d\u65ad\u7684\u5b9e\u9a8c\u548c\u8c03\u6574\uff0c\u6211\u4eec\u53ef\u4ee5\u5145\u5206\u53d1\u6325XGBoost\u7684\u4f18\u52bf\uff0c\u6784\u5efa\u51fa\u66f4\u4e3a\u7cbe\u786e\u548c\u9ad8\u6548\u7684\u9884\u6d4b\u6a21\u578b\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 \/>\u5728Python\u4e2d\u4f7f\u7528XGBoost\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u6b63\u786e\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>pip install xgboost<\/code>\u6765\u5b89\u88c5\uff0c\u6216\u8005\u5728Jupyter Notebook\u4e2d\u4f7f\u7528<code>!pip install xgboost<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import xgboost as xgb<\/code>\u6765\u5bfc\u5165\u5e93\uff0c\u4ee5\u4fbf\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u3002<\/p>\n<p><strong>XGBoost\u7684\u4e3b\u8981\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>XGBoost\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u68af\u5ea6\u63d0\u5347\u7b97\u6cd5\uff0c\u5177\u6709\u8bb8\u591a\u4f18\u52bf\u3002\u5b83\u80fd\u591f\u5904\u7406\u7f3a\u5931\u503c\uff0c\u5177\u6709\u5185\u7f6e\u7684\u6b63\u5219\u5316\u529f\u80fd\uff0c\u6709\u52a9\u4e8e\u51cf\u5c11\u8fc7\u62df\u5408\u3002\u6b64\u5916\uff0cXGBoost\u652f\u6301\u5e76\u884c\u8ba1\u7b97\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u6a21\u578b\u8bad\u7ec3\u7684\u901f\u5ea6\u3002\u7531\u4e8e\u5176\u5f3a\u5927\u7684\u6027\u80fd\uff0cXGBoost\u5728\u8bb8\u591a\u6570\u636e\u79d1\u5b66\u7ade\u8d5b\u4e2d\u8868\u73b0\u4f18\u5f02\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>XGBoost\u7684\u53c2\u6570\u8bbe\u7f6e\u6709\u54ea\u4e9b\u63a8\u8350\uff1f<\/strong><br \/>\u5728\u4f7f\u7528XGBoost\u65f6\uff0c\u5408\u9002\u7684\u53c2\u6570\u8bbe\u7f6e\u5bf9\u6a21\u578b\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u5e38\u7528\u7684\u53c2\u6570\u5305\u62ec<code>learning_rate<\/code>\uff08\u5b66\u4e60\u7387\uff09\uff0c<code>n_estimators<\/code>\uff08\u6811\u7684\u6570\u91cf\uff09\u548c<code>max_depth<\/code>\uff08\u6811\u7684\u6700\u5927\u6df1\u5ea6\uff09\u3002\u901a\u5e38\uff0c\u53ef\u4ee5\u4ece\u8f83\u5c0f\u7684\u5b66\u4e60\u7387\u5f00\u59cb\uff0c\u7ed3\u5408\u4ea4\u53c9\u9a8c\u8bc1\u6765\u786e\u5b9a\u6700\u4f73\u7684\u6811\u7684\u6570\u91cf\u3002\u5176\u4ed6\u53c2\u6570\u5982<code>subsample<\/code>\uff08\u91c7\u6837\u6bd4\u4f8b\uff09\u548c<code>colsample_bytree<\/code>\uff08\u5217\u91c7\u6837\u6bd4\u4f8b\uff09\u4e5f\u80fd\u6709\u6548\u63d0\u9ad8\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\u3002\u8c03\u4f18\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u4f7f\u7528GridSearchCV\u6216RandomizedSearchCV\u7b49\u5de5\u5177\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bc4\u4f30XGBoost\u6a21\u578b\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u8bc4\u4f30XGBoost\u6a21\u578b\u7684\u6027\u80fd\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6307\u6807\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4efb\u52a1\u7c7b\u578b\u3002\u5bf9\u4e8e\u5206\u7c7b\u4efb\u52a1\uff0c\u53ef\u4ee5\u9009\u62e9\u51c6\u786e\u7387\u3001\u7cbe\u786e\u7387\u3001\u53ec\u56de\u7387\u548cF1\u5206\u6570\u7b49\u6307\u6807\u3002\u5bf9\u4e8e\u56de\u5f52\u4efb\u52a1\uff0c\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u548c\u5747\u65b9\u6839\u8bef\u5dee\uff08RMSE\uff09\u662f\u5e38\u7528\u7684\u8bc4\u4f30\u6807\u51c6\u3002\u53ef\u4ee5\u4f7f\u7528<code>sklearn.metrics<\/code>\u5e93\u4e2d\u7684\u76f8\u5173\u51fd\u6570\u6765\u8ba1\u7b97\u8fd9\u4e9b\u6307\u6807\uff0c\u786e\u4fdd\u6a21\u578b\u5728\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u4e0a\u7684\u8868\u73b0\u90fd\u80fd\u5f97\u5230\u826f\u597d\u8bc4\u4f30\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5229\u7528XGBoost\u8fdb\u884c\u673a\u5668\u5b66\u4e60\u5efa\u6a21\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u7684\u65b9\u6cd5\u3002XGBoost\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u901f\u5ea6\u5feb\u3001\u6027\u80fd [&hellip;]","protected":false},"author":3,"featured_media":981159,"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\/981147"}],"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=981147"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981147\/revisions"}],"predecessor-version":[{"id":981161,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981147\/revisions\/981161"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/981159"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=981147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=981147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=981147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}