Struct petal_decomposition::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
sourceimpl<A> Pca<A> where
A: FromPrimitive + Lapack,
A::Real: ScalarOperand,
impl<A> Pca<A> where
A: FromPrimitive + Lapack,
A::Real: ScalarOperand,
sourcepub fn components(&self) -> &Array2<A>
pub fn components(&self) -> &Array2<A>
Returns the principal axes in feature space.
sourcepub fn n_components(&self) -> usize
pub fn n_components(&self) -> usize
Returns the number of components.
sourcepub fn singular_values(&self) -> &Array1<A::Real>
pub fn singular_values(&self) -> &Array1<A::Real>
Returns sigular values.
sourcepub fn explained_variance_ratio(&self) -> Array1<A::Real>
pub fn explained_variance_ratio(&self) -> Array1<A::Real>
Returns the ratio of explained variance for each component.
sourcepub fn fit<S>(
&mut self,
input: &ArrayBase<S, Ix2>
) -> Result<(), DecompositionError> where
S: Data<Elem = A>,
pub fn fit<S>(
&mut self,
input: &ArrayBase<S, Ix2>
) -> Result<(), DecompositionError> where
S: Data<Elem = A>,
Fits the model with input
.
Errors
DecompositionError::InvalidInput
if any of the dimensions ofinput
is less than the number of components, or the layout ofinput
is incompatible with LAPACK.DecompositionError::LinalgError
if the underlying Singular Vector Decomposition routine fails.
sourcepub fn transform<S>(
&self,
input: &ArrayBase<S, Ix2>
) -> Result<Array2<A>, DecompositionError> where
S: Data<Elem = A>,
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 ininput
does not match that of the training data.
sourcepub fn fit_transform<S>(
&mut self,
input: &ArrayBase<S, Ix2>
) -> Result<Array2<A>, DecompositionError> where
S: Data<Elem = A>,
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
DecompositionError::InvalidInput
if any of the dimensions ofinput
is less than the number of components, or the layout ofinput
is incompatible with LAPACK.DecompositionError::LinalgError
if the underlying Singular Vector Decomposition routine fails.
sourcepub fn inverse_transform<S>(
&self,
input: &ArrayBase<S, Ix2>
) -> Result<Array2<A>, DecompositionError> where
S: Data<Elem = A>,
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.
Auto Trait Implementations
impl<A> RefUnwindSafe for Pca<A> where
A: RefUnwindSafe,
<A as Scalar>::Real: RefUnwindSafe,
impl<A> Send for Pca<A> where
A: Send,
<A as Scalar>::Real: Send,
impl<A> Sync for Pca<A> where
A: Sync,
<A as Scalar>::Real: Sync,
impl<A> Unpin for Pca<A> where
<A as Scalar>::Real: Unpin,
impl<A> UnwindSafe for Pca<A> where
A: RefUnwindSafe,
<A as Scalar>::Real: UnwindSafe + RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more