mlpack  2.0.1
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  // Center the reconstructed approximation.
56  math::Center(transformedData, transformedData);
57 
58  // For PCA the data has to be centered, even if the data is centered. But
59  // it is not guaranteed that the data, when mapped to the kernel space, is
60  // also centered. Since we actually never work in the feature space we
61  // cannot center the data. So, we perform a "psuedo-centering" using the
62  // kernel matrix.
63  arma::colvec colMean = arma::sum(G, 1) / G.n_rows;
64  G.each_row() -= arma::sum(G, 0) / G.n_rows;
65  G.each_col() -= colMean;
66  G += arma::sum(colMean) / G.n_rows;
67 
68  // Eigendecompose the centered kernel matrix.
69  arma::eig_sym(eigval, eigvec, transformedData);
70 
71  // Swap the eigenvalues since they are ordered backwards (we need largest
72  // to smallest).
73  for (size_t i = 0; i < floor(eigval.n_elem / 2.0); ++i)
74  eigval.swap_rows(i, (eigval.n_elem - 1) - i);
75 
76  // Flip the coefficients to produce the same effect.
77  eigvec = arma::fliplr(eigvec);
78 
79  transformedData = eigvec.t() * G.t();
80  }
81 };
82 
83 } // namespace kpca
84 } // namespace mlpack
85 
86 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
void Apply(arma::mat &output)
Apply the low-rank factorization to obtain an output matrix G such that K' = G * G^T.
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
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.