Functions to run xgboost in Matlab

Two files are provided: xgboost_train and xgboost_test which call the xgboost dll from inside Matlab. The example is for classification.
3.5K Downloads
Updated 22 Sep 2021

View License

Two files are provided: xgboost_train and xgboost_test which call the xgboost dll from inside Matlab. The example is for classification. An evaluation criterion for stopping the learning process iterations can be supplied.
Supported evaluation criteria are 'AUC', 'Accuracy', 'None'.
'AUC' and 'Accuracy' require the statistics toolbox.
If eval_metric == 'None', the learning will be performed for max_num_iters, without internal cross validation.
Your own "outside" cross validation procedure can be used, which calls xgboost_train.m. An example of such outside procedure is documented in xgboost_train.m
The functions require that xgboost.dll and xgboost.h are available.
Steps to compile xgboost library and use it in Matlab:
Windows:
Step 1: create xgboost.dll
Follow these instructions: https://xgboost.readthedocs.io/en/latest/build.html#build-the-shared-library
- make folder D:\r\xgboost (e.g.)
- create an empty git repository
- pull from https://github.com/dmlc/xgboost
- Git bash here (D:\r\xgboost) - open a git bash. In it type:
- git submodule init
- git submodule update
- install cmake and add path to the env (automatically, just select the option)
= https://cgold.readthedocs.io/en/latest/first-step/installation.html
= download and install: https://github.com/Kitware/CMake/releases/download/v3.17.2/cmake-3.17.2-win64-x64.msi
- In dos, go to folder D:\r\xgboost. In it execute:
mkdir build
cd build
cmake .. -G"Visual Studio 14 2015 Win64"
# for VS15: cmake .. -G"Visual Studio 15 2017" -A x64
# for VS16: cmake .. -G"Visual Studio 16 2019" -A x64
cmake --build . --config Release
Result:
xgboost.dll is created here: "D:\r\xgboost\lib\xgboost.dll"
Step 2: get a header file:
- download header file: https://raw.githubusercontent.com/dmlc/xgboost/master/include/xgboost/c_api.h
- save it to "D:\r\xgboost\lib"
- rename c_api.h to xgboost.h
Result:
xgboost.h is created here: "D:\r\xgboost\lib\xgboost.h"
Step 3: run the example which is provided in comments in the script (xgboost_train.m)
- In xgboost_test.m and xgboost_train.m, change D:\r\xgboost\lib to the location of xgboost (only if you put it in another directory)
- The script utilizes an explanation on "Using XGBOOST in c++" provided here: https://stackoverflow.com/questions/36071672/using-xgboost-in-c
Alternatively, the xgboost_install script can be used:
Step 1:
Download the wheel file from https://s3-us-west-2.amazonaws.com/xgboost-nightly-builds/list.html
(see https://xgboost.readthedocs.io/en/latest/build.html)
Step 2:
Set the xgboost_install_dir and wheel_fn in xgboost_install.m
Step 3:
run xgboost_install
Linux:
Linux, tested on Ubuntu 18.04.4 LTS (bionic)
Step 1: create libxgboost.so
Follow these instructions: https://xgboost.readthedocs.io/en/latest/build.html#build-the-shared-library
- make folder ~/xgboost (e.g.)
- git clone --recursive https://github.com/dmlc/xgboost
- install cmake. 3.12 or higher
since bionic's default package archive, contained version 3.10.2-1, this required adding a PPA:
(see https://launchpad.net/~hnakamur/+archive/ubuntu/cmake)
= sudo add-apt-repository ppa:hnakamur/libarchive
= sudo add-apt-repository ppa:hnakamur/libzstd
= sudo add-apt-repository ppa:hnakamur/cmake
= sudo apt update
= sudo apt install cmake
- In Konsole, go to folder ~/xgboost. In it execute:
mkdir build
cd build
cmake ..
make -j$(nproc)
Result:
libxgboost.so is created here: "~/xgboost/lib/libxgboost.so"
Step 2: get a header file:
- download header file: https://raw.githubusercontent.com/dmlc/xgboost/master/include/xgboost/c_api.h
- save it to "~/xgboost/lib"
- in c_api.h, change the line "define XGB_DLL XGB_EXTERN_C _attribute_ ((visibility ("default")))" into "define XGB_DLL XGB_EXTERN_C"
- rename c_api.h to xgboost.h
- rename libxgboost.so to xgboost.so
Result:
xgboost.h is created here: "~/xgboost/lib/xgboost.h"
xgboost.so is created here: "~/xgboost/lib/xgboost.so"
Step 3: run the example which is provided in comments in the script (xgboost_train.m)
- In xgboost_test.m and xgboost_train.m, change D:\r\xgboost\lib to the location of xgboost (for example: ~/xgboost)
- The script utilizes an explanation on "Using XGBOOST in c++" provided here: https://stackoverflow.com/questions/36071672/using-xgboost-in-c
For installation instructions for macOS, see install_xgboost_matlab_macos.txt
Additionally:
See https://xgboost.readthedocs.io/en/stable/dev/c__api_8h.html for info on the xgboost library functions.
See https://xgboost.readthedocs.io/en/latest/parameter.html for info on xgboost inputs parameters.
The current example is for classification, but xgboost can also be used for regression. In this case the evaluation criterion has to be changed from AUC to MSE or MAE.

Cite As

Jeffrey van Prehn (2024). Functions to run xgboost in Matlab (https://www.mathworks.com/matlabcentral/fileexchange/75898-functions-to-run-xgboost-in-matlab), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2019a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.9

Clarify in installation steps that the location of XGBoost is hard-coded in xgboost_test.m and xgboost_train.m

1.0.8

Provide installation instructions for macOS

1.0.7

bugfix: take into account that matlab matrices are column-major and the c api expects row-major matrices. We provided a simple example for training and an example how to plot an AUC curve on a testset.

1.0.6

Update description with xgboost_install script usage

1.0.5

Install script for Windows

1.0.4

Provide instructions for Linux

1.0.3

Add example cross validation procedure for tuning two parameters as a comment section within xgboost_train.m

1.0.2

re-added xgboost_test.m (was removed accidentally in the upgrade to version 1.0.1)

1.0.1

- Make dependency on statistics toolbox optional, by supporting eval_metric 'None' (before, only AUC was supported)
- Support eval_metric 'Accuracy'

1.0.0