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/', dependencies=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)
廣告