pub struct IsolationForest { /* private fields */ }Expand description
Isolation Forest for anomaly detection.
Uses an ensemble of isolation trees to detect outliers based on path length. Anomalies are easier to isolate (shorter paths) than normal points.
§Algorithm
- Build N isolation trees on random subsamples
- Each tree recursively splits data by random feature + random threshold
- Compute average path length across all trees
- Convert to anomaly score (shorter path = more anomalous)
- Use contamination parameter to set classification threshold
§Examples
use aprender::prelude::*;
let data = Matrix::from_vec(
6,
2,
vec![
2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 2.0, 1.9, // Normal
10.0, 10.0, -10.0, -10.0, // Outliers
],
)
.expect("Valid matrix dimensions and data length");
let mut iforest = IsolationForest::new()
.with_contamination(0.3)
.with_random_state(42);
iforest.fit(&data).expect("Fit succeeds with valid data");
// Predict returns 1 for normal, -1 for anomaly
let predictions = iforest.predict(&data);
// score_samples returns anomaly scores (lower = more anomalous)
let scores = iforest.score_samples(&data);§Performance
- Time complexity: O(n log m) where n=samples, m=max_samples
- Space complexity: O(t * m) where t=n_estimators
Implementations§
Source§impl IsolationForest
impl IsolationForest
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new Isolation Forest with default parameters.
Default: 100 trees, auto max_samples, 0.1 contamination
Sourcepub fn with_n_estimators(self, n_estimators: usize) -> Self
pub fn with_n_estimators(self, n_estimators: usize) -> Self
Set the number of trees in the ensemble.
Sourcepub fn with_max_samples(self, max_samples: usize) -> Self
pub fn with_max_samples(self, max_samples: usize) -> Self
Set the number of samples to draw for each tree.
Sourcepub fn with_contamination(self, contamination: f32) -> Self
pub fn with_contamination(self, contamination: f32) -> Self
Set the expected proportion of anomalies (0 to 0.5).
Sourcepub fn with_random_state(self, seed: u64) -> Self
pub fn with_random_state(self, seed: u64) -> Self
Set random seed for reproducibility.
Sourcepub fn fit(&mut self, x: &Matrix<f32>) -> Result<()>
pub fn fit(&mut self, x: &Matrix<f32>) -> Result<()>
Fit the Isolation Forest on training data.
Trait Implementations§
Source§impl Clone for IsolationForest
impl Clone for IsolationForest
Source§fn clone(&self) -> IsolationForest
fn clone(&self) -> IsolationForest
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for IsolationForest
impl Debug for IsolationForest
Source§impl Default for IsolationForest
impl Default for IsolationForest
Source§impl<'de> Deserialize<'de> for IsolationForest
impl<'de> Deserialize<'de> for IsolationForest
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for IsolationForest
impl RefUnwindSafe for IsolationForest
impl Send for IsolationForest
impl Sync for IsolationForest
impl Unpin for IsolationForest
impl UnwindSafe for IsolationForest
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.