pcaTransform#

Purpose#

Reduces the dimension of a matrix using principal component vectors previously returned by pcaFit().

Format#

X_transform = pcaTransform(X, mdl)#
Parameters:
  • X (NxP matrix) – The matrix to transform. This matrix must have the same number of columns as the matrix passed to pcaFit().

  • mdl (pcaModel structure) – This must have been previously been filled in by a call to pcaFit().

Returns:

X_transform (Nxn_components matrix) – The input matrix projected on the component vectors returned by a previous call to pcaFit().

Examples#

new;
library gml;

/*
** Load and prepare data
*/
// Get file name with full path
fname = getGAUSSHome("pkgs/gml/examples/winequality.csv");

// Load data
X = loadd(fname, ". -quality");

// Split data
X_train = X[1:1000,.];
X_test = X[1001:rows(X), .];

/*
** Fit model using
** training data
*/
// Number of components
n_components = 3;

struct pcaModel mdl;
mdl = pcaFit(X_train, n_components);

/*
** Transform test data
*/
X_transform = pcaTransform(X_test, mdl);

After the above code, the first 5 rows of X_transform will be:

       PC1              PC2              PC3
 37.441282        1.2145282       -1.5416867
-2.0454164       -15.738950        1.0084994
 21.315231       -2.4328631       0.15655108
 41.776957        2.2901582       -2.2804431
0.73984770       -12.260074      -0.68265628