如何在macOS上为Python安装XGBoost

摘要:XGBoost是一个开发非常快速和准确的梯度增强模型的库,它在Kaggle数据科学竞赛中被大量的kaggle选手选用,其中包括两个以上kaggle比赛的夺冠方案。在本教程中,你将了解如何在macOS上为Python安装XGBoost库。

教程概述

本教程分为以下3个部分:

1.安装MacPorts;

2.构建XGBoost;

3.安装XGBoost。

注意:我在一系列不同的macOS版本上都是使用这个过程。本教程是在macOS High Sierra(10.13.1)上编写和测试的。

安装MacPorts

你需要安装GCC和Python环境,以便为Python构建和安装XGBoost。

我推荐GCC 7和Python 3.6,我建议使用MacPorts安装这些软件。

1.有关逐步安装MacPorts和Python环境的帮助,请参阅本教程:

如何在Mac OS X上安装Python 3环境以进行机器学习和深度学习

2.安装MacPorts和一个可用的Python环境之后,可以按如下方式安装GCC 7:

sudo port install gcc7
sudo port select --set gcc mp-gcc7

3.通过查看GCC版本来确定GCC是否安装成功,如下所示:

gcc -v

你应该看到GCC的版本打印; 例如:

gcc version 7.2.0 (MacPorts gcc7 7.2.0_0)

构建XGBoost

下一步是为你的系统下载和编译XGBoost。

1.首先,从GitHub下载代码库:

git clone --recursive https://github.com/dmlc/xgboost

2.更改到xgboost目录:

cd xgboost/

3.从下载的make目录里面复制用来编译XGBoost的配置文件:

cp make/config.mk ./config.mk

4.编译XGBoost; 携带你指定系统上的核心数(例如8,根据需要更改):

make -j8

构建过程可能需要一分钟,如果编译正常则不会产生任何错误消息,虽然可能会看到一些警告,但是这些警告可以忽略。

例如,编译的最后一个片段可能如下所示:

a - build/learner.o
a - build/logging.o
a - build/c_api/c_api.o
a - build/c_api/c_api_error.o
a - build/common/common.o
a - build/common/hist_util.o
a - build/data/data.o
a - build/data/simple_csr_source.o
a - build/data/simple_dmatrix.o
a - build/data/sparse_page_dmatrix.o
a - build/data/sparse_page_raw_format.o
a - build/data/sparse_page_source.o
a - build/data/sparse_page_writer.o
a - build/gbm/gblinear.o
a - build/gbm/gbm.o
a - build/gbm/gbtree.o
a - build/metric/elementwise_metric.o
a - build/metric/metric.o
a - build/metric/multiclass_metric.o
a - build/metric/rank_metric.o
a - build/objective/multiclass_obj.o
a - build/objective/objective.o
a - build/objective/rank_obj.o
a - build/objective/regression_obj.o
a - build/predictor/cpu_predictor.o
a - build/predictor/predictor.o
a - build/tree/tree_model.o
a - build/tree/tree_updater.o
a - build/tree/updater_colmaker.o
a - build/tree/updater_fast_hist.o
a - build/tree/updater_histmaker.o
a - build/tree/updater_prune.o
a - build/tree/updater_refresh.o
a - build/tree/updater_skmaker.o
a - build/tree/updater_sync.o
c++ -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -o xgboost build/cli_main.o build/learner.o build/logging.o build/c_api/c_api.o build/c_api/c_api_error.o build/common/common.o build/common/hist_util.o build/data/data.o build/data/simple_csr_source.o build/data/simple_dmatrix.o build/data/sparse_page_dmatrix.o build/data/sparse_page_raw_format.o build/data/sparse_page_source.o build/data/sparse_page_writer.o build/gbm/gblinear.o build/gbm/gbm.o build/gbm/gbtree.o build/metric/elementwise_metric.o build/metric/metric.o build/metric/multiclass_metric.o build/metric/rank_metric.o build/objective/multiclass_obj.o build/objective/objective.o build/objective/rank_obj.o build/objective/regression_obj.o build/predictor/cpu_predictor.o build/predictor/predictor.o build/tree/tree_model.o build/tree/tree_updater.o build/tree/updater_colmaker.o build/tree/updater_fast_hist.o build/tree/updater_histmaker.o build/tree/updater_prune.o build/tree/updater_refresh.o build/tree/updater_skmaker.o build/tree/updater_sync.o dmlc-core/libdmlc.a rabit/lib/librabit.a -pthread -lm -fopenmp

安装XGBoost

现在准备在你的系统上安装XGBoost。

1.将目录切换到xgboost项目的Python包中:

cd python-package

2.安装Python XGBoost包:

sudo python setup.py install

安装非常快,在安装结束时,你可能会看到以下消息:

Installed /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xgboost-0.6-py3.6.egg
Processing dependencies for xgboost==0.6
Searching for scipy==1.0.0
Best match: scipy 1.0.0
Adding scipy 1.0.0 to easy-install.pth fileUsing /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
Searching for numpy==1.13.3Best match: numpy 1.13.3Adding numpy 1.13.3 to easy-install.pth fileUsing /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
Finished processing dependencies for xgboost==0.6

3.通过打印xgboost版本来确认安装是否成功:

将以下代码保存到名为version.py的文件中:

import xgboost
print("xgboost", xgboost.__version__)

从命令行运行脚本:

python version.py

如果看到XGBoost版本打印到屏幕上,则说明安装成功:

xgboost 0.6

以上为译文。

文章原标题《How to Install XGBoost for Python on macOS》,译者:黄小凡,审校:袁虎。

相关推荐