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  transformedData = arma::symmatu(transformedData);
68  if (!arma::eig_sym(eigval, eigvec, transformedData))
69  {
70  Log::Fatal << "Failed to construct the kernel matrix." << std::endl;
71  }
72 
73  // Swap the eigenvalues since they are ordered backwards (we need largest
74  // to smallest).
75  for (size_t i = 0; i < floor(eigval.n_elem / 2.0); ++i)
76  eigval.swap_rows(i, (eigval.n_elem - 1) - i);
77 
78  // Flip the coefficients to produce the same effect.
79  eigvec = arma::fliplr(eigvec);
80 
81  transformedData = eigvec.t() * G.t();
82  }
83 };
84 
85 } // namespace kpca
86 } // namespace mlpack
87 
88 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes, Armadillo, cereal, and a few basic mlpa...
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.
constexpr auto data(Container const &container) noexcept -> decltype(container.data())
Definition: iterator.hpp:79
void Apply(arma::mat &output)
Apply the low-rank factorization to obtain an output matrix G such that K&#39; = G * G^T.
static util::PrefixedOutStream Fatal
Definition: log.hpp:105
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 ...