Pca

Struct Pca 

Source
pub struct Pca<A>
where A: Scalar,
{ /* private fields */ }
Expand description

Principal component analysis.

This reduces the dimensionality of the input data using Singular Value Decomposition (SVD). The data is centered for each feature before applying SVD.

§Examples

use petal_decomposition::PcaBuilder;

let x = ndarray::arr2(&[[0_f64, 0_f64], [1_f64, 1_f64], [2_f64, 2_f64]]);
let y = PcaBuilder::new(1).build().fit_transform(&x).unwrap();  // [-2_f64.sqrt(), 0_f64, 2_f64.sqrt()]
assert!((y[(0, 0)].abs() - 2_f64.sqrt()).abs() < 1e-8);
assert!(y[(1, 0)].abs() < 1e-8);
assert!((y[(2, 0)].abs() - 2_f64.sqrt()).abs() < 1e-8);

Implementations§

Source§

impl<A> Pca<A>
where A: Scalar,

Source

pub fn new(n_components: usize) -> Self

Creates a PCA model with the given number of components.

Source§

impl<A> Pca<A>
where A: FromPrimitive + Lapack, A::Real: ScalarOperand,

Source

pub fn components(&self) -> &Array2<A>

Returns the principal axes in feature space.

Source

pub fn mean(&self) -> &Array1<A>

Returns the per-feature empirical mean.

Source

pub fn n_components(&self) -> usize

Returns the number of components.

Source

pub fn singular_values(&self) -> &Array1<A::Real>

Returns sigular values.

Source

pub fn explained_variance_ratio(&self) -> Array1<A::Real>

Returns the ratio of explained variance for each component.

Source

pub fn fit<S>( &mut self, input: &ArrayBase<S, Ix2>, ) -> Result<(), DecompositionError>
where S: Data<Elem = A>,

Fits the model with input.

§Errors
Source

pub fn transform<S>( &self, input: &ArrayBase<S, Ix2>, ) -> Result<Array2<A>, DecompositionError>
where S: Data<Elem = A>,

Applies dimensionality reduction to input.

§Errors
  • DecompositionError::InvalidInput if the number of features in input does not match that of the training data.
Source

pub fn fit_transform<S>( &mut self, input: &ArrayBase<S, Ix2>, ) -> Result<Array2<A>, DecompositionError>
where S: Data<Elem = A>,

Fits the model with input and apply the dimensionality reduction on input.

This is equivalent to calling both fit and transform for the same input, but more efficient.

§Errors
Source

pub fn inverse_transform<S>( &self, input: &ArrayBase<S, Ix2>, ) -> Result<Array2<A>, DecompositionError>
where S: Data<Elem = A>,

Transforms data back to its original space.

§Errors

Returns DecompositionError::InvalidInput if the number of rows of input is different from that of the training data, or the number of columns of input is different from the number of components.

Trait Implementations§

Source§

impl<'de, A> Deserialize<'de> for Pca<A>
where for<'a> A: Scalar + Serialize + Deserialize<'a>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<A> Serialize for Pca<A>
where for<'a> A: Scalar + Serialize + Deserialize<'a>,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<A> Freeze for Pca<A>
where <A as Scalar>::Real: Freeze,

§

impl<A> RefUnwindSafe for Pca<A>

§

impl<A> Send for Pca<A>
where <A as Scalar>::Real: Send, A: Send,

§

impl<A> Sync for Pca<A>
where <A as Scalar>::Real: Sync, A: Sync,

§

impl<A> Unpin for Pca<A>
where <A as Scalar>::Real: Unpin,

§

impl<A> UnwindSafe for Pca<A>

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,