mlpack  master
nystroem_method.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_KERNEL_PCA_NYSTROEM_METHOD_HPP
14 #define MLPACK_METHODS_KERNEL_PCA_NYSTROEM_METHOD_HPP
15 
16 #include <mlpack/prereqs.hpp>
19 
20 namespace mlpack {
21 namespace kpca {
22 
23 template<
24  typename KernelType,
25  typename PointSelectionPolicy = kernel::KMeansSelection<>
26 >
28 {
29  public:
40  static void ApplyKernelMatrix(const arma::mat& data,
41  arma::mat& transformedData,
42  arma::vec& eigval,
43  arma::mat& eigvec,
44  const size_t rank,
45  KernelType kernel = KernelType())
46  {
47  arma::mat G, v;
49  rank);
50  nm.Apply(G);
51  transformedData = G.t() * G;
52 
53  // Center the reconstructed approximation.
54  math::Center(transformedData, transformedData);
55 
56  // For PCA the data has to be centered, even if the data is centered. But
57  // it is not guaranteed that the data, when mapped to the kernel space, is
58  // also centered. Since we actually never work in the feature space we
59  // cannot center the data. So, we perform a "psuedo-centering" using the
60  // kernel matrix.
61  arma::colvec colMean = arma::sum(G, 1) / G.n_rows;
62  G.each_row() -= arma::sum(G, 0) / G.n_rows;
63  G.each_col() -= colMean;
64  G += arma::sum(colMean) / G.n_rows;
65 
66  // Eigendecompose the centered kernel matrix.
67  arma::eig_sym(eigval, eigvec, transformedData);
68 
69  // Swap the eigenvalues since they are ordered backwards (we need largest
70  // to smallest).
71  for (size_t i = 0; i < floor(eigval.n_elem / 2.0); ++i)
72  eigval.swap_rows(i, (eigval.n_elem - 1) - i);
73 
74  // Flip the coefficients to produce the same effect.
75  eigvec = arma::fliplr(eigvec);
76 
77  transformedData = eigvec.t() * G.t();
78  }
79 };
80 
81 } // namespace kpca
82 } // namespace mlpack
83 
84 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Apply(arma::mat &output)
Apply the low-rank factorization to obtain an output matrix G such that K&#39; = G * G^T.
void Center(const arma::mat &x, arma::mat &xCentered)
Creates a centered matrix, where centering is done by subtracting the sum over the columns (a column ...
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.