mlpack  1.0.12
nystroem_method.hpp
Go to the documentation of this file.
1 
15 #ifndef __MLPACK_METHODS_KERNEL_PCA_NYSTROEM_METHOD_HPP
16 #define __MLPACK_METHODS_KERNEL_PCA_NYSTROEM_METHOD_HPP
17 
18 #include <mlpack/core.hpp>
21 
22 namespace mlpack {
23 namespace kpca {
24 
25 template<
26  typename KernelType,
27  typename PointSelectionPolicy = kernel::KMeansSelection<>
28 >
30 {
31  public:
42  static void ApplyKernelMatrix(const arma::mat& data,
43  arma::mat& transformedData,
44  arma::vec& eigval,
45  arma::mat& eigvec,
46  const size_t rank,
47  KernelType kernel = KernelType())
48  {
49  arma::mat G, v;
51  rank);
52  nm.Apply(G);
53  transformedData = G.t() * G;
54 
55  // For PCA the data has to be centered, even if the data is centered. But
56  // it is not guaranteed that the data, when mapped to the kernel space, is
57  // also centered. Since we actually never work in the feature space we
58  // cannot center the data. So, we perform a "psuedo-centering" using the
59  // kernel matrix.
60  arma::rowvec rowMean = arma::sum(transformedData, 0) /
61  transformedData.n_cols;
62  transformedData.each_col() -= arma::sum(transformedData, 1) /
63  transformedData.n_cols;
64  transformedData.each_row() -= rowMean;
65  transformedData += arma::sum(rowMean) / transformedData.n_cols;
66 
67  // Eigendecompose the centered kernel matrix.
68  arma::svd(eigvec, eigval, v, transformedData);
69  eigval %= eigval / (data.n_cols - 1);
70 
71  transformedData = eigvec.t() * G.t();
72  }
73 };
74 
75 }; // namespace kpca
76 }; // namespace mlpack
77 
78 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
void Apply(arma::mat &output)
Apply the low-rank factorization to obtain an output matrix G such that K' = G * G^T.
static void ApplyKernelMatrix(const arma::mat &data, arma::mat &transformedData, arma::vec &eigval, arma::mat &eigvec, const size_t rank, KernelType kernel=KernelType())
Construct the kernel matrix approximation using the nystroem method.