13 #ifndef MLPACK_METHODS_KERNEL_PCA_NAIVE_METHOD_HPP 14 #define MLPACK_METHODS_KERNEL_PCA_NAIVE_METHOD_HPP 21 template<
typename KernelType>
36 arma::mat& transformedData,
40 KernelType kernel = KernelType())
43 arma::mat kernelMatrix;
45 kernelMatrix.set_size(data.n_cols, data.n_cols);
50 for (
size_t i = 0; i < data.n_cols; ++i)
52 for (
size_t j = i; j < data.n_cols; ++j)
55 kernelMatrix(i, j) = kernel.Evaluate(data.unsafe_col(i),
61 for (
size_t i = 1; i < data.n_cols; ++i)
62 for (
size_t j = 0; j < i; ++j)
63 kernelMatrix(i, j) = kernelMatrix(j, i);
70 arma::rowvec rowMean = arma::sum(kernelMatrix, 0) / kernelMatrix.n_cols;
71 kernelMatrix.each_col() -= arma::sum(kernelMatrix, 1) / kernelMatrix.n_cols;
72 kernelMatrix.each_row() -= rowMean;
73 kernelMatrix += arma::sum(rowMean) / kernelMatrix.n_cols;
76 kernelMatrix = arma::symmatu(kernelMatrix);
77 if (!arma::eig_sym(eigval, eigvec, kernelMatrix))
79 Log::Fatal <<
"Failed to construct the kernel matrix." << std::endl;
84 for (
size_t i = 0; i < floor(eigval.n_elem / 2.0); ++i)
85 eigval.swap_rows(i, (eigval.n_elem - 1) - i);
88 eigvec = arma::fliplr(eigvec);
90 transformedData = eigvec.t() * kernelMatrix;
91 transformedData.each_col() /= arma::sqrt(eigval);
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.
The core includes that mlpack expects; standard C++ includes and Armadillo.
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.