#[non_exhaustive]pub struct IsolationForest { /* private fields */ }Expand description
Isolation Forest for anomaly detection.
Builds an ensemble of random isolation trees. Points that are isolated in fewer splits (shorter path lengths) receive higher anomaly scores.
§Algorithm
- Fit: Build
n_estimatorsisolation trees, each trained on a random subsample ofmax_samplespoints. - Score: For each point, compute the average path length across all
trees. Normalize to
[0, 1]usingscore = 2^(-E[h(x)] / c(max_samples)). - Label: Scores above the threshold (determined by
contamination) are labelled as anomalies (-1), others as normal (1).
§Examples
use scry_learn::anomaly::IsolationForest;
let mut ifo = IsolationForest::new()
.n_estimators(100)
.contamination(0.05);
ifo.fit(&data).unwrap();
let labels = ifo.predict_labels(&data);Implementations§
Source§impl IsolationForest
impl IsolationForest
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new IsolationForest with default parameters.
Defaults: 100 estimators, 256 max samples, 0.1 contamination.
Sourcepub fn n_estimators(self, n: usize) -> Self
pub fn n_estimators(self, n: usize) -> Self
Set the number of isolation trees (default: 100).
Sourcepub fn max_samples(self, n: usize) -> Self
pub fn max_samples(self, n: usize) -> Self
Set the subsample size per tree (default: 256).
Sourcepub fn contamination(self, c: f64) -> Self
pub fn contamination(self, c: f64) -> Self
Set the expected contamination ratio (default: 0.1).
Must be in (0, 0.5]. Used to determine the anomaly score threshold.
Sourcepub fn random_state(self, seed: u64) -> Self
pub fn random_state(self, seed: u64) -> Self
Set the random seed for reproducibility.
Sourcepub fn seed(self, s: u64) -> Self
pub fn seed(self, s: u64) -> Self
Alias for random_state (consistent with other models).
Sourcepub fn fit(&mut self, features: &[Vec<f64>]) -> Result<()>
pub fn fit(&mut self, features: &[Vec<f64>]) -> Result<()>
Build isolation trees on the provided feature matrix.
features is row-major: features[sample_idx][feature_idx].
§Errors
Returns ScryLearnError::EmptyDataset if features is empty.
Returns ScryLearnError::InvalidParameter if contamination is out of range.
Sourcepub fn predict(&self, features: &[Vec<f64>]) -> Vec<f64>
pub fn predict(&self, features: &[Vec<f64>]) -> Vec<f64>
Compute anomaly scores for each sample.
Returns a Vec<f64> where values closer to 1.0 indicate anomalies
and values closer to 0.0 indicate normal points.
Score formula: score = 2^(-E[h(x)] / c(ψ))
where E[h(x)] is the average path length and c(ψ) is the
average BST path length for the training subsample size ψ.
Sourcepub fn predict_labels(&self, features: &[Vec<f64>]) -> Vec<i8>
pub fn predict_labels(&self, features: &[Vec<f64>]) -> Vec<i8>
Predict anomaly labels: 1 for normal, -1 for anomaly.
Uses the contamination-based threshold computed during fit.
Returns all 1 (normal) if the model has not been fitted.
Sourcepub fn score_threshold(&self) -> f64
pub fn score_threshold(&self) -> f64
Returns the anomaly score threshold determined during fit.
Trait Implementations§
Source§impl Clone for IsolationForest
impl Clone for IsolationForest
Source§fn clone(&self) -> IsolationForest
fn clone(&self) -> IsolationForest
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for IsolationForest
impl Debug for IsolationForest
Source§impl Default for IsolationForest
impl Default for IsolationForest
Auto Trait Implementations§
impl Freeze for IsolationForest
impl RefUnwindSafe for IsolationForest
impl Send for IsolationForest
impl Sync for IsolationForest
impl Unpin for IsolationForest
impl UnsafeUnpin 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
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>
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>
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 more