pub struct LocalOutlierFactor { /* private fields */ }Expand description
Local Outlier Factor (LOF) for density-based anomaly detection.
LOF detects anomalies based on local density deviation. Unlike global methods, it finds outliers in regions with varying density by comparing each point’s density to its neighbors’ densities.
§Algorithm
- For each point, find k-nearest neighbors
- Compute reachability distance for each neighbor
- Compute local reachability density (LRD) for each point
- Compute LOF score: ratio of neighbors’ LRD to point’s LRD
§LOF Score Interpretation
- LOF ≈ 1: Similar density to neighbors (normal point)
- LOF >> 1: Lower density than neighbors (outlier)
- LOF < 1: Higher density than neighbors (core point)
§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 cluster
10.0, 10.0, -10.0, -10.0, // Outliers
],
)
.expect("Valid matrix dimensions and data length");
let mut lof = LocalOutlierFactor::new()
.with_n_neighbors(3)
.with_contamination(0.3);
lof.fit(&data).expect("Fit succeeds with valid data");
// Predict returns 1 for normal, -1 for anomaly
let predictions = lof.predict(&data);
// score_samples returns LOF scores (higher = more anomalous)
let scores = lof.score_samples(&data);§Performance
- Time complexity: O(n² log k) for k-NN search
- Space complexity: O(n²) for distance matrix
Implementations§
Source§impl LocalOutlierFactor
impl LocalOutlierFactor
Sourcepub fn new() -> LocalOutlierFactor
pub fn new() -> LocalOutlierFactor
Create a new Local Outlier Factor with default parameters.
Default: 20 neighbors, 0.1 contamination
Sourcepub fn with_n_neighbors(self, n_neighbors: usize) -> LocalOutlierFactor
pub fn with_n_neighbors(self, n_neighbors: usize) -> LocalOutlierFactor
Set the number of neighbors.
Sourcepub fn with_contamination(self, contamination: f32) -> LocalOutlierFactor
pub fn with_contamination(self, contamination: f32) -> LocalOutlierFactor
Set the expected proportion of anomalies (0 to 0.5).
Sourcepub fn fit(&mut self, x: &Matrix<f32>) -> Result<(), AprenderError>
pub fn fit(&mut self, x: &Matrix<f32>) -> Result<(), AprenderError>
Fit the Local Outlier Factor on training data.
Sourcepub fn score_samples(&self, x: &Matrix<f32>) -> Vec<f32>
pub fn score_samples(&self, x: &Matrix<f32>) -> Vec<f32>
Compute LOF scores for samples.
Returns a vector of LOF scores where higher scores indicate anomalies.
Sourcepub fn predict(&self, x: &Matrix<f32>) -> Vec<i32>
pub fn predict(&self, x: &Matrix<f32>) -> Vec<i32>
Predict anomaly labels for samples.
Returns 1 for normal points and -1 for anomalies.
Sourcepub fn negative_outlier_factor(&self) -> &[f32]
pub fn negative_outlier_factor(&self) -> &[f32]
Get the negative outlier factor for training samples.
Returns negative of LOF scores (sklearn compatibility).
Trait Implementations§
Source§impl Clone for LocalOutlierFactor
impl Clone for LocalOutlierFactor
Source§fn clone(&self) -> LocalOutlierFactor
fn clone(&self) -> LocalOutlierFactor
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 LocalOutlierFactor
impl Debug for LocalOutlierFactor
Source§impl Default for LocalOutlierFactor
impl Default for LocalOutlierFactor
Source§fn default() -> LocalOutlierFactor
fn default() -> LocalOutlierFactor
Source§impl<'de> Deserialize<'de> for LocalOutlierFactor
impl<'de> Deserialize<'de> for LocalOutlierFactor
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<LocalOutlierFactor, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<LocalOutlierFactor, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Serialize for LocalOutlierFactor
impl Serialize for LocalOutlierFactor
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl Freeze for LocalOutlierFactor
impl RefUnwindSafe for LocalOutlierFactor
impl Send for LocalOutlierFactor
impl Sync for LocalOutlierFactor
impl Unpin for LocalOutlierFactor
impl UnsafeUnpin for LocalOutlierFactor
impl UnwindSafe for LocalOutlierFactor
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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