new PCA([arg])

Principal Components Analysis

Examples

Using default constructor

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA();

Using custom constructor

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA({ k: 5, iter: 50 });

Parameter

Name Type Optional Description

arg

(module:analytics~PCAParam or module:fs.FIn)

Yes

Construction arguments. There are two ways of constructing:
1. Using the module:analytics~PCAParam object,
2. using the file input stream module:fs.FIn.

Methods

fit(A)

Finds the eigenvectors of the variance matrix.

Example

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA();
// create matrix
var matrix = new la.Matrix([[0, 1], [-1, 0]]);
// fit the matrix
pca.fit(matrix);

Parameter

Name Type Optional Description

A

module:la.Matrix

 

Matrix whose columns correspond to examples.

getModel() → Object

Returns the model.

Example

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA();
// create matrix
var matrix = new la.Matrix([[0, 1], [-1, 0]]);
// fit matrix before getting the model
pca.fit(matrix)
// get your model using function getModel
var model = pca.getModel();
Returns

ObjectB The object pcaModel containing the properties:
1. pcaModel.P - The eigenvectors. Type module:la.Matrix.
2. pcaModel.lambda - The eigenvalues. Type module:la.Vector.
3. pcaModel.mu - The mean values. Type module:la.Vector.

getParams() → moduel:analytics~PCAParam

Gets parameters.

Examples

Using default constructor

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA();
// check the constructor parameters
var paramvalue = pca.getParams();

Using custom constructor

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA();
// set parameters
pca.setParams({iter: 10, k: 5});
// check the changed parameters
var paramvalue = pca.getParams();
Returns

moduel:analytics~PCAParamB The constructor parameters.

inverseTransform(x) → (module:la.Vector or module:la.Matrix)

Reconstructs the vector in the original space, reverses centering.

Examples

Inverse transform of matrix

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA();
// create matrix
var matrix = new la.Matrix([[0, 1], [-1, 0]]);
// fit the matrix
pca.fit(matrix);
var model = pca.getModel();
// use inverseTransform on matrix
var invTransform = pca.inverseTransform(matrix);

Inverse transform of vector

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA();
// create vector
var vector = new la.Vector([0, -1]);
// create matrix
var matrix = new la.Matrix([[0, 1], [-1, 0]]);
// fit the matrix
pca.fit(matrix);
var model = pca.getModel();
// use inverseTransform on vector
var invTransform = pca.inverseTransform(vector);

Parameter

Name Type Optional Description

x

(module:la.Vector or module:la.Matrix)

 

Test vector or matrix with column examples, in the PCA space.

Returns

(module:la.Vector or module:la.Matrix)B Returns the reconstruction.

save(fout) → module:fs.FOut

Saves the model.

Example

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA();
// create matrix
var matrix = new la.Matrix([[0, 1], [-1, 0]]);
// fit matrix
pca.fit(matrix);
var model = pca.getModel();
// save model
pca.save(require('qminer').fs.openWrite('pca_test.bin')).close();

Parameter

Name Type Optional Description

fout

module:fs.FOut

 

The output stream.

Returns

module:fs.FOutB The output stream fout.

setParams(param)

Sets parameters.

Example

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA();
// set 5 eigenvectors and 10 iterations using setParams
pca.setParams({iter: 10, k: 5});

Parameter

Name Type Optional Description

param

module:analytics~PCAParam

 

The constructor parameters.

transform(x) → (module:la.Vector or module:la.Matrix)

Projects the example(s) and expresses them as coefficients in the eigenvector basis this.P. Recovering the data in the original space: (this.P).multiply(p), where p's rows are the coefficients in the eigenvector basis.

Examples

Transforming the matrix

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA();
// create matrix
var matrix = new la.Matrix([[0, 1], [-1, 0]]);
// fit the matrix
pca.fit(matrix);
var model = pca.getModel();
// transform matrix
var transform = pca.transform(matrix);

Transforming the vector

// import analytics module
var analytics = require('qminer').analytics;
// construct model
var pca = new analytics.PCA();
// create vector you wish to transform
var vector = new la.Vector([0, -1]);
// create matrix
var matrix = new la.Matrix([[0, 1], [-1, 0]]);
// fit the matrix
pca.fit(matrix);
var model = pca.getModel();
// transform vector
var transform = pca.transform(vector);

Parameter

Name Type Optional Description

x

(module:la.Vector or module:la.Matrix)

 

Test vector or matrix with column examples.

Returns

(module:la.Vector or module:la.Matrix)B Returns projected vector or matrix.