{"id":979774,"date":"2024-12-27T06:49:12","date_gmt":"2024-12-26T22:49:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/979774.html"},"modified":"2024-12-27T06:49:14","modified_gmt":"2024-12-26T22:49:14","slug":"python%e5%a6%82%e4%bd%95%e6%89%8b%e5%8a%a8%e5%ae%89%e8%a3%85xgboost","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/979774.html","title":{"rendered":"python\u5982\u4f55\u624b\u52a8\u5b89\u88c5xgboost"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205443\/67b961d2-7a7f-4f7c-a97a-c081ef857741.webp\" alt=\"python\u5982\u4f55\u624b\u52a8\u5b89\u88c5xgboost\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u624b\u52a8\u5b89\u88c5XGBoost\u7684\u8fc7\u7a0b\u53ef\u80fd\u9700\u8981\u4f60\u5177\u5907\u4e00\u5b9a\u7684\u8ba1\u7b97\u673a\u548c\u7f16\u7a0b\u77e5\u8bc6\u3002<strong>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u548cpip\uff0c\u4e0b\u8f7dXGBoost\u7684\u6e90\u7801\uff0c\u5b89\u88c5\u6240\u9700\u7684\u7f16\u8bd1\u5de5\u5177\uff0c\u7136\u540e\u901a\u8fc7\u547d\u4ee4\u884c\u7f16\u8bd1\u548c\u5b89\u88c5<\/strong>\u3002\u8be6\u7ec6\u6b65\u9aa4\u5982\u4e0b\uff1a\u9996\u5148\uff0c\u5b89\u88c5\u7f16\u8bd1\u5de5\u5177\uff1b\u5176\u6b21\uff0c\u4e0b\u8f7dXGBoost\u6e90\u7801\uff1b\u6700\u540e\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u7f16\u8bd1\u5e76\u5b89\u88c5XGBoost\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u51c6\u5907\u7f16\u8bd1\u73af\u5883<\/p>\n<\/p>\n<p><p>\u5728\u5b89\u88c5XGBoost\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86\u7f16\u8bd1\u5de5\u5177\u3002\u5bf9\u4e8eWindows\u7528\u6237\uff0c\u53ef\u4ee5\u5b89\u88c5MinGW\u6216Visual Studio\u7684C++\u6784\u5efa\u5de5\u5177\u3002\u5bf9\u4e8eLinux\u548cMac\u7528\u6237\uff0c\u53ef\u4ee5\u4f7f\u7528gcc\u7f16\u8bd1\u5668\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Windows\u7528\u6237<\/strong>\uff1a\u53ef\u4ee5\u4e0b\u8f7d\u5e76\u5b89\u88c5MinGW\uff0c\u786e\u4fdd\u9009\u62e9\u5b89\u88c5C++\u7f16\u8bd1\u5668\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c06MinGW\u7684bin\u76ee\u5f55\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684PATH\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Linux\u7528\u6237<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5gcc\u3002\u4f8b\u5982\uff0c\u5728Ubuntu\u4e0a\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>sudo apt-get install build-essential<\/code>\u5b89\u88c5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Mac\u7528\u6237<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528Xcode\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u4f7f\u7528\u547d\u4ee4<code>xcode-select --install<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4e0b\u8f7dXGBoost\u6e90\u7801<\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4eceGitHub\u4e0a\u4e0b\u8f7dXGBoost\u7684\u6e90\u7801\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4f60\u83b7\u5f97\u6700\u65b0\u7684\u7248\u672c\u3002\u4ee5\u4e0b\u662f\u4e0b\u8f7d\u6e90\u7801\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\uff08\u5982Windows\u7684\u547d\u4ee4\u63d0\u793a\u7b26\uff0cLinux\u6216Mac\u7684\u7ec8\u7aef\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5bfc\u822a\u5230\u4f60\u5e0c\u671b\u4e0b\u8f7d\u6e90\u7801\u7684\u76ee\u5f55\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u514b\u9686XGBoost\u7684GitHub\u4ed3\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">git clone --recursive https:\/\/github.com\/dmlc\/xgboost<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u8fdb\u5165xgboost\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd xgboost<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u7f16\u8bd1\u548c\u5b89\u88c5XGBoost<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u8bd1XGBoost<\/strong>\uff1a\u5728xgboost\u76ee\u5f55\u4e0b\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u7f16\u8bd1XGBoost\uff1a<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8eWindows\u7528\u6237\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">mkdir build<\/p>\n<p>cd build<\/p>\n<p>cmake .. -G&quot;Visual Studio 15 2017 Win64&quot;<\/p>\n<p>cmake --build . --target ALL_BUILD --config Release<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8eLinux\u548cMac\u7528\u6237\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">mkdir build<\/p>\n<p>cd build<\/p>\n<p>cmake ..<\/p>\n<p>make -j4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u547d\u4ee4\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>libxgboost.so<\/code>\u7684\u52a8\u6001\u94fe\u63a5\u5e93\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5Python\u5305<\/strong>\uff1a\u8fd4\u56de\u5230xgboost\u76ee\u5f55\uff0c\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5XGBoost\u7684Python\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd python-package<\/p>\n<p>python setup.py install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u9a8c\u8bc1\u5b89\u88c5<\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u542f\u52a8Python\u89e3\u91ca\u5668\uff0c\u5e76\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1XGBoost\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xgboost as xgb<\/p>\n<p>print(xgb.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u62a5\u9519\uff0c\u5e76\u4e14\u6253\u5370\u51fa\u4e86\u7248\u672c\u53f7\uff0c\u5219\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u8bd1\u9519\u8bef<\/strong>\uff1a\u786e\u4fdd\u4f60\u7684\u7f16\u8bd1\u5de5\u5177\u548cPython\u7248\u672c\u4e0eXGBoost\u7684\u8981\u6c42\u517c\u5bb9\u3002\u5982\u679c\u9047\u5230\u7f16\u8bd1\u9519\u8bef\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u66f4\u65b0\u7f16\u8bd1\u5de5\u5177\u6216\u67e5\u770bXGBoost GitHub\u9875\u9762\u4e0a\u7684\u5e38\u89c1\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8def\u5f84\u95ee\u9898<\/strong>\uff1a\u5982\u679c\u5b89\u88c5\u540e\u65e0\u6cd5\u5728Python\u4e2d\u5bfc\u5165XGBoost\uff0c\u8bf7\u68c0\u67e5Python\u7684\u8def\u5f84\u8bbe\u7f6e\uff0c\u786e\u4fdd\u5b89\u88c5\u8def\u5f84\u5728Python\u7684\u641c\u7d22\u8def\u5f84\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f9d\u8d56\u95ee\u9898<\/strong>\uff1a\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u548cSciPy\u7b49XGBoost\u6240\u4f9d\u8d56\u7684\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install numpy scipy<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u6210\u529f\u624b\u52a8\u5b89\u88c5XGBoost\u3002\u624b\u52a8\u5b89\u88c5\u867d\u7136\u7a0d\u663e\u590d\u6742\uff0c\u4f46\u53ef\u4ee5\u8ba9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u8f6f\u4ef6\u7684\u6784\u5efa\u548c\u5b89\u88c5\u8fc7\u7a0b\uff0c\u5e76\u4e14\u5728\u4e00\u4e9b\u7279\u6b8a\u73af\u5883\u4e0b\uff0c\u624b\u52a8\u5b89\u88c5\u53ef\u80fd\u662f\u89e3\u51b3\u517c\u5bb9\u6027\u95ee\u9898\u7684\u6709\u6548\u9014\u5f84\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u68c0\u67e5\u6211\u7684Python\u73af\u5883\u662f\u5426\u652f\u6301xgboost\u7684\u5b89\u88c5\uff1f<\/strong><br \/>\u5728\u5b89\u88c5xgboost\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684Python\u7248\u672c\u662f3.5\u53ca\u4ee5\u4e0a\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python --version<\/code>\u6216<code>python3 --version<\/code>\u6765\u68c0\u67e5\u5f53\u524d\u7684Python\u7248\u672c\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4f60\u7684pip\u7248\u672c\u4e5f\u8f83\u4e3a\u66f4\u65b0\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install --upgrade pip<\/code>\u6765\u66f4\u65b0\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u5b89\u88c5\u54ea\u4e9b\u4f9d\u8d56\u9879\u624d\u80fd\u6210\u529f\u5b89\u88c5xgboost\uff1f<\/strong><br \/>\u5728\u624b\u52a8\u5b89\u88c5xgboost\u4e4b\u524d\uff0c\u5efa\u8bae\u5b89\u88c5\u4e00\u4e9b\u4f9d\u8d56\u9879\uff0c\u5982numpy\u548cscipy\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<code>pip install numpy scipy<\/code>\u3002\u8fd9\u4e9b\u5e93\u662fxgboost\u8fd0\u884c\u7684\u57fa\u7840\uff0c\u786e\u4fdd\u5b83\u4eec\u7684\u5b89\u88c5\u53ef\u4ee5\u907f\u514d\u540e\u7eed\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u679c\u624b\u52a8\u5b89\u88c5xgboost\u65f6\u9047\u5230\u9519\u8bef\uff0c\u6211\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u9047\u5230\u5b89\u88c5\u9519\u8bef\u65f6\uff0c\u9996\u5148\u68c0\u67e5\u9519\u8bef\u63d0\u793a\u4fe1\u606f\uff0c\u6709\u65f6\u662f\u56e0\u4e3a\u7f3a\u5c11\u7f16\u8bd1\u5de5\u5177\u6216\u4f9d\u8d56\u5e93\u3002\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86C++\u7f16\u8bd1\u5668\uff08\u5982GCC\u6216Clang\uff09\uff0c\u5e76\u4e14\u5728Linux\u6216macOS\u4e0a\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528<code>sudo apt-get install build-essential<\/code>\u6216<code>brew install gcc<\/code>\u6765\u5b89\u88c5\u3002\u6b64\u5916\uff0c\u67e5\u9605xgboost\u7684\u5b98\u65b9\u6587\u6863\u548cGitHub\u9875\u9762\uff0c\u5e38\u5e38\u80fd\u627e\u5230\u89e3\u51b3\u65b9\u6848\u548c\u5b89\u88c5\u6307\u5357\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u624b\u52a8\u5b89\u88c5XGBoost\u7684\u8fc7\u7a0b\u53ef\u80fd\u9700\u8981\u4f60\u5177\u5907\u4e00\u5b9a\u7684\u8ba1\u7b97\u673a\u548c\u7f16\u7a0b\u77e5\u8bc6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Pyt [&hellip;]","protected":false},"author":3,"featured_media":979778,"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\/979774"}],"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=979774"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979774\/revisions"}],"predecessor-version":[{"id":979781,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979774\/revisions\/979781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/979778"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=979774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=979774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=979774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}