CatBoost - 安装



CatBoost 是一个非常快速、可扩展的开源梯度提升决策树库,来自 Yandex。CatBoost 的安装方式取决于您的操作系统和开发环境。本章将介绍在您的系统中安装 CatBoost 的不同方法。以下是一些常见的方法:

使用 pip (适用于 Python)

这是在您的系统中安装 CatBoost 最常见和最简单的方法。您可以在终端上运行以下命令,它将下载库并将其与依赖项一起安装到系统中。

打开您的命令行或终端并运行以下命令:

pip install catboost

在终端或命令提示符中运行上述命令后的过程:

Collecting catboost
  Downloading catboost-1.2.7-cp312-cp312-macosx_11_0_universal2.whl.metadata (1.2 kB)
Requirement already satisfied: graphviz in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from catboost) (0.20.3)
Requirement already satisfied: matplotlib in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from catboost) (3.9.2)
Collecting numpy<2.0,>=1.16.0 (from catboost)
  Downloading numpy-1.26.4-cp312-cp312-macosx_11_0_arm64.whl.metadata (61 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.1/61.1 kB 1.9 MB/s eta 0:00:00
Requirement already satisfied: pandas>=0.24 in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from catboost) (2.2.2)
Requirement already satisfied: scipy in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from catboost) (1.14.1)
Collecting plotly (from catboost)
  Downloading plotly-5.24.1-py3-none-any.whl.metadata (7.3 kB)
Requirement already satisfied: six in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from catboost) (1.16.0)
Requirement already satisfied: python-dateutil>=2.8.2 in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from pandas>=0.24->catboost) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from pandas>=0.24->catboost) (2024.1)
Requirement already satisfied: tzdata>=2022.7 in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from pandas>=0.24->catboost) (2023.4)
Requirement already satisfied: contourpy>=1.0.1 in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from matplotlib->catboost) (1.3.0)
Requirement already satisfied: cycler>=0.10 in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from matplotlib->catboost) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from matplotlib->catboost) (4.53.1)
Requirement already satisfied: kiwisolver>=1.3.1 in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from matplotlib->catboost) (1.4.7)
Requirement already satisfied: packaging>=20.0 in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from matplotlib->catboost) (24.1)
Requirement already satisfied: pillow>=8 in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from matplotlib->catboost) (10.3.0)
Requirement already satisfied: pyparsing>=2.3.1 in /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages (from matplotlib->catboost) (3.1.4)
Collecting tenacity>=6.2.0 (from plotly->catboost)
  Downloading tenacity-9.0.0-py3-none-any.whl.metadata (1.2 kB)
