Skip to main content

IncrementalNormalizer

Struct IncrementalNormalizer 

Source
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:

  1. Lazy – call new and let the first update determine n_features from the slice length.
  2. Explicit – call with_n_features to pre-allocate buffers and enforce a fixed dimensionality.

Implementations§

Source§

impl IncrementalNormalizer

Source

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);
Source

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));
Source

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));
Source

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).

Source

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.

Source

pub fn transform_in_place(&self, features: &mut [f64])

Standardize features in place using current running statistics.

Equivalent to transform but writes results back into the provided mutable slice, avoiding allocation.

§Panics

Same conditions as transform.

Source

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);
Source

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.

Source

pub fn variance(&self, idx: usize) -> f64

Running population variance for feature at idx.

Returns M2[idx] / count. Returns 0.0 if count == 0.

Source

pub fn std_dev(&self, idx: usize) -> f64

Running population standard deviation for feature at idx.

Equal to sqrt(variance(idx)).

Source

pub fn n_features(&self) -> Option<usize>

Number of features, or None if lazy mode has not yet seen a sample.

Source

pub fn count(&self) -> u64

Number of samples incorporated so far.

Source

pub fn reset(&mut self)

Reset all statistics, returning the normalizer to its initial state.

Preserves n_features and variance_floor settings but clears all running accumulators.

Trait Implementations§

Source§

impl Clone for IncrementalNormalizer

Source§

fn clone(&self) -> IncrementalNormalizer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for IncrementalNormalizer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for IncrementalNormalizer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl StreamingPreprocessor for IncrementalNormalizer

Source§

fn update_and_transform(&mut self, features: &[f64]) -> Vec<f64>

Update internal statistics from this sample and return transformed features. Read more
Source§

fn transform(&self, features: &[f64]) -> Vec<f64>

Transform features using current statistics without updating them. Read more
Source§

fn output_dim(&self) -> Option<usize>

Number of output features, or None if unknown until the first sample.
Source§

fn reset(&mut self)

Reset to initial (untrained) state.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,