本文共 4111 字,大约阅读时间需要 13 分钟。
摘要: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环境的帮助,请参阅本教程:2.安装MacPorts和一个可用的Python环境之后,可以按如下方式安装GCC 7:sudo port install gcc7sudo 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.oa - build/logging.oa - build/c_api/c_api.oa - build/c_api/c_api_error.oa - build/common/common.oa - build/common/hist_util.oa - build/data/data.oa - build/data/simple_csr_source.oa - build/data/simple_dmatrix.oa - build/data/sparse_page_dmatrix.oa - build/data/sparse_page_raw_format.oa - build/data/sparse_page_source.oa - build/data/sparse_page_writer.oa - build/gbm/gblinear.oa - build/gbm/gbm.oa - build/gbm/gbtree.oa - build/metric/elementwise_metric.oa - build/metric/metric.oa - build/metric/multiclass_metric.oa - build/metric/rank_metric.oa - build/objective/multiclass_obj.oa - build/objective/objective.oa - build/objective/rank_obj.oa - build/objective/regression_obj.oa - build/predictor/cpu_predictor.oa - build/predictor/predictor.oa - build/tree/tree_model.oa - build/tree/tree_updater.oa - build/tree/updater_colmaker.oa - build/tree/updater_fast_hist.oa - build/tree/updater_histmaker.oa - build/tree/updater_prune.oa - build/tree/updater_refresh.oa - build/tree/updater_skmaker.oa - build/tree/updater_sync.oc++ -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.eggProcessing dependencies for xgboost==0.6Searching for scipy==1.0.0Best match: scipy 1.0.0Adding scipy 1.0.0 to easy-install.pth fileUsing /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packagesSearching 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-packagesFinished processing dependencies for xgboost==0.6
3.通过打印xgboost版本来确认安装是否成功:
将以下代码保存到名为version.py的文件中:import xgboostprint("xgboost", xgboost.__version__)
从命令行运行脚本:
python version.py
如果看到XGBoost版本打印到屏幕上,则说明安装成功:
xgboost 0.6
以上为译文。
文章原标题《How to Install XGBoost for Python on macOS》,译者:黄小凡,审校:袁虎。
文章为简译,更为详细的内容,请查看
转载地址:http://lmcix.baihongyu.com/