pub struct IncrementalNormalizer { /* private fields */ }Expand description
Welford online normalizer – incremental zero-mean, unit-variance standardization.
Internally tracks per-feature mean and M2 (sum of squared deviations
from the current mean) so that variance is always available as M2 / count.
The struct supports two initialisation modes:
- Lazy – call
newand let the firstupdatedeterminen_featuresfrom the slice length. - Explicit – call
with_n_featuresto pre-allocate buffers and enforce a fixed dimensionality.
Implementations§
Source§impl IncrementalNormalizer
impl IncrementalNormalizer
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a normalizer with lazy dimensionality detection.
The number of features is inferred from the first call to
update.
use irithyll::preprocessing::IncrementalNormalizer;
let norm = IncrementalNormalizer::new();
assert_eq!(norm.count(), 0);Sourcepub fn with_n_features(n: usize) -> Self
pub fn with_n_features(n: usize) -> Self
Create a normalizer with a known feature count, pre-allocating buffers.
Subsequent calls to update will panic if the slice
length does not match n.
use irithyll::preprocessing::IncrementalNormalizer;
let norm = IncrementalNormalizer::with_n_features(5);
assert_eq!(norm.n_features(), Some(5));Sourcepub fn with_variance_floor(n: usize, floor: f64) -> Self
pub fn with_variance_floor(n: usize, floor: f64) -> Self
Create a normalizer with a known feature count and custom variance floor.
The floor is added to variance before taking the square root during
transformation, preventing NaN on constant features.
use irithyll::preprocessing::IncrementalNormalizer;
let norm = IncrementalNormalizer::with_variance_floor(3, 1e-8);
assert_eq!(norm.n_features(), Some(3));Sourcepub fn update(&mut self, features: &[f64])
pub fn update(&mut self, features: &[f64])
Incorporate a single sample into running statistics.
Uses Welford’s algorithm for numerically stable incremental updates:
count += 1
delta = x - mean
mean += delta / count
delta2 = x - mean (post-update)
M2 += delta * delta2§Panics
Panics if features.len() does not match the previously established
dimensionality (either from with_n_features
or from the first update call).
Sourcepub fn transform(&self, features: &[f64]) -> Vec<f64>
pub fn transform(&self, features: &[f64]) -> Vec<f64>
Standardize features using current running statistics.
Returns a new Vec<f64> where each element is
(x_i - mean_i) / sqrt(var_i + floor).
§Panics
Panics if no samples have been incorporated yet, or if the slice length does not match the established dimensionality.
Sourcepub fn transform_in_place(&self, features: &mut [f64])
pub fn transform_in_place(&self, features: &mut [f64])
Sourcepub fn update_and_transform(&mut self, features: &[f64]) -> Vec<f64>
pub fn update_and_transform(&mut self, features: &[f64]) -> Vec<f64>
Update statistics with features then return the standardized result.
This is equivalent to calling update followed by
transform, but slightly more efficient since the
sample count is guaranteed to be at least 1 after the update.
use irithyll::preprocessing::IncrementalNormalizer;
let mut norm = IncrementalNormalizer::new();
let z = norm.update_and_transform(&[5.0, 10.0]);
// After a single sample, variance is 0 so output is ~0 (floor-limited).
assert!(z[0].abs() < 1e6);Sourcepub fn mean(&self, idx: usize) -> f64
pub fn mean(&self, idx: usize) -> f64
Running mean for feature at idx.
§Panics
Panics if idx >= n_features or if no features have been set.
Sourcepub fn variance(&self, idx: usize) -> f64
pub fn variance(&self, idx: usize) -> f64
Running population variance for feature at idx.
Returns M2[idx] / count. Returns 0.0 if count == 0.
Sourcepub fn std_dev(&self, idx: usize) -> f64
pub fn std_dev(&self, idx: usize) -> f64
Running population standard deviation for feature at idx.
Equal to sqrt(variance(idx)).
Sourcepub fn n_features(&self) -> Option<usize>
pub fn n_features(&self) -> Option<usize>
Number of features, or None if lazy mode has not yet seen a sample.
Trait Implementations§
Source§impl Clone for IncrementalNormalizer
impl Clone for IncrementalNormalizer
Source§fn clone(&self) -> IncrementalNormalizer
fn clone(&self) -> IncrementalNormalizer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for IncrementalNormalizer
impl Debug for IncrementalNormalizer
Source§impl Default for IncrementalNormalizer
impl Default for IncrementalNormalizer
Source§impl StreamingPreprocessor for IncrementalNormalizer
impl StreamingPreprocessor for IncrementalNormalizer
Source§fn update_and_transform(&mut self, features: &[f64]) -> Vec<f64>
fn update_and_transform(&mut self, features: &[f64]) -> Vec<f64>
Source§fn transform(&self, features: &[f64]) -> Vec<f64>
fn transform(&self, features: &[f64]) -> Vec<f64>
Source§fn output_dim(&self) -> Option<usize>
fn output_dim(&self) -> Option<usize>
None if unknown until the first sample.Auto Trait Implementations§
impl Freeze for IncrementalNormalizer
impl RefUnwindSafe for IncrementalNormalizer
impl Send for IncrementalNormalizer
impl Sync for IncrementalNormalizer
impl Unpin for IncrementalNormalizer
impl UnsafeUnpin for IncrementalNormalizer
impl UnwindSafe for IncrementalNormalizer
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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