Rust bindings for XGBoost.

Overview

rust-xgboost

Travis Build Status Documentation link

Rust bindings for the XGBoost gradient boosting library.

Basic usage example:

extern crate xgboost;

use xgboost::{parameters, dmatrix::DMatrix, booster::Booster};

fn main() {
    // training matrix with 5 training examples and 3 features
    let x_train = &[1.0, 1.0, 1.0,
                    1.0, 1.0, 0.0,
                    1.0, 1.0, 1.0,
                    0.0, 0.0, 0.0,
                    1.0, 1.0, 1.0];
    let num_rows = 5;
    let y_train = &[1.0, 1.0, 1.0, 0.0, 1.0];

    // convert training data into XGBoost's matrix format
    let mut dtrain = DMatrix::from_dense(x_train, num_rows).unwrap();

    // set ground truth labels for the training matrix
    dtrain.set_labels(y_train).unwrap();

    // test matrix with 1 row
    let x_test = &[0.7, 0.9, 0.6];
    let num_rows = 1;
    let y_test = &[1.0];
    let mut dtest = DMatrix::from_dense(x_test, num_rows).unwrap();
    dtest.set_labels(y_test).unwrap();

    // build overall training parameters
    let params = parameters::ParametersBuilder::default().build().unwrap();

    // specify datasets to evaluate against during training
    let evaluation_sets = &[(&dtrain, "train"), (&dtest, "test")];

    // train model, and print evaluation data
    let bst = Booster::train(&params, &dtrain, 3, evaluation_sets).unwrap();

    println!("{:?}", bst.predict(&dtest).unwrap());
}

See the examples directory for more detailed examples of different features.

Status

Currently in a very early stage of development, so the API is changing as usability issues occur, or new features are supported.

Builds against XGBoost 0.81.

Platforms

Tested:

  • Linux
  • Mac OS

Unsupported:

  • Windows
