pub struct Pca<C: PcaComponent = Unfitted> { /* private fields */ }Expand description
Principal Component Analysis, state-aware via type parameter.
Fit with Pca::fit, then project new data with Pca::transform.
transform and the basis accessors exist only on Pca<Fitted>,
so projecting before fitting is a compile error rather than a runtime one:
use flow_dimensional_reduction::Pca;
let data = vec![1.0_f32, 2.0, 3.0, 4.0];
// `transform` does not exist on an unfitted model.
let _ = Pca::new(1).transform(&data, 2, 2);The default type parameter keeps Pca::new(k) working without a turbofish.
Implementations§
Source§impl Pca<Unfitted>
impl Pca<Unfitted>
Sourcepub fn new(n_components: usize) -> Self
pub fn new(n_components: usize) -> Self
Create an unfitted PCA requesting n_components components.
The actual count is clamped to the feature count d during Pca::fit.
Sourcepub fn fit(self, data: &[f32], n: usize, d: usize) -> PcaResult<Pca<Fitted>>
pub fn fit(self, data: &[f32], n: usize, d: usize) -> PcaResult<Pca<Fitted>>
Fit to n × d row-major data, consuming the unfitted model.
Means and the covariance matrix are accumulated in f64 regardless of
the f32 input, then downcast — n can be large enough that f32
accumulation loses significant precision.
§Errors
PcaError::EmptyData if n == 0 or d == 0;
PcaError::InsufficientData if n < 2;
PcaError::DimensionMismatch if data.len() != n * d;
PcaError::SvdFailed if the decomposition fails.
Source§impl Pca<Fitted>
impl Pca<Fitted>
Sourcepub fn transform(&self, data: &[f32], n: usize, d: usize) -> PcaResult<Vec<f32>>
pub fn transform(&self, data: &[f32], n: usize, d: usize) -> PcaResult<Vec<f32>>
Project n × d row-major data onto the fitted axes.
Returns n × n_components row-major.
§Errors
PcaError::DimensionMismatch if data.len() != n * d;
PcaError::FeatureMismatch if d differs from the fitted feature count.
Sourcepub fn components(&self) -> &[f32]
pub fn components(&self) -> &[f32]
Principal axes, k * d row-major: axis i occupies [i*d .. (i+1)*d].
Sourcepub fn components_shape(&self) -> (usize, usize)
pub fn components_shape(&self) -> (usize, usize)
Shape of Self::components as (k, d).
Sourcepub fn explained_variance_ratio(&self) -> &[f32]
pub fn explained_variance_ratio(&self) -> &[f32]
Fraction of total variance per component, descending.
Source§impl<C: PcaComponent> Pca<C>
Available in every state — delegates to the trait method, since a generic
C cannot be pattern-matched.
impl<C: PcaComponent> Pca<C>
Available in every state — delegates to the trait method, since a generic
C cannot be pattern-matched.
Sourcepub fn n_components(&self) -> usize
pub fn n_components(&self) -> usize
Component count: requested before fitting, actual (clamped) after.
Trait Implementations§
Auto Trait Implementations§
impl<C> Freeze for Pca<C>where
C: Freeze,
impl<C> RefUnwindSafe for Pca<C>where
C: RefUnwindSafe,
impl<C> Send for Pca<C>where
C: Send,
impl<C> Sync for Pca<C>where
C: Sync,
impl<C> Unpin for Pca<C>where
C: Unpin,
impl<C> UnsafeUnpin for Pca<C>where
C: UnsafeUnpin,
impl<C> UnwindSafe for Pca<C>where
C: UnwindSafe,
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,
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T, U> Imply<T> for U
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