kernel_pca.h
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00036 #ifndef KERNEL_PCA_H_
00037 #define KERNEL_PCA_H_
00038 
00039 #include <string>
00040 #include <map>
00041 #include <stdio.h>
00042 #include <errno.h>
00043 #include <unistd.h>
00044 #include "fastlib/fastlib.h"
00045 #include "fastlib/la/matrix.h"
00046 #include "fastlib/sparse/sparse_matrix.h"
00047 #include "mlpack/allknn/allknn.h"
00048 
00049 class KernelPCATest;
00110 class KernelPCA {
00111  public:
00112   friend class KernelPCATest;
00118   class GaussianKernel {
00119    public:
00120     void set(double bandwidth) {
00121       bandwidth_ = bandwidth;
00122     }
00123     double operator()(double distance) {
00124       return exp(-distance/bandwidth_);
00125     }
00126    private:
00127     double bandwidth_;
00128   };  
00129   
00130   ~KernelPCA() {
00131     Destruct();
00132   }
00141   void Init(std::string data_file, index_t knns, 
00142       index_t leaf_size);
00143   void Destruct();
00148   void ComputeNeighborhoods();
00152   void LoadAffinityMatrix();
00157   void EstimateBandwidth(double *bandwidth);
00161   static void SaveToTextFile(std::string file, 
00162                              Matrix &eigen_vectors,
00163                              Vector &eigen_values);
00164   static void SaveToBinaryFile(std::string file, 
00165                              Matrix &eigen_vectors,
00166                              Vector &eigen_values);
00173   template<typename DISTANCEKERNEL>    
00174   void ComputeGeneralKernelPCA(DISTANCEKERNEL kernel,
00175                                index_t num_of_eigenvalues,
00176                                Matrix *eigen_vectors,
00177                                Vector *eigen_values);
00181   void ComputeIsomap(index_t num_of_eigenvalues);
00186   void ComputeLLE(index_t num_of_eigenvalues,
00187                   Matrix *eigen_vectors,
00188                   Vector *eigen_values);
00192   template<typename DISTANCEKERNEL>
00193   void ComputeDiffusionMaps(DISTANCEKERNEL kernel, index_t num_of_eigenvalues);
00197   void ComputeLaplacialnEigenmaps(index_t);
00203   template<typename DISTANCEKERNEL>
00204   void ComputeSpectralRegression(DISTANCEKERNEL kernel,
00205                                  std::map<index_t, index_t> &data_label,
00206                                  Matrix *embedded_coordinates, 
00207                                  Vector *eigenvalues);
00208     
00209  private:
00210   AllkNN allknn_;
00211   index_t knns_;
00212   Matrix data_;
00213   SparseMatrix kernel_matrix_;  
00214   SparseMatrix affinity_matrix_;
00215   index_t dimension_;
00216 };
00217 
00218 #include "kernel_pca_impl.h"
00219 #endif