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 LinearModel<> ::InputType InputType
 

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. More...
 
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. More...
 
RealMatrix const & eigenvectors () const
 
RealVector const & mean () const
 mean of last training More...
 
- Public Member Functions inherited from shark::AbstractUnsupervisedTrainer< LinearModel<> >
virtual void train (ModelType &model, const UnlabeledData< InputType > &inputset)=0
 Core of the Trainer interface. More...
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
- Public Member Functions inherited from shark::ISerializable
virtual ~ISerializable ()
 Virtual d'tor. More...
 
virtual void read (InArchive &archive)
 Read the component from the supplied archive. More...
 
virtual void write (OutArchive &archive) const
 Write the component to the supplied archive. More...
 
void load (InArchive &archive, unsigned int version)
 Versioned loading of components, calls read(...). More...
 
void save (OutArchive &archive, unsigned int version) const
 Versioned storing of components, calls write(...). More...
 
 BOOST_SERIALIZATION_SPLIT_MEMBER ()
 

Protected Attributes

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

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 74 of file PCA.h.

Member Enumeration Documentation

◆ PCAAlgorithm

Enumerator
STANDARD 
SMALL_SAMPLE 
AUTO 

Definition at line 79 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 84 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 92 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(), and train().

◆ eigenvalue()

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

Returns ith eigenvalue.

Definition at line 143 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 139 of file PCA.h.

References m_eigenvalues.

◆ 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 154 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 159 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 99 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 104 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 114 of file PCA.h.

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

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 172 of file PCA.h.

Referenced by PCA().

◆ m_eigenvalues

RealVector shark::PCA::m_eigenvalues
protected

eigenvalues

Definition at line 166 of file PCA.h.

Referenced by eigenvalue(), and eigenvalues().

◆ m_eigenvectors

RealMatrix shark::PCA::m_eigenvectors
protected

eigenvectors

Definition at line 165 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 170 of file PCA.h.

Referenced by eigenvalue().

◆ m_mean

RealVector shark::PCA::m_mean
protected

mean value

Definition at line 167 of file PCA.h.

Referenced by mean().

◆ m_n

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

number of attributes

Definition at line 169 of file PCA.h.

◆ m_whitening

bool shark::PCA::m_whitening
protected

normalize variance yes/no

Definition at line 164 of file PCA.h.

Referenced by setWhitening().


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