pthimon / clustering Goto Github PK
View Code? Open in Web Editor NEWLibrary for performing spectral clustering in C++
License: Other
Library for performing spectral clustering in C++
License: Other
The demo code won't compile, several minor mistakes need to be fixed. Thanks!
Hi,
there is a little mistake in your implementation, for the spectral clustering, the laplacian has to be calculated from the affinity matrix and then the eigenvalue decomposition follows. Should be:
SpectralClustering::SpectralClustering(Eigen::MatrixXd& data, int numDims):
mNumDims(numDims),
mNumClusters(0)
{
//TODO normalise affinity matrix?
// NO: we need Eigenvectors of Laplacian, not the Affinity Matrix !!!!
Eigen::MatrixXd Deg = Eigen::MatrixXd::Zero(data.rows(),data.cols());
for ( int i=0; i < data.cols(); i++) {
Deg(i,i)=1/(sqrt((data.row(i).sum())) );
}
Eigen::MatrixXd Lapla = Deg * data * Deg;
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> s(Lapla, true);
// Change ends here
Eigen::VectorXd val = s.eigenvalues();
Eigen::MatrixXd vec = s.eigenvectors();
//sort eigenvalues/vectors
int n = data.cols();
for (int i = 0; i < n - 1; ++i) {
int k;
val.segment(i, n - i).maxCoeff(&k);
if (k > 0) {
std::swap(val[i], val[k + i]);
vec.col(i).swap(vec.col(k + i));
}
}
//choose the number of eigenvectors to consider
if (mNumDims < vec.cols()) {
mEigenVectors = vec.block(0,0,vec.rows(),mNumDims);
} else {
mEigenVectors = vec;
}
}
This algorithm is not the classic spectral clustering. You may need to change the name in case of confusions.
Hi pthimon,
Thanks for your example of STSC and your code.
Could you please offer another example of 2D point clustering such as example shown in STSC ?
Best regards,
Che-Cheng
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.