Comments
  • Will this repository be maintained?

    Will this repository be maintained?

    @davechallis @jonathanstrong Hi.

    Do you have any plans to continue maintaining this repository?

    I'm working on LightGBM's rust bindings. https://github.com/vaaaaanquish/lightgbm-rs I have an issue where I want to integrate the interface with XGBoost.

    If the maintenance continues, I can provide some pull requests. I have the energy to take over as maintainer if you want.

    Please provide a policy. I am waiting for a good answer :)

    opened by vaaaaanquish 3
  • thread 'main' panicked at 'assertion failed: !out_result.is_null()'

    thread 'main' panicked at 'assertion failed: !out_result.is_null()'

    Hello, I'm getting this error /Users/<name>/.cargo/registry/src/github.com-1ecc6299db9ec823/xgboost-0.1.4/src/booster.rs:367:9 when trying to run a prediction on a loaded xgboost model that was trained via Python. Here is the sample code:

    extern crate xgboost;
    
    use xgboost::{Booster, DMatrix};
    
    fn main() -> Result<(), String> {
        let model = Booster::load("model.bst").unwrap();
    
        let data = &[
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0,
            100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 1.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0,
        ];
    
        let dmat = DMatrix::from_dense(data, 1).unwrap();
    
        let score = model.predict(&dmat).unwrap();
        return Ok(());
    }
    
    

    Any idea why this happens?

    opened by amackillop 1
  • Expose raw probabilities from MultiSoftmax learning objective.

    Expose raw probabilities from MultiSoftmax learning objective.

    It would be nice to have predict_proba functionality similar to that provided by scikit, to see the probability of each class in a multiclass classification model.

    opened by montanalow 1
  • Update XGBoost 1.4.2

    Update XGBoost 1.4.2

    @davechallis @jonathanstrong Hi.

    Overview

    • fix: https://github.com/davechallis/rust-xgboost/issues/8
      • upgrade xgboost 1.4.2
      • update bindgen
      • update example

    Requires

    This PR requires the following merge

    • [x] ~https://github.com/davechallis/rust-xgboost/pull/13~
      • Included in this PR
    • [ ] https://github.com/davechallis/xgboost/pull/1
    • [x] https://github.com/dmlc/xgboost/pull/7265

    Confirmed that example/runall.sh is working.

    $ head xgboost-sys/xgboost/NEWS.md
    XGBoost Change Log
    ==================
    
    This file records the changes in xgboost library in reverse chronological order.
    
    ## v1.4.2 (2021.05.13)
    
    $ ./runall.sh
    ...
    Compiling xgboost-sys v0.2.0 (/app/rust-xgboost/xgboost-sys)
    ...
    
    

    Description

    • Using CMake because removed build.sh https://github.com/dmlc/xgboost/commit/207f0587111a951f5cd6c609ade001587f242bd1
    • XGBoosterPredict's args has been changed
      • https://xgboost.readthedocs.io/en/latest/dev/c__api_8h.html#adc14afaedd5f1add105d18942a4de33c
      • 0 for predict
      • 1 for custom metric training
    • remove root_index
      • https://github.com/dmlc/xgboost/pull/5138#discussion_r360838092
    • add group_ptr
      • https://github.com/dmlc/xgboost/pull/5187
    • demo file to start with 1 index
      • https://github.com/dmlc/xgboost/commit/a894ab6898f5ed525ca7dec89a79ea1fa7f49f5a#diff-b1f466d8b5ac867fc06e6d49115839b7b8428f5c3eb9f7f11488dd677f6f7dbc
    opened by vaaaaanquish 1
  • Incorrect sequence of updaters

    Incorrect sequence of updaters

    When using the xgboost module with the basic example I am getting the below warning

    [12:49:10] DANGER AHEAD: You have manually specified `updater` parameter. The `tree_method` parameter will be ignored. Incorrect sequence of updaters will produce undefined behavior. For common uses, we recommend using `tree_method` parameter instead.
    [12:49:10] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 0 extra nodes, 0 pruned nodes, max_depth=0
    [0]	test-error:0	train-error:0.2
    [12:49:10] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 0 extra nodes, 0 pruned nodes, max_depth=0
    [1]	test-error:0	train-error:0.2
    
    opened by infinite-Joy 1
  • Update to XGBoost 1.62

    Update to XGBoost 1.62

    This PR:

    • updates to use the upstream XGBoost version 1.62
    • vendors xgboost-sys into the crate for easier distribution (I'm not sure if this is the right way to do this?)
    opened by levkk 0
  • Increment Bindgen Version

    Increment Bindgen Version

    Hi guys. Any thoughts on incrementing bindgen version? Got into an issue where another library was using a newer version of bindgen, which link to a newer version of clang.

    opened by asdacap 0
  • Change types for some `TreeBoosterParameters` to match types in XGBoost

    Change types for some `TreeBoosterParameters` to match types in XGBoost

    While working on a personal project using this repo I noticed that some of the types for the parameters in TreeBoosterParameters don't seem to match up with what's in XGBoost? You can check them out here.

    Changelog

    • Change types for a few TreeBoosterParameters to match types in XGBoost:
      • gamma: u32 -> f32
      • min_child_weight: u32 -> f32
      • max_delta_step: u32 -> f32
      • lambda: u32 -> f32
      • alpha: u32 -> f32
    • Add some missing parameters:
      • colsample_bynode: Subsample columns for each split.
      • num_parallel_tree: Number of trees to train in parallel ("boosted random forest")
    • Change updater parameter to only pass a value if user specifies one (fix: #7)
    • Add impls of From<String> and From<&str> for TreeMethod.
    • Removed some muts to suppress compiler warnings.

    I'm keen to hear feedback on the changes for the updater parameter; the main design goal for the change was "make the warning message go away", so would not be surprised if my changes aren't the best way of handling things.

    Closes #7

    opened by KyussCaesar 0
  • upgrade xgboost-sys

    upgrade xgboost-sys

    Hi there, Thank you for your great work! Is it possible to upgrade xgboost-sys which depends on an old version of clang-sys, and causes conflict with boringssl?

    error: multiple packages link to native library `clang`, but a native library can be linked only once
    
    package `clang-sys v0.22.0`
        ... which satisfies dependency `clang-sys = "^0.22.0"` (locked to 0.22.0) of package `bindgen v0.36.1`
        ... which satisfies dependency `bindgen = "^0.36"` (locked to 0.36.1) of package `xgboost-sys v0.1.2`
        ... which satisfies dependency `xgboost-sys = "^0.1.2"` (locked to 0.1.2) of package `xgboost v0.1.4`
        ... which satisfies dependency `xgboost = "^0.1.4"` (locked to 0.1.4) of package ...
    ...
    links to native library `clang`
    
    package `clang-sys v1.3.1`
        ... which satisfies dependency `clang-sys = "^1"` (locked to 1.3.1) of package `bindgen v0.59.2`
        ... which satisfies dependency `bindgen = "^0.59"` (locked to 0.59.2) of package `boring-sys v2.0.0`
        ... which satisfies dependency `boring-sys = ">=1.1.0, <3.0.0"` (locked to 2.0.0) of package `boring v2.0.0`
    
    ...
    
    opened by 1a1a11a 0
  • Python trained model deployment

    Python trained model deployment

    Hi guys,

    Thanks for all your work on this wrapper for XGboost, its awesome!

    Is it possible to take a model file trained with the xgboost python package and load it for predictions in urst using crate?

    Cheers!

    opened by CharlieBickerton 1
  • I am getting this when I tuned my parameters

    I am getting this when I tuned my parameters

    DANGER AHEAD: You have manually specified updater parameter. The tree_method parameter will be ignored. Incorrect sequence of updaters will produce undefined behaviour. For common uses, we recommend usingtree_method parameter instead.

    Thanks in advance for the great model

    opened by dataxsaiyan3 0
  • Not able to build on ubuntu machine

    Not able to build on ubuntu machine

    I have tried to use xgboost 0.1.4 on a mac and it works fine in a Mac, but not building in Ubuntu machines. This is probably because the build procedure uses clang in the backend. Building the original Xgboost (https://github.com/dmlc/xgboost) with make and cmake. I can see that there is a platform dependent build step. https://github.com/davechallis/rust-xgboost/blob/master/xgboost-sys/build.rs#L60. Not sure if this is the source of the problem.

    opened by infinite-Joy 1
  • help on safe implementation of XGBoosterGetModelRaw?

    help on safe implementation of XGBoosterGetModelRaw?

    hey -

    I was trying to implement the other side of the load/save from buffer, XGBoosterGetModelRaw, but I'm stuck, and thought maybe you could help.

    This is where I am (in Booster impl):

        /// Returns a `Vec<u8>` with model weights.
        pub fn to_vec(&self) -> XGBResult<Vec<u8>> {
            debug!("Writing Booster to_vec");
            let mut out_len = 0; 
            let mut out_dptr = ptr::null_mut();
            xgb_call!(xgboost_sys::XGBoosterGetModelRaw(self.handle, &mut out_len, out_dptr))?;
            // let bytes: &[u8] = unsafe {
            //     let length: u64 = *(out_len as *const _);
            //     std::slice::from_raw_parts(out_dptr as *const _, length as usize)
            // };
            // let mut out: Vec<u8> = vec![0u8; bytes.len()];
            // out[..].copy_from_slice(bytes);
            // Ok(out)
        }
    

    The commented out section is because I can't get past calling the xgboost function.

    I have tried a fairly wide variety of various pointer things as it relates to calling XGBoosterGetModelRaw, but I get SIGSEGV no matter what.

    The xgboost api is defined as:

    /*!
     * \brief save model into binary raw bytes, return header of the array
     * user must copy the result out, before next xgboost call
     * \param handle handle
     * \param out_len the argument to hold the output length
     * \param out_dptr the argument to hold the output data pointer
     * \return 0 when success, -1 when failure happens
     */
    XGB_DLL int XGBoosterGetModelRaw(BoosterHandle handle,
                                     bst_ulong *out_len,
                                     const char **out_dptr);
    

    Any ideas? Thanks!

    opened by jonathanstrong 1
Owner
Dave Challis
Dave Challis
Rust bindings for the C++ api of PyTorch.

tch-rs Rust bindings for the C++ api of PyTorch. The goal of the tch crate is to provide some thin wrappers around the C++ PyTorch api (a.k.a. libtorc

Laurent Mazare 2.3k Jan 1, 2023
Rust bindings for TensorFlow Lite

Rust bindings for TensorFlow Lite This crates provides TensorFlow Lite APIs. Please read the API documentation on docs.rs Using the interpreter from a

Boncheol Gu 84 Dec 11, 2022
Rust language bindings for Faiss

Faiss-rs This project provides Rust bindings to Faiss, the state-of-the-art vector search and clustering library. Installing as a dependency Currently

Eduardo Pinho 86 Jan 7, 2023
Locality Sensitive Hashing in Rust with Python bindings

lsh-rs (Locality Sensitive Hashing) Locality sensitive hashing can help retrieving Approximate Nearest Neighbors in sub-linear time. For more informat

Ritchie Vink 65 Jan 2, 2023
OpenAI Gym bindings for Rust

gym-rs OpenAI gym binding for Rust. Actively maintained! If you have any problem just create an issue. Install Just install the requierements layed ou

Mr.Robb 45 Dec 11, 2022
High-level non-blocking Deno bindings to the rust-bert machine learning crate.

bertml High-level non-blocking Deno bindings to the rust-bert machine learning crate. Guide Introduction The ModelManager class manages the FFI bindin

Carter Snook 14 Dec 15, 2022
An example of using TensorFlow rust bindings to serve trained machine learning models via Actix Web

Serving TensorFlow with Actix-Web This repository gives an example of training a machine learning model using TensorFlow2.0 Keras in python, exporting

Kyle Kosic 39 Dec 12, 2022
Docker for PyTorch rust bindings `tch`. Example of pretrain model.

tch-rs-pretrain-example-docker Docker for PyTorch rust bindings tch-rs. Example of pretrain model. Docker files support the following install libtorch

vaaaaanquish 5 Oct 7, 2022
Rust language bindings for the LIBLINEAR C/C++ library.

liblinear-rs Rust bindings for the liblinear C/C++ library. Provides a thin (but rustic) wrapper around the original C-interface exposed by the librar

Madeesh Kannan 8 Sep 22, 2022
mxnet Rust Bindings

mxnet Rust Bindings This is a work in progress. Contributions gladly accepted! The mxnet crate defines a high-level Rust API for mxnet using the mxnet

Jacob Lee 5 Sep 17, 2022
Rust bindings for darknet

Rust bindings for darknet Darknet: Convolutional Neural Networks todo rewrite the demo function used in yolo.c in rust Examples Link existing files an

Oliver Funk 8 Jul 11, 2021
SlintDotnet is a C# bindings project to enable developers to use Slint UI with .NET C#

SlintDotnet (Alpha) Slint is a UI toolkit that supports different programming languages. SlintDotnet is the integration with .NET C#. ⚠️ This is exper

Matheus Castello 9 Oct 2, 2023
Msgpack serialization/deserialization library for Python, written in Rust using PyO3, and rust-msgpack. Reboot of orjson. msgpack.org[Python]

ormsgpack ormsgpack is a fast msgpack library for Python. It is a fork/reboot of orjson It serializes faster than msgpack-python and deserializes a bi

Aviram Hassan 139 Dec 30, 2022
Practice repo for learning Rust. Currently going through "Rust for JavaScript Developers" course.

rust-practice ?? Practice repo for learning Rust. Directories /rust-for-js-dev Files directed towards "Rust for JavaScript Developers" course. Thank y

Sammy Samkough 0 Dec 25, 2021
A Rust library with homemade machine learning models to classify the MNIST dataset. Built in an attempt to get familiar with advanced Rust concepts.

mnist-classifier Ideas UPDATED: Finish CLI Flags Parallelize conputationally intensive functions Class-based naive bayes README Image parsing Confusio

Neil Kaushikkar 0 Sep 2, 2021
🦀Rust Turkiye - Rust Dersleri

Rust Turkiye - Rust Dersleri CURIOSITY - Featuring Richard Feynman Bu repo Rust Turkiye tarafindan duzenlenen Rust Dersleri egitiminin alistirma ve ko

Theo M. Bulut 12 Jan 14, 2023
A Rust machine learning framework.

Linfa linfa (Italian) / sap (English): The vital circulating fluid of a plant. linfa aims to provide a comprehensive toolkit to build Machine Learning

Rust-ML 2.2k Jan 2, 2023
Machine Learning library for Rust

rusty-machine This library is no longer actively maintained. The crate is currently on version 0.5.4. Read the API Documentation to learn more. And he

James Lucas 1.2k Dec 31, 2022
Rust library for Self Organising Maps (SOM).

RusticSOM Rust library for Self Organising Maps (SOM). Using this Crate Add rusticsom as a dependency in Cargo.toml [dependencies] rusticsom = "1.1.0"

Avinash Shenoy 26 Oct 17, 2022