Downloading catboost-1.2.7-cp312-cp312-macosx_11_0_universal2.whl (27.0 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 27.0/27.0 MB 4.7 MB/s eta 0:00:0000:0100:01
Downloading numpy-1.26.4-cp312-cp312-macosx_11_0_arm64.whl (13.7 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 13.7/13.7 MB 3.6 MB/s eta 0:00:0000:0100:01
Downloading plotly-5.24.1-py3-none-any.whl (19.1 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 19.1/19.1 MB 4.7 MB/s eta 0:00:0000:0100:01
Downloading tenacity-9.0.0-py3-none-any.whl (28 kB)
Installing collected packages: tenacity, numpy, plotly, catboost
  Attempting uninstall: numpy
    Found existing installation: numpy 2.1.1
    Uninstalling numpy-2.1.1:
      Successfully uninstalled numpy-2.1.1
Successfully installed catboost-1.2.7 numpy-1.26.4 plotly-5.24.1 tenacity-9.0.0

[notice] A new release of pip is available: 24.0 -> 24.2
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.

使用 Conda (适用于 Anaconda 用户)

如果您使用的是 Anaconda 或 Miniconda,则可以使用 Conda 安装 CatBoost。运行以下命令时,它将下载必要的包和依赖项,并将其安装到您的系统中。

conda install -c conda-forge catboost

终端输出通常如下所示:

(base) $ conda install -c conda-forge catboost
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
  environment location: /path/to/conda/environment
  added / updated specs:
    - catboost

The following packages will be downloaded:

    package                       |            build
    ------------------------------|-----------------
    catboost-1.1                   |   py38h9b699db_0   16.0 MB
    libcxx-11.1.0                  |       habf9029_0    1.3 MB
    libgfortran-5.4.0              |       h6c81a4c_10   2.2 MB
    openmp-4.5                     |       h44a4d74_1    3.1 MB
    ------------------------------------------------------------
                                           Total:   22.6 MB

The following NEW packages will be INSTALLED:

  catboost           conda-forge/osx-64::catboost-1.1-py38h9b699db_0
  libcxx             conda-forge/osx-64::libcxx-11.1.0-habf9029_0
  libgfortran        conda-forge/osx-64::libgfortran-5.4.0-h6c81a4c_10
  openmp             conda-forge/osx-64::openmp-4.5-h44a4d74_1

Proceed ([y]/n)? y

Downloading and Extracting Packages
catboost-1.1         |  16.0 MB | ########## | 100%
libcxx-11.1.0        |   1.3 MB | ########## | 100%
libgfortran-5.4.0    |   2.2 MB | ########## | 100%
openmp-4.5           |   3.1 MB | ########## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

从源码构建

如果您是高级用户,并且想要最新版本,则可以从源代码构建 CatBoost。因此,请安装 CMake、Git、Python 和系统所需的其它构建工具。首先,您需要克隆 CatBoost 仓库:

git clone https://github.com/catboost/catboost.git

终端输出如下:

$ git clone https://github.com/catboost/catboost.git
Cloning into 'catboost'...
remote: Enumerating objects: 345600, done.
remote: Counting objects: 100% (2147/2147), done.
remote: Compressing objects: 100% (1098/1098), done.
remote: Total 345600 (delta 1345), reused 1623 (delta 1019), pack-reused 343453
Receiving objects: 100% (345600/345600), 297.60 MiB | 5.50 MiB/s, done.
Resolving deltas: 100% (266248/266248), done.

运行上述命令后,您需要导航到克隆的目录:

cd catboost

然后像下面这样构建项目:

./ya make -r -DUSE_SYSTEM_PYTHON=1 catboost/python-package

终端输出如下:

$ ./ya make -r -DUSE_SYSTEM_PYTHON=1 catboost/python-package
Configuring the build...
[INFO] [18:34:56] Project configuration complete
[INFO] [18:34:57] Building CatBoost package...
[INFO] [18:34:58] Running CMake...
[INFO] [18:35:02] Generating build system...
[INFO] [18:35:03] Build started...
[ 10%] Building CXX object contrib/libs/oblivious_tree/CMakeFiles/oblivious_tree.dir/tree.cpp.o
[ 20%] Building CXX object contrib/libs/model/CMakeFiles/model.dir/model.cpp.o
[ 30%] Linking CXX static library libcatboostmodel.a
[ 40%] Building wheel for Python package
[ 60%] Linking CXX shared library libcatboost.so
[ 80%] Generating Python bindings...
[100%] Completed successfully

最后安装包:

cd catboost/python-package
pip install .

安装包后,上述命令的输出如下:

$ pip install .
Processing /path/to/catboost/python-package
  Preparing metadata (setup.py) ... done
Requirement already satisfied: numpy in /path/to/conda/env/lib/python3.8/site-packages (from catboost==1.1) (1.20.0)
Requirement already satisfied: six in /path/to/conda/env/lib/python3.8/site-packages (from catboost==1.1) (1.15.0)
Building wheels for collected packages: catboost
  Building wheel for catboost (setup.py) ... done
  Created wheel for catboost: filename=catboost-1.1-cp38-cp38-linux_x86_64.whl size=1234567
  Stored in directory: /tmp/pip-ephem-wheel-cache-abcd1234
Successfully built catboost
Installing collected packages: catboost
Successfully installed catboost-1.1

安装 R 版本

如果您想使用 R 编程语言,则可以使用以下命令安装 R 版本的 CatBoost。要运行该命令,您只需打开 R 控制台或 RStudio,然后运行该命令:

install.packages('catboost', repos = 'https://cloud.r-project.org/', dependencies=TRUE)

此命令将安装 R 编程语言的 CatBoost。

广告