[−][src]Struct petal_decomposition::RandomizedPca
Principal component analysis using randomized singular value decomposition.
This uses randomized SVD (singular value decomposition) proposed by Halko et al. [1] to reduce the dimensionality of the input data. The data is centered for each feature before applying randomized SVD.
Examples
use petal_decomposition::RandomizedPca; let x = ndarray::arr2(&[[0_f64, 0_f64], [1_f64, 1_f64], [2_f64, 2_f64]]); let mut pca = RandomizedPca::new(1); let y = pca.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);
References
- N. Halko, P. G. Martinsson, and J. A. Tropp. Finding Structure with Randomness: Probabilistic Algorithms for Constructing Approximate Matrix Decompositions. SIAM Review, 53(2), 217–288, 2011.
Implementations
impl<A> RandomizedPca<A, Pcg> where
A: Scalar + Lapack,
A::Real: ScalarOperand,
[src]
A: Scalar + Lapack,
A::Real: ScalarOperand,
#[must_use]pub fn new(n_components: usize) -> Self
[src]
Creates a PCA model based on randomized SVD.
The random matrix for randomized SVD is created from a PCG random number generator (the XSL 128/64 (MCG) variant on a 64-bit CPU and the XSH RR 64/32 (LCG) variant on a 32-bit CPU), initialized with a randomly-generated seed.
#[must_use]pub fn with_seed(n_components: usize, seed: u128) -> Self
[src]
Creates a PCA model based on randomized SVD, with a PCG random number generator initialized with the given seed.
It uses a PCG random number generator (the XSL 128/64 (MCG) variant on a
64-bit CPU and the XSH RR 64/32 (LCG) variant on a 32-bit CPU). Use
with_rng
for a different random number generator.
impl<A, R> RandomizedPca<A, R> where
A: Scalar + Lapack,
A::Real: ScalarOperand,
R: Rng,
[src]
A: Scalar + Lapack,
A::Real: ScalarOperand,
R: Rng,
#[must_use]pub fn with_rng(n_components: usize, rng: R) -> Self
[src]
Creates a PCA model with the given number of components and random number generator. The random number generator is used to create a random matrix for randomized SVD.
pub fn components(&self) -> &Array2<A>
[src]
Returns the principal axes in feature space.
pub fn mean(&self) -> &Array1<A>
[src]
Returns the per-feature empirical mean.
pub fn n_components(&self) -> usize
[src]
Returns the number of components.
pub fn singular_values(&self) -> &Array1<A::Real>
[src]
Returns sigular values.
pub fn explained_variance_ratio(&self) -> Array1<A::Real>
[src]
Returns the ratio of explained variance for each component.
pub fn fit<S>(
&mut self,
input: &ArrayBase<S, Ix2>
) -> Result<(), DecompositionError> where
S: Data<Elem = A>,
[src]
&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.DecompositionError::LinalgError
if the underlying Singular Vector Decomposition routine fails.
pub fn transform<S>(
&self,
input: &ArrayBase<S, Ix2>
) -> Result<Array2<A>, DecompositionError> where
S: Data<Elem = A>,
[src]
&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.
pub fn fit_transform<S>(
&mut self,
input: &ArrayBase<S, Ix2>
) -> Result<Array2<A>, DecompositionError> where
S: Data<Elem = A>,
[src]
&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.
Errors
Returns DecompositionError::LinalgError
if the underlying Singular
Vector Decomposition routine fails.
pub fn inverse_transform<S>(
&self,
input: &ArrayBase<S, Ix2>
) -> Result<Array2<A>, DecompositionError> where
S: Data<Elem = A>,
[src]
&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, R> RefUnwindSafe for RandomizedPca<A, R> where
A: RefUnwindSafe,
R: RefUnwindSafe,
<A as Scalar>::Real: RefUnwindSafe,
A: RefUnwindSafe,
R: RefUnwindSafe,
<A as Scalar>::Real: RefUnwindSafe,
impl<A, R> Send for RandomizedPca<A, R> where
A: Send,
R: Send,
<A as Scalar>::Real: Send,
A: Send,
R: Send,
<A as Scalar>::Real: Send,
impl<A, R> Sync for RandomizedPca<A, R> where
A: Sync,
R: Sync,
<A as Scalar>::Real: Sync,
A: Sync,
R: Sync,
<A as Scalar>::Real: Sync,
impl<A, R> Unpin for RandomizedPca<A, R> where
R: Unpin,
<A as Scalar>::Real: Unpin,
R: Unpin,
<A as Scalar>::Real: Unpin,
impl<A, R> UnwindSafe for RandomizedPca<A, R> where
A: RefUnwindSafe,
R: UnwindSafe,
<A as Scalar>::Real: RefUnwindSafe + UnwindSafe,
A: RefUnwindSafe,
R: UnwindSafe,
<A as Scalar>::Real: RefUnwindSafe + UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,