#anomaly-detection #machine-learning

extended-isolation-forest

rust port of the anomaly detection algorithm

4 releases

0.2.3 Nov 30, 2022
0.2.2 Oct 17, 2022
0.2.1 Aug 2, 2022
0.2.0 May 12, 2021

#1890 in Algorithms

Download history 79/week @ 2025-11-03 65/week @ 2025-11-10 101/week @ 2025-11-17 47/week @ 2025-11-24 119/week @ 2025-12-01 82/week @ 2025-12-08 89/week @ 2025-12-15 141/week @ 2025-12-22 242/week @ 2025-12-29 449/week @ 2026-01-05 372/week @ 2026-01-12 713/week @ 2026-01-19 441/week @ 2026-01-26 542/week @ 2026-02-02 1142/week @ 2026-02-09 3433/week @ 2026-02-16

5,709 downloads per month
Used in 3 crates (2 directly)

MIT license

260KB
362 lines

Extended Isolation Forest

Latest Version dependency status

This is a rust port of the anomaly detection algorithm described in Extended Isolation Forest and implemented in https://github.com/sahandha/eif. For a detailed description see the paper or the github repository.

This crate requires rust >= 1.51 as it makes use of min_const_generics.

Includes optional serde support with the serde feature.

Example

use rand::distributions::Uniform;
use rand::Rng;
use extended_isolation_forest::{Forest, ForestOptions};

fn make_f64_forest() -> Forest<f64, 3> {
    let rng = &mut rand::thread_rng();
    let distribution = Uniform::new(-4., 4.);
    let distribution2 = Uniform::new(10., 50.);
    let values: Vec<_> = (0..3000)
        .map(|_| [rng.sample(distribution), rng.sample(distribution), rng.sample(distribution2)])
        .collect();

    let options = ForestOptions {
        n_trees: 150,
        sample_size: 200,
        max_tree_depth: None,
        extension_level: 1,
    };
    Forest::from_slice(values.as_slice(), &options).unwrap()
}

fn main() {
    let forest = make_f64_forest();

    // no anomaly
    assert!(forest.score(&[1.0, 3.0, 25.0]) < 0.5);
    assert!(forest.score(&[-1.0, 3.0, 25.0]) < 0.5);

    // anomalies
    assert!(forest.score(&[-12.0, 6.0, 25.0]) > 0.5);
    assert!(forest.score(&[-1.0, 2.0, 60.0]) > 0.5);
    assert!(forest.score(&[-1.0, 2.0, 0.0]) > 0.5);
}

Example: Detection anomalies in movement recordings

This example uses acceleration data recorded using a smartphone while walking up and down stairs. The anomaly was caused by a small jump. The code is in examples/walking_stairs.rs, the data itself is in data/acceleration. All data for this example was collected with the phyphox smartphone app.

The example can be executed using

cargo run --example walking_stairs

Expected result:

Dependencies

~1.5MB
~29K SLoC