Struct Booster

Source
pub struct Booster { /* private fields */ }
Expand description

Core model in LightGBM, containing functions for training, evaluating and predicting.

Implementations§

Source§

impl Booster

Source

pub fn from_file(filename: &str) -> Result<Self>

Load model from file.

Source

pub fn from_string(model_description: &str) -> Result<Self>

Load model from string.

Source

pub fn save_file(&self, filename: &str) -> Result<()>

Save model to file.

Source

pub fn save_string(&self) -> Result<String>

Save model to string. This returns the same content that save_file writes into a file.

Source

pub fn num_classes(&self) -> i32

Get the number of classes.

Source

pub fn num_features(&self) -> i32

Get the number of features.

Source

pub fn num_iterations(&self) -> i32

Get the number of iterations in the booster.

Source

pub fn max_iterations(&self) -> i32

Get the maximum number of iterations used for prediction.

Source

pub fn set_max_iterations(&mut self, max_iterations: i32) -> Result<()>

Sets the the maximum number of iterations for prediction.

Source

pub fn train(dataset: Dataset, parameters: &Value) -> Result<Self>

Trains a new model using dataset and parameters.

Example

extern crate serde_json;
use lightgbm3::{Dataset, Booster};
use serde_json::json;

let xs = vec![vec![1.0, 0.1, 0.2, 0.1],
              vec![0.7, 0.4, 0.5, 0.1],
              vec![0.9, 0.8, 0.5, 0.1],
              vec![0.2, 0.2, 0.8, 0.7],
              vec![0.1, 0.7, 1.0, 0.9]];
let labels = vec![0.0, 0.0, 0.0, 1.0, 1.0];
let dataset = Dataset::from_vec_of_vec(xs, labels, true).unwrap();
let params = json!{
   {
        "num_iterations": 3,
        "objective": "binary",
        "metric": "auc"
    }
};
let bst = Booster::train(dataset, &params).unwrap();

Full set of parameters can be found on the official LightGBM docs: https://lightgbm.readthedocs.io/en/latest/Parameters.html

Source

pub fn predict<T: DType>( &self, flat_x: &[T], n_features: i32, is_row_major: bool, ) -> Result<Vec<f64>>

Get predictions given &[f32] or &[f64] slice of features. The resulting vector will have the size of n_rows by n_classes.

Source

pub fn predict_with_params<T: DType>( &self, flat_x: &[T], n_features: i32, is_row_major: bool, params: &str, ) -> Result<Vec<f64>>

Get predictions given &[f32] or &[f64] slice of features. The resulting vector will have the size of n_rows by n_classes.

Example:

use serde_json::json;
let y_pred = bst.predict_with_params(&xs, 10, true, "num_threads=1").unwrap();
Source

pub fn raw_scores<T: DType>( &self, flat_x: &[T], n_features: i32, is_row_major: bool, ) -> Result<Vec<f64>>

Get raw scores given &[f32] or &[f64] slice of features. The resulting vector will have the size of n_rows by n_classes.

Source

pub fn raw_scores_with_params<T: DType>( &self, flat_x: &[T], n_features: i32, is_row_major: bool, parameters: &str, ) -> Result<Vec<f64>>

Get raw scores given &[f32] or &[f64] slice of features. The resulting vector will have the size of n_rows by n_classes.

Example:

use serde_json::json;
let y_pred = bst.predict_with_params(&xs, 10, true, "num_threads=1").unwrap();
Source

pub fn predict_from_vec_of_vec<T: DType>( &self, x: Vec<Vec<T>>, is_row_major: bool, ) -> Result<Vec<Vec<f64>>>

Predicts results for the given x and returns a vector or vectors (inner vectors will contain probabilities of classes per row). For regression the resulting inner vectors will have single element, so consider using predict method instead.

Input data example

let data = vec![vec![1.0, 0.1],
                vec![0.7, 0.4],
                vec![0.1, 0.7],
                vec![0.2, 0.5]];

Output data example for 3 classes:

let output = vec![vec![0.1, 0.8, 0.1],
                  vec![0.7, 0.2, 0.1],
                  vec![0.5, 0.4, 0.1],
                  vec![0.2, 0.2, 0.6],
];
Source

pub fn feature_name(&self) -> Result<Vec<String>>

Gets features names.

Source

pub fn feature_importance( &self, importance_type: ImportanceType, ) -> Result<Vec<f64>>

Get feature importance. Refer to ImportanceType

Trait Implementations§

Source§

impl Drop for Booster

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.