XGBoost is a library for developing very fast and accurate gradient boosting models.
It is a library at the center of many winning solutions in Kaggle data science competitions.
In this tutorial, you will discover how to install the XGBoost library for Python on macOS.
Let’s get started.
This tutorial is divided into 3 parts; they are:
- Install MacPorts
- Build XGBoost
- Install XGBoost
Note: I have used this procedure for years on a range of different macOS versions and it has not changed. This tutorial was written and tested on macOS High Sierra (10.13.1).
1. Install MacPorts
You need GCC and a Python environment installed in order to build and install XGBoost for Python.
I recommend GCC 7 and Python 3.6 and I recommend installing these prerequisites using MacPorts.
- 1. For help installing MacPorts and a Python environment step-by-step, see this tutorial:
- 2. After MacPorts and a working Python environment are installed, you can install and select GCC 7 as follows:
sudo port install gcc7 sudo port select --set gcc mp-gcc7
- 3. Confirm your GCC installation was successful as follows:
You should see the version of GCC printed; for example:
.. gcc version 7.2.0 (MacPorts gcc7 7.2.0_0)
What version did you see?
Let me know in the comments below.
2. Build XGBoost
The next step is to download and compile XGBoost for your system.
- 1. First, check out the code repository from GitHub:
git clone --recursive https://github.com/dmlc/xgboost
- 2. Change into the xgboost directory.
- 3. Copy the configuration we intend to use to compile XGBoost into position.
cp make/config.mk ./config.mk
- 4. Compile XGBoost; this requires that you specify the number of cores on your system (e.g. 8, change as needed).
The build process may take a minute and should not produce any error messages, although you may see some warnings that you can safely ignore.
For example, the last snippet of the compilation might look as follows:
... 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
Did this step work for you?
Let me know in the comments below.
3. Install XGBoost
You are now ready to install XGBoost on your system.
- 1. Change directory into the Python package of the xgboost project.
- 2. Install the Python XGBoost package.
sudo python setup.py install
The installation is very fast.
For example, at the end of the installation, you may see messages like the following:
... 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 file Using /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages Searching for numpy==1.13.3 Best match: numpy 1.13.3 Adding numpy 1.13.3 to easy-install.pth file Using /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages Finished processing dependencies for xgboost==0.6
- 3. Confirm that the installation was successful by printing the xgboost version, which requires the library to be loaded.
Save the following code to a file called version.py.
import xgboost print("xgboost", xgboost.__version__)
Run the script from the command line:
You should see the XGBoost version printed to screen:
How did you do?
Post your results in the comments below.
This section provides more resources on the topic if you are looking to go deeper.
- How to Install a Python 3 Environment on Mac OS X for Machine Learning and Deep Learning
- MacPorts Installation Guide
- XGBoost Installation Guide
In this tutorial, you discovered how to install XGBoost for Python on macOS step-by-step.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.