CVR-Lib last update 20 Sep 2009

cvr::pca< T > Class Template Reference
[Linear Algebra]

Principal Components Analysis (PCA). More...

#include <cvrPCA.h>

Inheritance diagram for cvr::pca< T >:

Inheritance graph
[legend]
Collaboration diagram for cvr::pca< T >:

Collaboration graph
[legend]

List of all members.

Classes

class  parameters
 The parameters for the class pca. More...

Public Member Functions

 pca ()
 pca (const parameters &par)
 pca (const pca &other)
virtual ~pca ()
virtual const std::string & name () const
virtual bool apply (const matrix< T > &data, matrix< T > &result)
virtual bool apply (matrix< T > &srcdest)
virtual bool apply (const vector< T > &src, vector< T > &result)
bool setCovarianceAndMean (const matrix< T > &coVar, const vector< T > &meanVec)
virtual bool transform (const vector< T > &src, vector< T > &result) const
virtual bool transform (const matrix< T > &src, matrix< T > &result) const
virtual bool transform (matrix< T > &srcdest) const
virtual bool computeTransformMatrix (const matrix< T > &src)
virtual bool train (const matrix< T > &src)
virtual bool reconstruct (const vector< T > &coeff, vector< T > &dest) const
virtual bool reconstruct (const matrix< T > &coeff, matrix< T > &dest) const
virtual bool getTransformMatrix (matrix< T > &result) const
virtual const matrix< T > & getTransformMatrix () const
virtual bool getOffsetVector (vector< T > &result) const
virtual const vector< T > & getOffsetVector () const
virtual bool getEigenValues (vector< T > &result) const
virtual const vector< T > & getEigenValues () const
virtual bool getEigenVectors (matrix< T > &result) const
virtual bool getEigenVectorsInRows (matrix< T > &result) const
virtual const matrix< T > & getEigenVectors () const
virtual void setDimension (int k)
pcacopy (const pca &other)
pcaoperator= (const pca &other)
virtual functorclone () const
virtual functornewInstance () const
const parametersgetParameters () const
virtual bool updateParameters ()
virtual bool read (ioHandler &handler, const bool complete=true)
virtual bool write (ioHandler &handler, const bool complete=true) const
int getUsedDimension () const

Protected Member Functions

 pca (const bool createDefaultParams)
int checkDim ()
void reset ()

Protected Attributes

matrix< T > orderedEigVec_
matrix< T > transformMatrix_
vector< T > eigValues_
vector< T > offset_
vector< T > transformedOffset_
vector< T > scale_
vector< T > whiteScale_
int usedDimensionality_


Detailed Description

template<typename T>
class cvr::pca< T >

Principal Components Analysis (PCA).

Functor for computing the principal components of a data set.

It receives a set of input vectors in form of a matrix (each row of the matrix corresponds to an input vector), which will be transformed with PCA.

The first time you use the apply()-method, the transformation matrix will be computed. You can use this transformation matrix with other data sets using the transform() methods.

Please note that the eigenvector matrices will contain the eigenvector in the COLUMNS and not in the rows, as could be expected. This avoids the requirement of transposing matrices in the vector transformations.

For large data matrices is is advisable to use singular value decomposition instead of an eigensystem for the PCA. The operation will usually be faster and using less memory. To do so set parameters::useSVD to true.


Constructor & Destructor Documentation

template<typename T >
cvr::pca< T >::pca ( const bool  createDefaultParams  )  [protected]

Default constructor.

Parameters:
createDefaultParams if true (default) a default parameters object will be created.

template<typename T >
cvr::pca< T >::pca (  ) 

Default constructor.

template<typename T >
cvr::pca< T >::pca ( const parameters par  ) 

Default constructor with parameters.

template<typename T >
cvr::pca< T >::pca ( const pca< T > &  other  ) 

Copy constructor.

Parameters:
other the object to be copied

template<typename T >
virtual cvr::pca< T >::~pca (  )  [virtual]

Destructor.


Member Function Documentation

template<typename T >
virtual bool cvr::pca< T >::apply ( const vector< T > &  src,
vector< T > &  result 
) [inline, virtual]

Transforms a single vector according to a previously computed transformation matrix.

(this is an alias for the transform() method)

References cvr::pca< T >::transform().

template<typename T >
virtual bool cvr::pca< T >::apply ( matrix< T > &  srcdest  )  [virtual]

On-Place version of the transformation.

If you don't need to transform the input data, and just want to use the input matrix to compute the principal components you can use the method computeTransformMatrix(). If you just need to transform the data, without computing the transformation matrix, you can use the method transform().

Parameters:
srcdest matrix<T> with the source data, which will also contain the result.
Returns:
a reference to srcdest.

template<typename T >
virtual bool cvr::pca< T >::apply ( const matrix< T > &  data,
matrix< T > &  result 
) [virtual]

