1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
use crate::binning::bin_matrix;
use crate::constraints::ConstraintMap;
use crate::data::Matrix;
use crate::errors::ForustError;
use crate::objective::{gradient_hessian_callables, ObjectiveType};
use crate::splitter::{MissingBranchSplitter, MissingImputerSplitter, Splitter};
use crate::tree::Tree;
use rand::rngs::StdRng;
use rand::SeedableRng;
use rayon::prelude::*;
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::HashMap;
use std::fs;
pub enum ContributionsMethod {
Weight,
Average,
}
/// Gradient Booster object
///
/// * `objective_type` - The name of objective function used to optimize.
/// Valid options include "LogLoss" to use logistic loss as the objective function,
/// or "SquaredLoss" to use Squared Error as the objective function.
/// * `iterations` - Total number of trees to train in the ensemble.
/// * `learning_rate` - Step size to use at each iteration. Each
/// leaf weight is multiplied by this number. The smaller the value, the more
/// conservative the weights will be.
/// * `max_depth` - Maximum depth of an individual tree. Valid values
/// are 0 to infinity.
/// * `max_leaves` - Maximum number of leaves allowed on a tree. Valid values
/// are 0 to infinity. This is the total number of final nodes.
/// * `l2` - L2 regularization term applied to the weights of the tree. Valid values
/// are 0 to infinity.
/// * `gamma` - The minimum amount of loss required to further split a node.
/// Valid values are 0 to infinity.
/// * `min_leaf_weight` - Minimum sum of the hessian values of the loss function
/// required to be in a node.
/// * `base_score` - The initial prediction value of the model.
/// * `nbins` - Number of bins to calculate to partition the data. Setting this to
/// a smaller number, will result in faster training time, while potentially sacrificing
/// accuracy. If there are more bins, than unique values in a column, all unique values
/// will be used.
/// * `allow_missing_splits` - Allow for splits to be made such that all missing values go
/// down one branch, and all non-missing values go down the other, if this results
/// in the greatest reduction of loss. If this is false, splits will only be made on non
/// missing values.
#[derive(Deserialize, Serialize)]
pub struct GradientBooster {
pub objective_type: ObjectiveType,
pub iterations: usize,
pub learning_rate: f32,
pub max_depth: usize,
pub max_leaves: usize,
pub l2: f32,
pub gamma: f32,
pub min_leaf_weight: f32,
pub base_score: f64,
pub nbins: u16,
pub parallel: bool,
pub allow_missing_splits: bool,
pub monotone_constraints: Option<ConstraintMap>,
pub subsample: f32,
pub seed: u64,
#[serde(deserialize_with = "parse_missing")]
pub missing: f64,
pub create_missing_branch: bool,
pub trees: Vec<Tree>,
metadata: HashMap<String, String>,
}
fn parse_missing<'de, D>(d: D) -> Result<f64, D::Error>
where
D: Deserializer<'de>,
{
Deserialize::deserialize(d).map(|x: Option<_>| x.unwrap_or(f64::NAN))
}
impl Default for GradientBooster {
fn default() -> Self {
Self::new(
ObjectiveType::LogLoss,
10,
0.3,
5,
usize::MAX,
1.,
0.,
1.,
0.5,
256,
true,
true,
None,
1.,
0,
f64::NAN,
false,
)
}
}
impl GradientBooster {
/// Gradient Booster object
///
/// * `objective_type` - The name of objective function used to optimize.
/// Valid options include "LogLoss" to use logistic loss as the objective function,
/// or "SquaredLoss" to use Squared Error as the objective function.
/// * `iterations` - Total number of trees to train in the ensemble.
/// * `learning_rate` - Step size to use at each iteration. Each
/// leaf weight is multiplied by this number. The smaller the value, the more
/// conservative the weights will be.
/// * `max_depth` - Maximum depth of an individual tree. Valid values
/// are 0 to infinity.
/// * `max_leaves` - Maximum number of leaves allowed on a tree. Valid values
/// are 0 to infinity. This is the total number of final nodes.
/// * `l2` - L2 regularization term applied to the weights of the tree. Valid values
/// are 0 to infinity.
/// * `gamma` - The minimum amount of loss required to further split a node.
/// Valid values are 0 to infinity.
/// * `min_leaf_weight` - Minimum sum of the hessian values of the loss function
/// required to be in a node.
/// * `base_score` - The initial prediction value of the model.
/// * `nbins` - Number of bins to calculate to partition the data. Setting this to
/// a smaller number, will result in faster training time, while potentially sacrificing
/// accuracy. If there are more bins, than unique values in a column, all unique values
/// will be used.
/// * `parallel` - Should the algorithm be run in parallel?
/// * `allow_missing_splits` - Should the algorithm allow splits that completed seperate out missing
/// and non-missing values, in the case where `create_missing_branch` is false. When `create_missing_branch`
/// is true, setting this to true will result in the missin branch being further split.
/// * `monotone_constraints` - Constraints that are used to enforce a specific relationship
/// between the training features and the target variable.
/// * `subsample` - Percent of records to randomly sample at each iteration when training a tree.
/// * `seed` - Integer value used to seed any randomness used in the algorithm.
/// * `missing` - Value to consider missing.
/// * `create_missing_branch` - Should missing be split out it's own separate branch?
#[allow(clippy::too_many_arguments)]
pub fn new(
objective_type: ObjectiveType,
iterations: usize,
learning_rate: f32,
max_depth: usize,
max_leaves: usize,
l2: f32,
gamma: f32,
min_leaf_weight: f32,
base_score: f64,
nbins: u16,
parallel: bool,
allow_missing_splits: bool,
monotone_constraints: Option<ConstraintMap>,
subsample: f32,
seed: u64,
missing: f64,
create_missing_branch: bool,
) -> Self {
GradientBooster {
objective_type,
iterations,
learning_rate,
max_depth,
max_leaves,
l2,
gamma,
min_leaf_weight,
base_score,
nbins,
parallel,
allow_missing_splits,
monotone_constraints,
subsample,
seed,
missing,
create_missing_branch,
trees: Vec::new(),
metadata: HashMap::new(),
}
}
/// Fit the gradient booster on a provided dataset.
///
/// * `data` - Either a pandas DataFrame, or a 2 dimensional numpy array.
/// * `y` - Either a pandas Series, or a 1 dimensional numpy array.
/// * `sample_weight` - Instance weights to use when
/// training the model. If None is passed, a weight of 1 will be used for every record.
pub fn fit(
&mut self,
data: &Matrix<f64>,
y: &[f64],
sample_weight: &[f64],
) -> Result<(), ForustError> {
let constraints_map = self
.monotone_constraints
.as_ref()
.unwrap_or(&ConstraintMap::new())
.to_owned();
if self.create_missing_branch {
let splitter = MissingBranchSplitter {
l2: self.l2,
gamma: self.gamma,
min_leaf_weight: self.min_leaf_weight,
learning_rate: self.learning_rate,
allow_missing_splits: self.allow_missing_splits,
constraints_map,
};
self.fit_trees(y, sample_weight, data, &splitter)?;
} else {
let splitter = MissingImputerSplitter {
l2: self.l2,
gamma: self.gamma,
min_leaf_weight: self.min_leaf_weight,
learning_rate: self.learning_rate,
allow_missing_splits: self.allow_missing_splits,
constraints_map,
};
self.fit_trees(y, sample_weight, data, &splitter)?;
};
Ok(())
}
fn fit_trees<T: Splitter>(
&mut self,
y: &[f64],
sample_weight: &[f64],
data: &Matrix<f64>,
splitter: &T,
) -> Result<(), ForustError> {
let mut rng = StdRng::seed_from_u64(self.seed);
let mut yhat = vec![self.base_score; y.len()];
let (calc_grad, calc_hess) = gradient_hessian_callables(&self.objective_type);
let mut grad = calc_grad(y, &yhat, sample_weight);
let mut hess = calc_hess(y, &yhat, sample_weight);
// Generate binned data
// TODO
// In scikit-learn, they sample 200_000 records for generating the bins.
// we could consider that, especially if this proved to be a large bottleneck...
let binned_data = bin_matrix(data, sample_weight, self.nbins, self.missing)?;
let bdata = Matrix::new(&binned_data.binned_data, data.rows, data.cols);
for _ in 0..self.iterations {
let mut tree = Tree::new();
tree.fit(
&bdata,
&binned_data.cuts,
&grad,
&hess,
splitter,
self.max_leaves,
self.max_depth,
self.parallel,
self.subsample,
&mut rng,
);
let preds = tree.predict(data, self.parallel, &self.missing);
yhat = yhat.iter().zip(preds).map(|(i, j)| *i + j).collect();
self.trees.push(tree);
grad = calc_grad(y, &yhat, sample_weight);
hess = calc_hess(y, &yhat, sample_weight);
}
Ok(())
}
/// Fit the gradient booster on a provided dataset without any weights.
///
/// * `data` - Either a pandas DataFrame, or a 2 dimensional numpy array.
/// * `y` - Either a pandas Series, or a 1 dimensional numpy array.
pub fn fit_unweighted(&mut self, data: &Matrix<f64>, y: &[f64]) -> Result<(), ForustError> {
let w = vec![1.0; data.rows];
self.fit(data, y, &w)
}
/// Generate predictions on data using the gradient booster.
///
/// * `data` - Either a pandas DataFrame, or a 2 dimensional numpy array.
pub fn predict(&self, data: &Matrix<f64>, parallel: bool) -> Vec<f64> {
let mut init_preds = vec![self.base_score; data.rows];
self.trees.iter().for_each(|tree| {
for (p_, val) in init_preds
.iter_mut()
.zip(tree.predict(data, parallel, &self.missing))
{
*p_ += val;
}
});
init_preds
}
// pub fn predict(&self, data: &Matrix<f64>, parallel: bool) -> Vec<f64> {
// // After we disconvered it's faster materializing the row once, and then
// // Passing that to each tree, let's see if we can do the same with the booster
// // prediction...
// // Clean this up..
// let mut init_preds = vec![self.base_score; data.rows];
// if parallel {
// init_preds.par_iter_mut().enumerate().for_each(|(i, p)| {
// let pred_row = data.get_row(i);
// for t in &self.trees {
// *p += t.predict_row_from_row_slice(&pred_row);
// }
// });
// } else {
// init_preds.iter_mut().enumerate().for_each(|(i, p)| {
// let pred_row = data.get_row(i);
// for t in &self.trees {
// *p += t.predict_row_from_row_slice(&pred_row);
// }
// });
// }
// init_preds
// }
pub fn predict_contributions(
&self,
data: &Matrix<f64>,
method: ContributionsMethod,
parallel: bool,
) -> Vec<f64> {
match method {
ContributionsMethod::Average => self.predict_contributions_average(data, parallel),
ContributionsMethod::Weight => self.predict_contributions_weight(data, parallel),
}
}
fn predict_contributions_weight(&self, data: &Matrix<f64>, parallel: bool) -> Vec<f64> {
let mut contribs = vec![0.; (data.cols + 1) * data.rows];
// Add the bias term to every bias value...
let bias_idx = data.cols + 1;
contribs
.iter_mut()
.skip(bias_idx - 1)
.step_by(bias_idx)
.for_each(|v| *v += self.base_score);
// Clean this up..
// materializing a row, and then passing that to all of the
// trees seems to be the fastest approach (5X faster), we should test
// something like this for normal predictions.
if parallel {
data.index
.par_iter()
.zip(contribs.par_chunks_mut(data.cols + 1))
.for_each(|(row, c)| {
let r_ = data.get_row(*row);
self.trees.iter().for_each(|t| {
t.predict_contributions_row_weight(&r_, c, &self.missing);
});
});
} else {
data.index
.iter()
.zip(contribs.chunks_mut(data.cols + 1))
.for_each(|(row, c)| {
let r_ = data.get_row(*row);
self.trees.iter().for_each(|t| {
t.predict_contributions_row_weight(&r_, c, &self.missing);
});
});
}
contribs
}
/// Generate predictions on data using the gradient booster.
/// This is equivalent to the XGBoost predict contributions with approx_contribs
///
/// * `data` - Either a pandas DataFrame, or a 2 dimensional numpy array.
fn predict_contributions_average(&self, data: &Matrix<f64>, parallel: bool) -> Vec<f64> {
let weights: Vec<Vec<f64>> = if parallel {
self.trees
.par_iter()
.map(|t| t.distribute_leaf_weights())
.collect()
} else {
self.trees
.iter()
.map(|t| t.distribute_leaf_weights())
.collect()
};
let mut contribs = vec![0.; (data.cols + 1) * data.rows];
// Add the bias term to every bias value...
let bias_idx = data.cols + 1;
contribs
.iter_mut()
.skip(bias_idx - 1)
.step_by(bias_idx)
.for_each(|v| *v += self.base_score);
// Clean this up..
// materializing a row, and then passing that to all of the
// trees seems to be the fastest approach (5X faster), we should test
// something like this for normal predictions.
if parallel {
data.index
.par_iter()
.zip(contribs.par_chunks_mut(data.cols + 1))
.for_each(|(row, c)| {
let r_ = data.get_row(*row);
self.trees.iter().zip(weights.iter()).for_each(|(t, w)| {
t.predict_contributions_row_average(&r_, c, w, &self.missing);
});
});
} else {
data.index
.iter()
.zip(contribs.chunks_mut(data.cols + 1))
.for_each(|(row, c)| {
let r_ = data.get_row(*row);
self.trees.iter().zip(weights.iter()).for_each(|(t, w)| {
t.predict_contributions_row_average(&r_, c, w, &self.missing);
});
});
}
contribs
}
/// Given a value, return the partial dependence value of that value for that
/// feature in the model.
///
/// * `feature` - The index of the feature.
/// * `value` - The value for which to calculate the partial dependence.
pub fn value_partial_dependence(&self, feature: usize, value: f64) -> f64 {
let pd: f64 = if self.parallel {
self.trees
.par_iter()
.map(|t| t.value_partial_dependence(feature, value, &self.missing))
.sum()
} else {
self.trees
.iter()
.map(|t| t.value_partial_dependence(feature, value, &self.missing))
.sum()
};
pd + self.base_score
}
/// Save a booster as a json object to a file.
///
/// * `path` - Path to save booster.
pub fn save_booster(&self, path: &str) -> Result<(), ForustError> {
let model = self.json_dump()?;
match fs::write(path, model) {
Err(e) => Err(ForustError::UnableToWrite(e.to_string())),
Ok(_) => Ok(()),
}
}
/// Dump a booster as a json object
pub fn json_dump(&self) -> Result<String, ForustError> {
match serde_json::to_string(self) {
Ok(s) => Ok(s),
Err(e) => Err(ForustError::UnableToWrite(e.to_string())),
}
}
/// Load a booster from Json string
///
/// * `json_str` - String object, which can be serialized to json.
pub fn from_json(json_str: &str) -> Result<Self, ForustError> {
let model = serde_json::from_str::<GradientBooster>(json_str);
match model {
Ok(m) => Ok(m),
Err(e) => Err(ForustError::UnableToRead(e.to_string())),
}
}
/// Load a booster from a path to a json booster object.
///
/// * `path` - Path to load booster from.
pub fn load_booster(path: &str) -> Result<Self, ForustError> {
let json_str = match fs::read_to_string(path) {
Ok(s) => Ok(s),
Err(e) => Err(ForustError::UnableToRead(e.to_string())),
}?;
Self::from_json(&json_str)
}
// Set methods for paramters
/// Set the objective_type on the booster.
/// * `objective_type` - The objective type of the booster.
pub fn set_objective_type(mut self, objective_type: ObjectiveType) -> Self {
self.objective_type = objective_type;
self
}
/// Set the iterations on the booster.
/// * `iterations` - The number of iterations of the booster.
pub fn set_iterations(mut self, iterations: usize) -> Self {
self.iterations = iterations;
self
}
/// Set the learning_rate on the booster.
/// * `learning_rate` - The learning rate of the booster.
pub fn set_learning_rate(mut self, learning_rate: f32) -> Self {
self.learning_rate = learning_rate;
self
}
/// Set the max_depth on the booster.
/// * `max_depth` - The maximum tree depth of the booster.
pub fn set_max_depth(mut self, max_depth: usize) -> Self {
self.max_depth = max_depth;
self
}
/// Set the max_leaves on the booster.
/// * `max_leaves` - The maximum number of leaves of the booster.
pub fn set_max_leaves(mut self, max_leaves: usize) -> Self {
self.max_leaves = max_leaves;
self
}
/// Set the l2 on the booster.
/// * `l2` - The l2 regulation term of the booster.
pub fn set_l2(mut self, l2: f32) -> Self {
self.l2 = l2;
self
}
/// Set the gamma on the booster.
/// * `gamma` - The gamma value of the booster.
pub fn set_gamma(mut self, gamma: f32) -> Self {
self.gamma = gamma;
self
}
/// Set the min_leaf_weight on the booster.
/// * `min_leaf_weight` - The minimum sum of the hession values allowed in the
/// node of a tree of the booster.
pub fn set_min_leaf_weight(mut self, min_leaf_weight: f32) -> Self {
self.min_leaf_weight = min_leaf_weight;
self
}
/// Set the base_score on the booster.
/// * `base_score` - The base score of the booster.
pub fn set_base_score(mut self, base_score: f64) -> Self {
self.base_score = base_score;
self
}
/// Set the nbins on the booster.
/// * `nbins` - The nummber of bins used for partitioning the data of the booster.
pub fn set_nbins(mut self, nbins: u16) -> Self {
self.nbins = nbins;
self
}
/// Set the parallel on the booster.
/// * `parallel` - Set if the booster should be trained in parallels.
pub fn set_parallel(mut self, parallel: bool) -> Self {
self.parallel = parallel;
self
}
/// Set the allow_missing_splits on the booster.
/// * `allow_missing_splits` - Set if missing splits are allowed for the booster.
pub fn set_allow_missing_splits(mut self, allow_missing_splits: bool) -> Self {
self.allow_missing_splits = allow_missing_splits;
self
}
/// Set the monotone_constraints on the booster.
/// * `monotone_constraints` - The monotone constraints of the booster.
pub fn set_monotone_constraints(mut self, monotone_constraints: Option<ConstraintMap>) -> Self {
self.monotone_constraints = monotone_constraints;
self
}
/// Set the subsample on the booster.
/// * `subsample` - Percent of the data to randomly sample when training each tree.
pub fn set_subsample(mut self, subsample: f32) -> Self {
self.subsample = subsample;
self
}
/// Set the seed on the booster.
/// * `seed` - Integer value used to see any randomness used in the algorithm.
pub fn set_seed(mut self, seed: u64) -> Self {
self.seed = seed;
self
}
/// Set missing value of the booster
/// * `missing` - Float value to consider as missing.
pub fn set_missing(mut self, missing: f64) -> Self {
self.missing = missing;
self
}
/// Insert metadata
/// * `key` - String value for the metadata key.
/// * `value` - value to assign to the metadata key.
pub fn insert_metadata(&mut self, key: String, value: String) {
self.metadata.insert(key, value);
}
/// Get Metadata
/// * `key` - Get the associated value for the metadata key.
pub fn get_metadata(&self, key: &String) -> Option<String> {
self.metadata.get(key).cloned()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_booster_fit_subsample() {
let file = fs::read_to_string("resources/contiguous_with_missing.csv")
.expect("Something went wrong reading the file");
let data_vec: Vec<f64> = file
.lines()
.map(|x| x.parse::<f64>().unwrap_or(f64::NAN))
.collect();
let file = fs::read_to_string("resources/performance.csv")
.expect("Something went wrong reading the file");
let y: Vec<f64> = file.lines().map(|x| x.parse::<f64>().unwrap()).collect();
let data = Matrix::new(&data_vec, 891, 5);
//let data = Matrix::new(data.get_col(1), 891, 1);
let mut booster = GradientBooster::default();
booster.iterations = 10;
booster.nbins = 300;
booster.max_depth = 3;
booster.subsample = 0.5;
let sample_weight = vec![1.; y.len()];
booster.fit(&data, &y, &sample_weight).unwrap();
let preds = booster.predict(&data, false);
let contribs = booster.predict_contributions(&data, ContributionsMethod::Average, false);
assert_eq!(contribs.len(), (data.cols + 1) * data.rows);
println!("{}", booster.trees[0]);
println!("{}", booster.trees[0].nodes.len());
println!("{}", booster.trees.last().unwrap().nodes.len());
println!("{:?}", &preds[0..10]);
}
#[test]
fn test_booster_fit() {
let file = fs::read_to_string("resources/contiguous_with_missing.csv")
.expect("Something went wrong reading the file");
let data_vec: Vec<f64> = file
.lines()
.map(|x| x.parse::<f64>().unwrap_or(f64::NAN))
.collect();
let file = fs::read_to_string("resources/performance.csv")
.expect("Something went wrong reading the file");
let y: Vec<f64> = file.lines().map(|x| x.parse::<f64>().unwrap()).collect();
let data = Matrix::new(&data_vec, 891, 5);
//let data = Matrix::new(data.get_col(1), 891, 1);
let mut booster = GradientBooster::default();
booster.iterations = 10;
booster.nbins = 300;
booster.max_depth = 3;
let sample_weight = vec![1.; y.len()];
booster.fit(&data, &y, &sample_weight).unwrap();
let preds = booster.predict(&data, false);
let contribs = booster.predict_contributions(&data, ContributionsMethod::Average, false);
assert_eq!(contribs.len(), (data.cols + 1) * data.rows);
println!("{}", booster.trees[0]);
println!("{}", booster.trees[0].nodes.len());
println!("{}", booster.trees.last().unwrap().nodes.len());
println!("{:?}", &preds[0..10]);
}
#[test]
fn test_tree_save() {
let file = fs::read_to_string("resources/contiguous_with_missing.csv")
.expect("Something went wrong reading the file");
let data_vec: Vec<f64> = file
.lines()
.map(|x| x.parse::<f64>().unwrap_or(f64::NAN))
.collect();
let file = fs::read_to_string("resources/performance.csv")
.expect("Something went wrong reading the file");
let y: Vec<f64> = file.lines().map(|x| x.parse::<f64>().unwrap()).collect();
let data = Matrix::new(&data_vec, 891, 5);
//let data = Matrix::new(data.get_col(1), 891, 1);
let mut booster = GradientBooster::default();
booster.iterations = 10;
booster.nbins = 300;
booster.max_depth = 3;
let sample_weight = vec![1.; y.len()];
booster.fit(&data, &y, &sample_weight).unwrap();
let preds = booster.predict(&data, true);
booster.save_booster("resources/model64.json").unwrap();
let booster2 = GradientBooster::load_booster("resources/model64.json").unwrap();
assert_eq!(booster2.predict(&data, true)[0..10], preds[0..10]);
// Test with non-NAN missing.
booster.missing = 0.;
booster.save_booster("resources/modelmissing.json").unwrap();
let booster3 = GradientBooster::load_booster("resources/modelmissing.json").unwrap();
assert_eq!(booster3.missing, 0.);
assert_eq!(booster3.missing, booster.missing);
}
}