5 releases

0.0.5 Aug 5, 2024
0.0.4 Aug 4, 2024
0.0.3 May 20, 2024
0.0.2 Mar 5, 2024
0.0.1 Jun 16, 2023

#66 in Machine learning

Download history 3/week @ 2024-09-11 9/week @ 2024-09-18 17/week @ 2024-09-25 1/week @ 2024-10-02 72/week @ 2024-10-09 209/week @ 2024-10-16 189/week @ 2024-10-23 23/week @ 2024-10-30 22/week @ 2024-11-06 1/week @ 2024-11-13 16/week @ 2024-11-20 33/week @ 2024-11-27 10/week @ 2024-12-04

60 downloads per month
Used in 3 crates (via jams-core)

MIT license

150KB
2.5K SLoC

LGBM-rs

Crates.io Docs.rs Actions Status

Unofficial Rust bindings for LightGBM

Requirement

Windows or Linux

  1. Install LightGBM and build according to LightGBM Documentation.
  2. Set the environment variable LIGHTGBM_LIB_DIR to the directory containing the build output (.dll and .lib on Windows, .so on Linux).

MacOS

  1. Run brew install lightgbm and install LightGBM on your system.
  2. Set the environment variable LIGHTGBM_LIB_DIR to the directory containing lib_lightgbm.dylib.
brew install lightgbm
export LIGHTGBM_LIB_DIR=/opt/homebrew/Cellar/lightgbm/4.5.0/lib/

Example

Cargo.toml

[dependencies]
lgbm = "0.0.5"

main.rs

use lgbm::{
    parameters::{Objective, Verbosity},
    Booster, Dataset, Field, MatBuf, Parameters, PredictType,
};
use std::sync::Arc;

fn main() -> anyhow::Result<()> {
    let mut p = Parameters::new();
    p.push("num_class", 3);
    p.push("objective", Objective::Multiclass);
    p.push("verbosity", Verbosity::Fatal);

    let mut train = Dataset::from_mat(&MatBuf::from_rows(train_features()), None, &p)?;
    train.set_field(Field::LABEL, &train_labels())?;

    let mut valid = Dataset::from_mat(&MatBuf::from_rows(valid_features()), Some(&train), &p)?;
    valid.set_field(Field::LABEL, &valid_labels())?;

    let mut b = Booster::new(Arc::new(train), &p)?;
    b.add_valid_data(Arc::new(valid))?;
    for _ in 0..100 {
        if b.update_one_iter()? {
            break;
        }
    }
    let p = Parameters::new();
    let rs = b.predict_for_mat(
        &MatBuf::from_rows(test_features()),
        PredictType::Normal,
        0,
        None,
        &p,
    )?;
    println!("\n{rs:.5}");
    Ok(())
}
fn train_features() -> Vec<[f64; 1]> {
    (0..128).map(|x| [(x % 3) as f64]).collect()
}
fn train_labels() -> Vec<f32> {
    (0..128).map(|x| (x % 3) as f32).collect()
}
fn valid_features() -> Vec<[f64; 1]> {
    (0..64).map(|x| [(x % 3) as f64]).collect()
}
fn valid_labels() -> Vec<f32> {
    (0..64).map(|x| (x % 3) as f32).collect()
}
fn test_features() -> Vec<[f64; 1]> {
    (0..4).map(|x| [(x % 3) as f64]).collect()
}

output

num_data  : 4
num_class : 3
num_2     : 1

   |    0    |    1    |    2    |
---|---------|---------|---------|
 0 | 0.99998 | 0.00001 | 0.00001 |
 1 | 0.00001 | 0.99998 | 0.00001 |
 2 | 0.00001 | 0.00001 | 0.99998 |
 3 | 0.99998 | 0.00001 | 0.00001 |

Static linking or dynamic linking

The following types of linking are supported.

os static dynamic
Windows
Linux
MacOS

On Windows, if lib_lightgbm.dll exists in the directory specified by LIGHTGBM_LIB_DIR, it will be dynamically linked. Otherwise, it will be statically linked.

On Linux, if lib_lightgbm.a exists in the directory specified by LIGHTGBM_LIB_DIR, it is statically linked. Otherwise, it is dynamically linked.

License

This project is licensed under MIT. See the LICENSE files for details.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you will be under the MIT license, without any additional terms or conditions.

Dependencies

~5–7MB
~110K SLoC