Sign in to follow this  
akira32

K-Mean Clustering C++ source code

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 this post


Link to post
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 want
Let 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 vector
Let sim(x,y) be the similarity function
Let c[n] be the vectors for our clusters

Init:
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[i] = argmax(j = 1 to n) { sim(S[i], c[j]) }
end

Run:
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[i], c[j]) }
if a != A[i]
A[i] = 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[i] = 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by cignox1
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...


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by akira32
Quote:
Original post by cignox1
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...


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...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this