mlpack  2.0.1
naive_method.hpp
Go to the documentation of this file.
1 
15 #ifndef __MLPACK_METHODS_KERNEL_PCA_NAIVE_METHOD_HPP
16 #define __MLPACK_METHODS_KERNEL_PCA_NAIVE_METHOD_HPP
17 
18 #include <mlpack/core.hpp>
19 
20 namespace mlpack {
21 namespace kpca {
22 
23 template<typename KernelType>
25 {
26  public:
37  static void ApplyKernelMatrix(const arma::mat& data,
38  arma::mat& transformedData,
39  arma::vec& eigval,
40  arma::mat& eigvec,
41  const size_t /* unused */,
42  KernelType kernel = KernelType())
43  {
44  // Construct the kernel matrix.
45  arma::mat kernelMatrix;
46  // Resize the kernel matrix to the right size.
47  kernelMatrix.set_size(data.n_cols, data.n_cols);
48 
49  // Note that we only need to calculate the upper triangular part of the
50  // kernel matrix, since it is symmetric. This helps minimize the number of
51  // kernel evaluations.
52  for (size_t i = 0; i < data.n_cols; ++i)
53  {
54  for (size_t j = i; j < data.n_cols; ++j)
55  {
56  // Evaluate the kernel on these two points.
57  kernelMatrix(i, j) = kernel.Evaluate(data.unsafe_col(i),
58  data.unsafe_col(j));
59  }
60  }
61 
62  // Copy to the lower triangular part of the matrix.
63  for (size_t i = 1; i < data.n_cols; ++i)
64  for (size_t j = 0; j < i; ++j)
65  kernelMatrix(i, j) = kernelMatrix(j, i);
66 
67  // For PCA the data has to be centered, even if the data is centered. But it
68  // is not guaranteed that the data, when mapped to the kernel space, is also
69  // centered. Since we actually never work in the feature space we cannot
70  // center the data. So, we perform a "psuedo-centering" using the kernel
71  // matrix.
72  arma::rowvec rowMean = arma::sum(kernelMatrix, 0) / kernelMatrix.n_cols;
73  kernelMatrix.each_col() -= arma::sum(kernelMatrix, 1) / kernelMatrix.n_cols;
74  kernelMatrix.each_row() -= rowMean;
75  kernelMatrix += arma::sum(rowMean) / kernelMatrix.n_cols;
76 
77  // Eigendecompose the centered kernel matrix.
78  arma::eig_sym(eigval, eigvec, kernelMatrix);
79 
80  // Swap the eigenvalues since they are ordered backwards (we need largest to
81  // smallest).
82  for (size_t i = 0; i < floor(eigval.n_elem / 2.0); ++i)
83  eigval.swap_rows(i, (eigval.n_elem - 1) - i);
84 
85  // Flip the coefficients to produce the same effect.
86  eigvec = arma::fliplr(eigvec);
87 
88  transformedData = eigvec.t() * kernelMatrix;
89  transformedData.each_col() /= arma::sqrt(eigval);
90  }
91 };
92 
93 } // namespace kpca
94 } // namespace mlpack
95 
96 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
static void ApplyKernelMatrix(const arma::mat &data, arma::mat &transformedData, arma::vec &eigval, arma::mat &eigvec, const size_t, KernelType kernel=KernelType())
Construct the exact kernel matrix.
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...