pub struct Booster { /* private fields */ }
Expand description
Core model in LightGBM, containing functions for training, evaluating and predicting.
Implementations§
Source§impl Booster
impl Booster
Sourcepub fn from_string(model_description: &str) -> Result<Self>
pub fn from_string(model_description: &str) -> Result<Self>
Load model from string.
Sourcepub fn save_string(&self) -> Result<String>
pub fn save_string(&self) -> Result<String>
Save model to string. This returns the same content that save_file
writes into a file.
Sourcepub fn num_classes(&self) -> i32
pub fn num_classes(&self) -> i32
Get the number of classes.
Sourcepub fn num_features(&self) -> i32
pub fn num_features(&self) -> i32
Get the number of features.
Sourcepub fn num_iterations(&self) -> i32
pub fn num_iterations(&self) -> i32
Get the number of iterations in the booster.
Sourcepub fn max_iterations(&self) -> i32
pub fn max_iterations(&self) -> i32
Get the maximum number of iterations used for prediction.
Sourcepub fn set_max_iterations(&mut self, max_iterations: i32) -> Result<()>
pub fn set_max_iterations(&mut self, max_iterations: i32) -> Result<()>
Sets the the maximum number of iterations for prediction.
Sourcepub fn train(dataset: Dataset, parameters: &Value) -> Result<Self>
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, ¶ms).unwrap();
Full set of parameters can be found on the official LightGBM docs: https://lightgbm.readthedocs.io/en/latest/Parameters.html
Sourcepub fn predict<T: DType>(
&self,
flat_x: &[T],
n_features: i32,
is_row_major: bool,
) -> Result<Vec<f64>>
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
.
Sourcepub fn predict_with_params<T: DType>(
&self,
flat_x: &[T],
n_features: i32,
is_row_major: bool,
params: &str,
) -> Result<Vec<f64>>
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();
Sourcepub fn raw_scores<T: DType>(
&self,
flat_x: &[T],
n_features: i32,
is_row_major: bool,
) -> Result<Vec<f64>>
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
.
Sourcepub fn raw_scores_with_params<T: DType>(
&self,
flat_x: &[T],
n_features: i32,
is_row_major: bool,
parameters: &str,
) -> Result<Vec<f64>>
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();
Sourcepub fn predict_from_vec_of_vec<T: DType>(
&self,
x: Vec<Vec<T>>,
is_row_major: bool,
) -> Result<Vec<Vec<f64>>>
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],
];
Sourcepub fn feature_name(&self) -> Result<Vec<String>>
pub fn feature_name(&self) -> Result<Vec<String>>
Gets features names.
Sourcepub fn feature_importance(
&self,
importance_type: ImportanceType,
) -> Result<Vec<f64>>
pub fn feature_importance( &self, importance_type: ImportanceType, ) -> Result<Vec<f64>>
Get feature importance. Refer to ImportanceType