Computes the principal components of the data matrix and transforms it according to the new coordinate system.

The result is the transformed matrix. Data and result must not be references to the same matrix. Data points are expected to be in the rows of the data matrix.

If you don't need to transform the input data, and just want to use the input matrix to compute the principal components you can use the method computeTransformMatrix(). If you just need to transform the data, without computing the transformation matrix, you can use the method transform().

Parameters:
data matrix<T> with the source data.
result matrix<T> with the result data.
Returns:
true if the PCA could be computed, false otherwise

template<typename T >
int cvr::pca< T >::checkDim (  )  [protected]

Determines the intrinsic dimensionality of the data set if the user specify autoDim, otherwise return parameters::resultDim.

The member usedDimensionality will be set with the returned value

template<typename T >
virtual functor* cvr::pca< T >::clone (  )  const [virtual]

Return a pointer to a clone of this functor.

Implements cvr::functor.

template<typename T >
virtual bool cvr::pca< T >::computeTransformMatrix ( const matrix< T > &  src  )  [virtual]

Compute the transformation matrix.

Similar to the apply() method, but it does not transform the given data (this saves some time).

Parameters:
src the matrix with the input data to be analysed.
Returns:
true if transformation matrix could be computed, false otherwise

template<typename T >
pca& cvr::pca< T >::copy ( const pca< T > &  other  ) 

Copy data of "other" functor.

Parameters:
other the functor to be copied
Returns:
a reference to this functor object

template<typename T >
virtual const vector<T>& cvr::pca< T >::getEigenValues (  )  const [virtual]

Returns the previously computed eigenvalues of the covariance matrix.

Returns:
a const reference to the last computed eigenvalues

template<typename T >
virtual bool cvr::pca< T >::getEigenValues ( vector< T > &  result  )  const [virtual]

Returns the previously computed eigenvalues of the covariance matrix.

Parameters:
result the vector which will receive the eigenvalues.
Returns:
true if the values could be obtained, false otherwise.

template<typename T >
virtual const matrix<T>& cvr::pca< T >::getEigenVectors (  )  const [virtual]

Returns the previously computed eigenvectors of the covariance matrix.

Returns:
a const reference to the last computed eigenvectors

template<typename T >
virtual bool cvr::pca< T >::getEigenVectors ( matrix< T > &  result  )  const [virtual]

Returns the previously computed eigenvectors of the covariance matrix.

Parameters:
result the matrix which will receive the eigenvectors. Each column of the matrix contains one eigenvector.
Returns:
true if the vectors could be obtained, false otherwise

template<typename T >
virtual bool cvr::pca< T >::getEigenVectorsInRows ( matrix< T > &  result  )  const [virtual]

Returns the previously computed eigenvectors of the covariance matrix.

This method will call the normal getEigenVectors() methods and after that will transpose the obtained matrix, i.e. it is faster to get the eigenvectors in the columns.

Parameters:
result the matrix which will receive the eigenvectors. Each row of the matrix contains one eigenvector.
Returns:
true if the vectors could be obtained, false otherwise

template<typename T >
virtual const vector<T>& cvr::pca< T >::getOffsetVector (  )  const [virtual]

Returns the previously computed offset vector, which corresponds to the mean of the data.

Returns:
a const reference to the last computed or used offset vector.

template<typename T >
virtual bool cvr::pca< T >::getOffsetVector ( vector< T > &  result  )  const [virtual]

Returns the previously computed offset vector, which corresponds to the mean of the data.

Parameters:
result the offset vector will be written here.
Returns:
true if the matrix could be computed, false otherwise.

template<typename T >
const parameters& cvr::pca< T >::getParameters (  )  const

Return used parameters.

Reimplemented from cvr::linearAlgebraFunctor.

template<typename T >
virtual const matrix<T>& cvr::pca< T >::getTransformMatrix (  )  const [virtual]

Returns the previously computed transform matrix.

Returns:
a const reference to the last computed or used transformation matrix.

template<typename T >
virtual bool cvr::pca< T >::getTransformMatrix ( matrix< T > &  result  )  const [virtual]

Returns the previously computed transform matrix.

Parameters:
result the matrix which will receive the transformation matrix.
Returns:
true if the matrix could be computed, false otherwise.

template<typename T >
int cvr::pca< T >::getUsedDimension (  )  const [inline]

Number of dimensions considered in the transformation.

Returns:
the number of dimensions used for the transformation. It is always less or equal the number of dimensions of the input vectors.

References cvr::pca< T >::usedDimensionality_.

template<typename T >
virtual const std::string& cvr::pca< T >::name (  )  const [virtual]

Returns the name of this type ("pca").

Implements cvr::functor.

template<typename T >
virtual functor* cvr::pca< T >::newInstance (  )  const [virtual]

Return a pointer to a new instance of this functor.

Implements cvr::functor.

