XGBoost - 安装
XGBoost 是一个改进的分布式梯度提升库,它速度快、功能多样且可移植。XGBoost 可以通过多种方式安装,具体取决于操作系统和开发环境。以下是安装 XGBoost 的不同方法。
使用 pip(适用于 Python)
安装 XGBoost 最简单、最常见的方法是通过 pip。因此,您所要做的就是在终端中输入以下命令来下载并安装该库及其依赖项。
pip install xgboost
输出
这是在终端或命令提示符上运行上述命令后的过程 −
Collecting xgboost Downloading xgboost-1.7.3-py3-none-manylinux2014_x86_64.whl (199.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 199.9/199.9 MB 8.4 MB/s eta 0:00:00 Requirement already satisfied: numpy in /usr/local/lib/python3.8/site-packages (from xgboost) (1.21.2) Installing collected packages: xgboost Successfully installed xgboost-1.7.3
使用 Conda(适用于 Anaconda 用户)
如果您使用的是 Anaconda 或 Miniconda,则可以借助 Conda 安装 XGBoost。运行以下命令时,它将下载所需的包和依赖项,然后将其安装在您的系统中。
conda install -c conda-forge xgboost
输出
终端输出通常如下所示 −
(base) $ conda install -c conda-forge xgboost Collecting package metadata (current_repodata.json): done Solving environment: done ## Package Plan ## environment location: /path/to/conda/environment added / updated specs: - xgboost The following packages will be downloaded: package | build ------------------------------|----------------- xgboost-1.7.3 | py38h9b699db_0 70.0 MB ------------------------------------------------------------ Total: 70.0 MB Proceed ([y]/n)? y Downloading and Extracting Packages xgboost-1.7.3 | 70.0 MB | ########## | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done
从源代码构建
如果您想要最新版本的 XGBoost 或想要更改您的构建,您可以通过源代码进行此操作。您必须在计算机上安装 git、cmake 和其他构建工具。
首先,您必须克隆 XGBoost 存储库 −
git clone --recursive https://github.com/dmlc/xgboost
以下是终端输出 −
$ git clone --recursive https://github.com/dmlc/xgboost Cloning into 'xgboost'... remote: Enumerating objects: 17723, done. remote: Counting objects: 100% (17723/17723), done. remote: Compressing objects: 100% (4181/4181), done. remote: Total 17723 (delta 12943), reused 16361 (delta 11547), pack-reused 0 Receiving objects: 100% (17723/17723), 28.36 MiB | 4.61 MiB/s, done. Resolving deltas: 100% (12943/12943), done.
运行上述命令后,您必须导航到克隆的目录 −
cd xgboost
现在像下面这样构建项目 −
mkdir build cd build cmake .. make -j4
终端输出如下 −
-- The CXX compiler identification is GNU 9.4.0 -- The C compiler identification is GNU 9.4.0 ... -- Configuring done -- Generating done -- Build files have been written to: /path/to/xgboost/build [ 1%] Building CXX object CMakeFiles/xgboost.dir/src/learner.cc.o [ 2%] Building CXX object CMakeFiles/xgboost.dir/src/common/host_device_vector.cc.o ... [100%] Linking CXX executable xgboost [100%] Built target xgboost
最后安装 python 包 −
cd ../python-package python setup.py install
安装包后,上述命令的输出如下 −
running install running bdist_egg running egg_info creating xgboost.egg-info writing xgboost.egg-info/PKG-INFO writing dependency_links to xgboost.egg-info/dependency_links.txt writing requirements to xgboost.egg-info/requires.txt writing top-level names to xgboost.egg-info/top_level.txt writing manifest file 'xgboost.egg-info/SOURCES.txt' reading manifest file 'xgboost.egg-info/SOURCES.txt' writing manifest file 'xgboost.egg-info/SOURCES.txt' installing library code to build/bdist.linux-x86_64/egg running install_lib creating /usr/local/lib/python3.8/dist-packages/xgboost-1.7.5-py3.8-linux-x86_64.egg ... byte-compiling /usr/local/lib/python3.8/dist-packages/xgboost/__init__.py to __init__.cpython-38.pyc running install_scripts creating /usr/local/bin ... running install_egg_info Writing /usr/local/lib/python3.8/dist-packages/xgboost-1.7.5-py3.8.egg-info
为 R 安装
如果您想使用 R 编程语言,则可以从 CRAN 存储库安装 XGBoost。使用以下命令为 R 编程安装它 −
install.packages('catboost', repos = 'https://cloud.r-project.org/', dependency=TRUE)
因此,该命令将为 R 编程安装 CatBoost。请参考以下输出了解终端上的安装过程 −
Installing package into '/home/user/R/x86_64-pc-linux-gnu-library/4.1' (as 'lib' is unspecified) trying URL 'https://cloud.r-project.org/src/contrib/xgboost_1.7.5.tar.gz' Content type 'application/x-gzip' length 2277833 bytes (2.2 MB) ================================================== downloaded 2.2 MB * installing *source* package 'xgboost' ... ** package 'xgboost' successfully unpacked and MD5 sums checked ** using staged installation ** libs *** Installing xgboost g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG -I../include -I../third_party/dmlc-core/include -I../third_party/rabit/include -I../third_party/xgboost/src -I../third_party/xgboost/src/../include -fpic -O2 -fPIC -c xgboost_R.cc -o xgboost_R.o ... ... ** R ** demo ** inst ** tests ** preparing package for lazy loading ** help *** installing help indices *** copying figures *** copying HTML documentation ** building package indices ** testing if installed package can be loaded * DONE (xgboost)