Skip to main content

Pca

Struct Pca 

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

Source

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.

Source

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>

Source

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.

Source

pub fn components(&self) -> &[f32]

Principal axes, k * d row-major: axis i occupies [i*d .. (i+1)*d].

Source

pub fn components_shape(&self) -> (usize, usize)

Shape of Self::components as (k, d).

Source

pub fn explained_variance_ratio(&self) -> &[f32]

Fraction of total variance per component, descending.

Source

pub fn mean(&self) -> &[f32]

Column means of the training data.

Source§

impl<C: PcaComponent> Pca<C>

Available in every state — delegates to the trait method, since a generic C cannot be pattern-matched.

Source

pub fn n_components(&self) -> usize

Component count: requested before fitting, actual (clamped) after.

Trait Implementations§

Source§

impl<C: Clone + PcaComponent> Clone for Pca<C>

Source§

fn clone(&self) -> Pca<C>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<C: Debug + PcaComponent> Debug for Pca<C>

Source§

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

Formats the value using the given formatter. Read more

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> 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> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DistributionExt for T
where T: ?Sized,

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V