Skip to content

Latest commit

 

History

History
49 lines (38 loc) · 1.32 KB

README.md

File metadata and controls

49 lines (38 loc) · 1.32 KB

IsolationForest

ci-badge

Isolation forest implementation in Rust with option Python bindings.

Example

use isoforest::{IsolationForestParams, IsolationTreeParams, MaxFeatures, MaxSamples};
use ndarray::array;
use std::io::Result;

use linfa::dataset::DatasetBase;
use linfa::traits::{Fit, Predict};

fn main() -> Result<()> {
   let data = array![
       [-2.0, -1.0],
       [-1.0, -1.0],
       [-1.0, -2.0],
       [1.0, 1.0],
       [1.0, 2.0],
       [2.0, 1.0],
       [6.0, 3.0],  // anomaly
       [-4.0, 7.0]  // anomaly
   ];

   let dataset = DatasetBase::new(data.clone(), ());
   let tree_params = IsolationTreeParams::default()
       .with_max_samples(MaxSamples::Auto)
       .with_max_features(MaxFeatures::Ratio(1.0))
       .with_seed(3);

   let forest_prams = IsolationForestParams::new(3, &tree_params);

   let model = forest_prams.fit(&dataset).unwrap();
   let scores = model.decision_function(&data).unwrap();
   let preds = model.predict(&data).unwrap();

   println!("{:?}", preds);
   println!("{:?}", scores);
   // expected result [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0]
   Ok(())
}

Lincese

Licensed under the Apache License, Version 2.0