Cosan  1.0
Data Analytics Library
Cosan::PrincipalComponentAnalysis< NumericType > Class Template Reference

#include <principalcomponentanalysis.h>

Inheritance diagram for Cosan::PrincipalComponentAnalysis< NumericType >:
Cosan::Preprocessor< NumericType > Cosan::CosanBO

Public Member Functions

 PrincipalComponentAnalysis ()=delete
 
 PrincipalComponentAnalysis (CosanRawData< NumericType > &RD, gsl::index ncom=3)
 
 PrincipalComponentAnalysis (CosanData< NumericType > &RD, gsl::index ncom=3)
 
 PrincipalComponentAnalysis (const CosanMatrix< NumericType > &RD, gsl::index ncom=3)
 
CosanMatrix< NumericType > & GetPC ()
 
- Public Member Functions inherited from Cosan::Preprocessor< NumericType >
 Preprocessor ()
 
virtual ~Preprocessor ()=default
 
virtual void fit (const CosanMatrix< NumericType > &X)
 
virtual CosanMatrix< NumericTypetransform (const CosanMatrix< NumericType > &X)
 
- Public Member Functions inherited from Cosan::CosanBO
 CosanBO ()
 Default constructor. More...
 
virtual const std::string GetName () const
 Get the name of the objects. More...
 

Private Member Functions

void fit (const CosanMatrix< NumericType > &X, gsl::index ncom)
 

Private Attributes

CosanMatrix< NumericTypePrincipalComponent
 
CosanMatrix< NumericTypeFullComponent
 

Detailed Description

template<Numeric NumericType>
class Cosan::PrincipalComponentAnalysis< NumericType >

https://en.wikipedia.org/wiki/Principal_component_analysis

Definition at line 16 of file principalcomponentanalysis.h.

Constructor & Destructor Documentation

◆ PrincipalComponentAnalysis() [1/4]

template<Numeric NumericType>
Cosan::PrincipalComponentAnalysis< NumericType >::PrincipalComponentAnalysis ( )
delete

◆ PrincipalComponentAnalysis() [2/4]

template<Numeric NumericType>
Cosan::PrincipalComponentAnalysis< NumericType >::PrincipalComponentAnalysis ( CosanRawData< NumericType > &  RD,
gsl::index  ncom = 3 
)
inline

Definition at line 19 of file principalcomponentanalysis.h.

19  :Preprocessor<NumericType>(){
20  ncom = std::min(ncom,RD.GetcolsX());
21  this->fit(RD.GetInput(),ncom);
22  }

◆ PrincipalComponentAnalysis() [3/4]

template<Numeric NumericType>
Cosan::PrincipalComponentAnalysis< NumericType >::PrincipalComponentAnalysis ( CosanData< NumericType > &  RD,
gsl::index  ncom = 3 
)
inline

Definition at line 23 of file principalcomponentanalysis.h.

23  :Preprocessor<NumericType>(){
24  ncom = std::min(ncom,RD.GetcolsX());
25  this->fit(RD.GetInput(),ncom);
26  }

◆ PrincipalComponentAnalysis() [4/4]

template<Numeric NumericType>
Cosan::PrincipalComponentAnalysis< NumericType >::PrincipalComponentAnalysis ( const CosanMatrix< NumericType > &  RD,
gsl::index  ncom = 3 
)
inline

Definition at line 28 of file principalcomponentanalysis.h.

28  :Preprocessor<NumericType>(){
29  ncom = std::min(ncom,RD.cols());
30  this->fit(RD,ncom);
31  }

Member Function Documentation

◆ fit()

template<Numeric NumericType>
void Cosan::PrincipalComponentAnalysis< NumericType >::fit ( const CosanMatrix< NumericType > &  X,
gsl::index  ncom 
)
inlineprivate

Definition at line 37 of file principalcomponentanalysis.h.

37  {
38  fmt::print("*********************************\n");
39  fmt::print("Begin PCA on Input Data X. Select the first {:} principal components\n",ncom);
40  CosanMatrix<NumericType> centered = X.rowwise() - X.colwise().mean();
41  CosanMatrix<NumericType> cov = centered.adjoint() * centered;
42 
43  Eigen::SelfAdjointEigenSolver<CosanMatrix<NumericType>> eigensolver(cov);
44  if (eigensolver.info() != Eigen::Success) {
45  throw std::invalid_argument("Cannot solve eigenvalue decomposition.");}
46 
47  std::vector<NumericType> vec(eigensolver.eigenvalues().data(), eigensolver.eigenvalues().data() + eigensolver.eigenvalues().size());
48  std::reverse(vec.begin(),vec.end());
49  std::vector<NumericType> runningSum(vec.size());
50  std::partial_sum(vec.begin(), vec.end(), runningSum.begin());
51  for (gsl::index i =0;i<runningSum.size();i++){
52  fmt::print("The first {:} principal components explains {:f}% of the total variance.\n",i,runningSum[i]/runningSum.back()*100);
53  if (runningSum[i]/runningSum.back()>0.98 and i>=8){
54  break;
55  }
56  }
57 // std::cout<<eigensolver.eigenvalues()<<std::endl;
58 // std::cout<<eigensolver.eigenvectors()<<std::endl;
59 // std::cout<<eigensolver.eigenvectors().rightCols(1)<<std::endl;
60  fmt::print("Finsh PCA on Input Data X. The first {:} principal components explains {:f}% of the total variance.\n",ncom,runningSum[ncom]/runningSum.back()*100);
61  fmt::print("Uer .GetPC() function to get the principal components.\n" );
62  fmt::print("*********************************\n");
63  PrincipalComponent = eigensolver.eigenvectors().rightCols(ncom);
64  }

◆ GetPC()

template<Numeric NumericType>
CosanMatrix<NumericType>& Cosan::PrincipalComponentAnalysis< NumericType >::GetPC ( )
inline

Definition at line 34 of file principalcomponentanalysis.h.

34 {return PrincipalComponent;}

Member Data Documentation

◆ FullComponent

template<Numeric NumericType>
CosanMatrix<NumericType> Cosan::PrincipalComponentAnalysis< NumericType >::FullComponent
private

Definition at line 36 of file principalcomponentanalysis.h.

◆ PrincipalComponent

template<Numeric NumericType>
CosanMatrix<NumericType> Cosan::PrincipalComponentAnalysis< NumericType >::PrincipalComponent
private

Definition at line 36 of file principalcomponentanalysis.h.


The documentation for this class was generated from the following file:
Cosan::PrincipalComponentAnalysis::PrincipalComponent
CosanMatrix< NumericType > PrincipalComponent
Definition: principalcomponentanalysis.h:36
Cosan::vec
std::vector< NumericType > vec
Definition: templateTest.cpp:14
Cosan::PrincipalComponentAnalysis::fit
void fit(const CosanMatrix< NumericType > &X, gsl::index ncom)
Definition: principalcomponentanalysis.h:37