tag:blogger.com,1999:blog-6314876008291942531.post6418108845034775552..comments2024-01-14T00:36:43.430-08:00Comments on Antonio Gulli's coding playground: Kernel PCAUnknownnoreply@blogger.comBlogger2125tag:blogger.com,1999:blog-6314876008291942531.post-21665533680724902082011-03-20T11:00:39.880-07:002011-03-20T11:00:39.880-07:00Thanks a lot for the code! Indeed it's a bit b...Thanks a lot for the code! Indeed it's a bit buggy, but it's still a very nice starting point. Below is a little fixup that should make it work with any dimensionality.<br /><br />A quick question: You say "in this toy example we materialize the kernel, which is not necessary". What do you mean exactly? how could we not do that?<br /><br />ciao<br />Basilio<br /><br /><br /><br /><br />void PCA::kernel_pca(MatrixXd & dataPoints, unsigned int dimSpace)<br />{<br /> int m = dataPoints.rows();<br /> int n = dataPoints.cols();<br /><br /> Kernel k = new Kernel(dataPoints);<br /><br /> // ''centralize''<br /> MatrixXd K_Centralized = k->get()<br /> - MatrixXd::Ones(n, n)*k->get()<br /> - k->get()*MatrixXd::Ones(n, n)<br /> + MatrixXd::Ones(n, n)*k->get()*MatrixXd::Ones(n, n);<br /> std::cout << "Centralized" << std::endl;<br /><br /> // compute the eigenvalue on the K_Centralized Matrix<br /> EigenSolver m_solve(K_Centralized);<br /> std::cout << "got the eigenvalues, eigenvectors" << std::endl;<br /> VectorXd eigenvalues = m_solve.eigenvalues().real();<br /> MatrixXd eigenVectors = m_solve.eigenvectors().real(); <br /><br /> // sort and get the permutation indices<br /> PermutationIndices pi;<br /> for (int i = 0 ; i < n; i++)<br /> pi.push_back(std::make_pair(-eigenvalues(i), i));<br /><br /> std::sort(pi.begin(), pi.end());<br /><br /> // get top eigenvectors<br /> _result = MatrixXd::Zero(n, dimSpace);<br /> for (unsigned int i = 0; i < dimSpace; i++)<br /> {<br /> _result.col(i) = eigenVectors.col(pi[i].second); // permutation indices<br /> }<br /><br /> MatrixXd sqrtE = MatrixXd::Zero(dimSpace, dimSpace);<br /> for (unsigned int i = 0; i < dimSpace; i++)<br /> {<br /> sqrtE(i, i) = sqrt(-pi[i].first);<br /> }<br /><br /> // get the final data projection<br /> _result = (sqrtE * _result.transpose()).transpose();<br /> delete k;<br />}b4siliohttps://www.blogger.com/profile/01480122986559653098noreply@blogger.comtag:blogger.com,1999:blog-6314876008291942531.post-62789154191345633372010-07-25T16:31:15.493-07:002010-07-25T16:31:15.493-07:00Works only for a Square matrix as the dataset :(Works only for a Square matrix as the dataset :(Zenhttps://www.blogger.com/profile/01419150569581900116noreply@blogger.com