[][src]Module smartcore::decomposition::pca

PCA is a popular approach for deriving a low-dimensional set of features from a large set of variables.

PCA

Principal components analysis (PCA) is a method that is used to select several linear combinations that capture most of the variation in your data. PCA is an unsupervised approach, since it involves only a set of features \(X1, X2, . . . , Xn\), and no associated response \(Y\). Apart from producing derived variables for use in supervised learning problems, PCA also serves as a tool for data visualization.

PCA is scale sensitive. Before PCA is performed, the variables should be centered to have mean zero. Furthermore, the results obtained also depend on whether the variables have been individually scaled. Use use_correlation_matrix parameter to standardize your variables (to mean 0 and standard deviation 1).

Example:

use smartcore::linalg::naive::dense_matrix::*;
use smartcore::decomposition::pca::*;

// Iris data
let iris = DenseMatrix::from_2d_array(&[
                    &[5.1, 3.5, 1.4, 0.2],
                    &[4.9, 3.0, 1.4, 0.2],
                    &[4.7, 3.2, 1.3, 0.2],
                    &[4.6, 3.1, 1.5, 0.2],
                    &[5.0, 3.6, 1.4, 0.2],
                    &[5.4, 3.9, 1.7, 0.4],
                    &[4.6, 3.4, 1.4, 0.3],
                    &[5.0, 3.4, 1.5, 0.2],
                    &[4.4, 2.9, 1.4, 0.2],
                    &[4.9, 3.1, 1.5, 0.1],
                    &[7.0, 3.2, 4.7, 1.4],
                    &[6.4, 3.2, 4.5, 1.5],
                    &[6.9, 3.1, 4.9, 1.5],
                    &[5.5, 2.3, 4.0, 1.3],
                    &[6.5, 2.8, 4.6, 1.5],
                    &[5.7, 2.8, 4.5, 1.3],
                    &[6.3, 3.3, 4.7, 1.6],
                    &[4.9, 2.4, 3.3, 1.0],
                    &[6.6, 2.9, 4.6, 1.3],
                    &[5.2, 2.7, 3.9, 1.4],
                    ]);

let pca = PCA::fit(&iris, PCAParameters::default().with_n_components(2)).unwrap(); // Reduce number of features to 2

let iris_reduced = pca.transform(&iris).unwrap();

Structs

PCA

Principal components analysis algorithm

PCAParameters

PCA parameters