你需要安装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 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 fornumpy== 1.13.3Best match: numpy 1.13.3Adding numpy 1.13.3toeasy-install.pth fileUsing/opt/ local/ Library/Frameworks/Python.framework/ Versions/ 3.6/lib/python3 .6/site-packagesFinished processing dependencies forxgboost== 0.6
3.通过打印xgboost版本来确认安装是否成功:
将以下代码保存到名为version.py的文件中:
importxgboostprint( "xgboost", xgboost.__version__)
从命令行运行脚本:
python version.py
如果看到XGBoost版本打印到屏幕上,则说明安装成功:
xgboost 0.6
原文:https://machinelearningmastery.com/install-xgboost-python-macos/?spm=a2c4e.11153959.blogcont460393.10.2096234ee2ZFp8
抓住AI时代机遇,从学习开始: