shark::PCA Class Reference

Principal Component Analysis. More...

#include <shark/Algorithms/Trainers/PCA.h>

+ Inheritance diagram for shark::PCA:

Public Types

enum  PCAAlgorithm { STANDARD , SMALL_SAMPLE , AUTO }
 
- Public Types inherited from shark::AbstractUnsupervisedTrainer< LinearModel<> >
typedef LinearModel<> ModelType
 
typedef Model::InputType InputType
 
typedef UnlabeledData< InputTypeDatasetType
 

Public Member Functions

 PCA (bool whitening=false)
 
 PCA (UnlabeledData< RealVector > const &inputs, bool whitening=false)
 
std::string name () const
 From INameable: return the class name.
 
void setWhitening (bool whitening)
 
void train (LinearModel<> &model, UnlabeledData< RealVector > const &inputs)
 
SHARK_EXPORT_SYMBOL void setData (UnlabeledData< RealVector > const &inputs)
 
SHARK_EXPORT_SYMBOL void encoder (LinearModel<> &model, std::size_t m=0)
 
SHARK_EXPORT_SYMBOL void decoder (LinearModel<> &model, std::size_t m=0)
 
RealVector const & eigenvalues () const
 
double eigenvalue (std::size_t i) const
 Returns ith eigenvalue.
 
RealMatrix const & eigenvectors () const
 
RealVector const & mean () const
 mean of last training
 
- Public Member Functions inherited from shark::AbstractUnsupervisedTrainer< LinearModel<> >
virtual void train (ModelType &model, DatasetType const &inputset)=0
 Core of the Trainer interface.
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
- Public Member Functions inherited from shark::ISerializable
virtual ~ISerializable ()
 Virtual d'tor.
 
virtual void read (InArchive &archive)
 Read the component from the supplied archive.
 
virtual void write (OutArchive &archive) const
 Write the component to the supplied archive.
 
void load (InArchive &archive, unsigned int version)
 Versioned loading of components, calls read(...).
 
void save (OutArchive &archive, unsigned int version) const
 Versioned storing of components, calls write(...).
 
 BOOST_SERIALIZATION_SPLIT_MEMBER ()
 

Protected Attributes

bool m_whitening
 normalize variance yes/no
 
RealMatrix m_eigenvectors
 eigenvectors
 
RealVector m_eigenvalues
 eigenvalues
 
RealVector m_mean
 mean value
 
std::size_t m_n
 number of attributes
 
std::size_t m_l
 number of training data points
 
PCAAlgorithm m_algorithm
 whether to use design matrix or its transpose for building covariance matrix
 

Detailed Description

Principal Component Analysis.

The Principal Component Analysis, also known as Karhunen-Loeve transformation, takes a symmetric \( n \times n \) matrix \( A \) and uses its decomposition

\( A = \Gamma \Lambda \Gamma^T, \)

where \( \Lambda \) is the diagonal matrix of eigenvalues of \( A \) and \( \Gamma \) is the orthogonal matrix with the corresponding eigenvectors as columns. \( \Lambda \) then defines a successive orthogonal rotation that maximizes the variances of the coordinates, i.e. the coordinate system is rotated in such a way that the correlation between the new axes becomes zero. If there are \( p \) axes, the first axis is rotated in a way that the points on the new axis have maximum variance. Then the remaining \( p - 1 \) axes are rotated such that a another axis covers a maximum part of the rest variance, that is not covered by the first axis. After the rotation of \( p - 1 \) axes, the rotation destination of axis no. \( p \) is fixed. An application for PCA is the reduction of dimensions by skipping the components with the least corresponding eigenvalues/variances. Furthermore, the eigenvalues may be rescaled to one, resulting in a whitening of the data.

Definition at line 73 of file PCA.h.

Member Enumeration Documentation

◆ PCAAlgorithm

Enumerator
STANDARD 
SMALL_SAMPLE 
AUTO 

Definition at line 78 of file PCA.h.

Constructor & Destructor Documentation

◆ PCA() [1/2]

shark::PCA::PCA ( bool  whitening = false)
inline

Constructor. The parameter defines whether the model should also whiten the data.

Definition at line 83 of file PCA.h.

References AUTO, and m_algorithm.

◆ PCA() [2/2]

shark::PCA::PCA ( UnlabeledData< RealVector > const &  inputs,
bool  whitening = false 
)
inline

Constructor. The parameter defines whether the model should also whiten the data. The eigendecomposition of the data is stored inthe PCA object.

Definition at line 91 of file PCA.h.

References AUTO, m_algorithm, and setData().

Member Function Documentation

◆ decoder()

SHARK_EXPORT_SYMBOL void shark::PCA::decoder ( LinearModel<> &  model,
std::size_t  m = 0 
)

Returns a model mapping encoded data from the m-dimensional PCA coordinate system back to the n-dimensional original coordinate system.

Referenced by main().

◆ eigenvalue()

double shark::PCA::eigenvalue ( std::size_t  i) const
inline

Returns ith eigenvalue.

Definition at line 142 of file PCA.h.

References m_eigenvalues, m_l, and SIZE_CHECK.

Referenced by main().

◆ eigenvalues()

RealVector const & shark::PCA::eigenvalues ( ) const
inline

Eigenvalues of last training. The number of eigenvalues is equal to the minimum of the input dimensions (i.e., number of attributes) and the number of data points used for training the PCA.

Definition at line 138 of file PCA.h.

References m_eigenvalues.

Referenced by main().

◆ eigenvectors()

RealMatrix const & shark::PCA::eigenvectors ( ) const
inline

Eigenvectors of last training. The number of eigenvectors is equal to the minimum of the input dimensions (i.e., number of attributes) and the number of data points used for training the PCA.

Definition at line 153 of file PCA.h.

References m_eigenvectors.

◆ encoder()

SHARK_EXPORT_SYMBOL void shark::PCA::encoder ( LinearModel<> &  model,
std::size_t  m = 0 
)

Returns a model mapping the original data to the m-dimensional PCA coordinate system.

Referenced by main(), and train().

◆ mean()

RealVector const & shark::PCA::mean ( ) const
inline

mean of last training

Definition at line 158 of file PCA.h.

References m_mean.

Referenced by main().

◆ name()

std::string shark::PCA::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 98 of file PCA.h.

◆ setData()

SHARK_EXPORT_SYMBOL void shark::PCA::setData ( UnlabeledData< RealVector > const &  inputs)

Sets the input data and performs the PCA. This is a computationally costly operation. The eigendecomposition of the data is stored inthe PCA object.

Referenced by PCA(), and train().

◆ setWhitening()

void shark::PCA::setWhitening ( bool  whitening)
inline

If set to true, the encoded data has unit variance along the new coordinates.

Definition at line 103 of file PCA.h.

References m_whitening.

Referenced by main().

◆ train()

void shark::PCA::train ( LinearModel<> &  model,
UnlabeledData< RealVector > const &  inputs 
)
inline

Train the model to perform PCA. The model must be a LinearModel object with offset, and its output dimension defines the number of principal components represented. The model returned is the one given by the econder() function (i.e., mapping from the original input space to the PCA coordinate system).

< reduced dimensionality

Definition at line 113 of file PCA.h.

References encoder(), shark::Shape::numElements(), shark::LinearModel< InputType, ActivationFunction >::outputShape(), and setData().

Referenced by main().

Member Data Documentation

◆ m_algorithm

PCAAlgorithm shark::PCA::m_algorithm
protected

whether to use design matrix or its transpose for building covariance matrix

Definition at line 171 of file PCA.h.

Referenced by PCA(), and PCA().

◆ m_eigenvalues

RealVector shark::PCA::m_eigenvalues
protected

eigenvalues

Definition at line 165 of file PCA.h.

Referenced by eigenvalue(), and eigenvalues().

◆ m_eigenvectors

RealMatrix shark::PCA::m_eigenvectors
protected

eigenvectors

Definition at line 164 of file PCA.h.

Referenced by eigenvectors().

◆ m_l

std::size_t shark::PCA::m_l
protected

number of training data points

Definition at line 169 of file PCA.h.

Referenced by eigenvalue().

◆ m_mean

RealVector shark::PCA::m_mean
protected

mean value

Definition at line 166 of file PCA.h.

Referenced by mean().

◆ m_n

std::size_t shark::PCA::m_n
protected

number of attributes

Definition at line 168 of file PCA.h.

◆ m_whitening

bool shark::PCA::m_whitening
protected

normalize variance yes/no

Definition at line 163 of file PCA.h.

Referenced by setWhitening().


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