template<typename T >
pca& cvr::pca< T >::operator= ( const pca< T > &  other  ) 

Alias for copy.

template<typename T >
virtual bool cvr::pca< T >::read ( ioHandler handler,
const bool  complete = true 
) [virtual]

Read this functor from the given handler.

Reimplemented from cvr::functor.

template<typename T >
virtual bool cvr::pca< T >::reconstruct ( const matrix< T > &  coeff,
matrix< T > &  dest 
) const [virtual]

Reconstructs a set of data vectors dest from the given coefficients coeff, using the transformMatrix found by computeTransformMatrix() or apply() and the appropriate offset.

As usual coeff as well as dest contain one data vector per row.

Parameters:
coeff each row contains PCA coefficients for reconstruction.
dest each row is one reconstructed data vector.
Returns:
true if reconstruction was successful

template<typename T >
virtual bool cvr::pca< T >::reconstruct ( const vector< T > &  coeff,
vector< T > &  dest 
) const [virtual]

Reconstructs a data vector dest from the given coefficients coeff, using the transformMatrix found by computeTransformMatrix() or apply() and the appropriate offset.

Parameters:
coeff PCA coefficients for reconstruction.
dest reconstructed data vector.
Returns:
true if reconstruction was successful

template<typename T >
void cvr::pca< T >::reset (  )  [protected]

Resets all private members to size 0.

Used when an error occurs in the calculation of the transform matrix.

template<typename T >
bool cvr::pca< T >::setCovarianceAndMean ( const matrix< T > &  coVar,
const vector< T > &  meanVec 
)

Pass the covariance matrix and the mean values directly to the functor to generate the transform matrix.

If you know the mean and covariance of your data, you can use this method to speed up the computations of the transformation matrix. Otherwise, just call one of the apply() methods with your data vectors in the rows of the matrix. The covariance and mean vectors will be computed there automatically.

template<typename T >
virtual void cvr::pca< T >::setDimension ( int  k  )  [virtual]

Set the dimension to which the vectors should be reduced.

This is just considered when computing the transformation matrix. After this matrix is determined the (destination) dimension is fixed and just can be changed by recalculating the transformation matrix.

template<typename T >
virtual bool cvr::pca< T >::train ( const matrix< T > &  src  )  [virtual]

template<typename T >
virtual bool cvr::pca< T >::transform ( matrix< T > &  srcdest  )  const [virtual]

Transform an entire matrix according to a previously computed transformation matrix.

Unfortunately, we must choose a name different from apply.

Parameters:
srcdest the data matrix. The result will be left here too.
Returns:
true if successful, false otherwise.

template<typename T >
virtual bool cvr::pca< T >::transform ( const matrix< T > &  src,
matrix< T > &  result 
) const [virtual]

Transform an entire matrix according to a previously computed transformation matrix.

Unfortunately, we must choose a name different from apply.

Parameters:
src the data matrix
result the matrix which will receive the transformed data
Returns:
true if successful, false otherwise.

template<typename T >
virtual bool cvr::pca< T >::transform ( const vector< T > &  src,
vector< T > &  result 
) const [virtual]

Transforms a single vector according to a previously computed transformation matrix.

Parameters:
src the data vector
result the vector which will receive the transformed data
Returns:
a reference to result

Referenced by cvr::pca< T >::apply().

template<typename T >
virtual bool cvr::pca< T >::updateParameters (  )  [virtual]

Update functor's parameters.

This member initializes some internal data according to the values in the parameters.

Returns:
true if successful, false otherwise

Reimplemented from cvr::parametersManager.

template<typename T >
virtual bool cvr::pca< T >::write ( ioHandler handler,
const bool  complete = true 
) const [virtual]

Write this functor to the given handler.

Reimplemented from cvr::functor.


Member Data Documentation

template<typename T >
vector<T> cvr::pca< T >::eigValues_ [protected]

Eigenvalues.

template<typename T >
vector<T> cvr::pca< T >::offset_ [protected]

Offset.

template<typename T >
matrix<T> cvr::pca< T >::orderedEigVec_ [protected]

Ordered eigen vectors.

template<typename T >
vector<T> cvr::pca< T >::scale_ [protected]

Scale.

template<typename T >
vector<T> cvr::pca< T >::transformedOffset_ [protected]

Transformed offset.

template<typename T >
matrix<T> cvr::pca< T >::transformMatrix_ [protected]

Transformation matrix.

template<typename T >
int cvr::pca< T >::usedDimensionality_ [protected]

Dimensionality being used.

Referenced by cvr::pca< T >::getUsedDimension().

template<typename T >
vector<T> cvr::pca< T >::whiteScale_ [protected]

Whitening scale.


The documentation for this class was generated from the following file:

Generated on Sun Sep 20 22:09:01 2009 for CVR-Lib by Doxygen 1.5.8