# K-Mean Clustering C++ source code

This topic is 3569 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Where is the K-Mean Clustering C++ source code? I had found a web talks about K-Mean Clustering, but it have no c++ source code. http://people.revoledu.com/kardi/tutorial/kMean/Resources.htm Do somebody know where is the C++ source code of K-mean Clustering?

##### Share on other sites
The k-means algorithm is pretty straight forward. Here is the pseudo-code for it (just coded off the top of my head) ... the C++ version should be fairly easy to derive from this...it just really depends on the size of your vectors, the similarity function, what your features are, et cetera.

Let n be the number of clusters you wantLet S be the set of feature vectors (|S| is the size of the set)Let A be the set of associated clusters for each feature vectorLet sim(x,y) be the similarity functionLet c[n] be the vectors for our clustersInit:   Let S' = S   //choose n random vectors to start our clusters   for i=1 to n      j = rand(|S'|)      c[n] = S'[j]      S' = S' - {c[n]} //remove that vector from S' so we can't choose it again   end   //assign initial clusters   for i=1 to |S|      A = argmax(j = 1 to n) { sim(S, c[j]) }   endRun:   Let change = true   while change     change = false //assume there is no change     //reassign feature vectors to clusters     for i = 1 to |S|        a = argmax(j = 1 to n) { sim(S, c[j]) }        if a != A           A = a           change = true //a vector changed affiliations -- so we need to                          //recompute our cluster vectors and run again        end     end     //recalculate cluster locations if a change occurred     if change        for i = 1 to n           mean, count = 0            for j = 1 to |S|              if A[j] == i                 mean = mean + S[j]                 count = count + 1              end           end           c = mean/count         end     end

That should be straight forward enough for you to code up yourself.

[Edited by - visage on May 14, 2008 4:01:25 PM]

##### Share on other sites
You may also want to give a look to Weka. It is written with java, and I don't know how easy is it to read, but you may find it interesting...

##### Share on other sites
Quote:
 Original post by cignox1You may also want to give a look to Weka. It is written with java, and I don't know how easy is it to read, but you may find it interesting...

Sorry!My computer has some problems about Security, so I cannot see the source code.
Could you post the java source code?

##### Share on other sites
Dunno if this will help, but if you go to this link:

http://people.revoledu.com/kardi/tutorial/kMean/index.html

and then click on the little voronoi region looking image it will get you some VB source code.

##### Share on other sites
Quote:
Original post by akira32
Quote:
 Original post by cignox1You may also want to give a look to Weka. It is written with java, and I don't know how easy is it to read, but you may find it interesting...

Sorry!My computer has some problems about Security, so I cannot see the source code.
Could you post the java source code?

Weka is a complex data mining system, it is not made by a few lines of code that I can post here. You really need to download it from the site I linked...