Skip to content

Instantly share code, notes, and snippets.

@tomrockdsouza
Last active June 2, 2017 09:21
Show Gist options
  • Save tomrockdsouza/5f590713a1aa34cdd48e8889df27ad19 to your computer and use it in GitHub Desktop.
Save tomrockdsouza/5f590713a1aa34cdd48e8889df27ad19 to your computer and use it in GitHub Desktop.
Java Implementation of Kmeans Algorithm on Linear Dataset ( Based on Random Input )
/** The class encapsulates an implementation of the Kmeans algorithm for Linear Dataset
*
* First argument is for Number of Elements
* Second argument is for Start of Randomizer
* Third argument is for End of Randomizer
* Fourth argument is for Number of Clusters
*
* Usage with the command line :
* >java Kmeans 7 10 20 3
* This Means 7 tupples of data will be created
* With each tupple having randomized values between 10 to 40
* And 3 clusters will be formed.
*
* As per Kmeans Algorithm when the datasets from previous iteration belong to the same clusters in current iteration
* The Algorithm stops and clusters are finalized
*
* @author Tomrock D'souza, St. Francis Institute Of Technology, University of Mumbai, 2017
* @copyright GNU General Public License v3
* No reproduction in whole or part without maintaining this copyright notice
* and imposing this condition on any subsequent users.
*/
import java.util.*;
import java.lang.Math;
class Kmeans{
public static void main(String args[]){
int number,numSize,numStart,numEnd,x,y=0,cSize,l,z,i,j;
Double temp,tempa;
//Processing Arguments
numSize=Integer.parseInt(args[0]);
numStart=Integer.parseInt(args[1]);
numEnd=Integer.parseInt(args[2]);
cSize=Integer.parseInt(args[3]);
x=numEnd-numStart+1;
Integer[] rand=new Integer[x];
for(z=numStart;z<=numEnd;z++)
{
rand[y]=z;
y++;
}
//Filling Array With Random Variables
Collections.shuffle(Arrays.asList(rand));
Integer[] array = Arrays.copyOfRange(rand, 0, numSize+1);
Integer[] clustvalue=new Integer[numSize];
Integer[] clusttemp=new Integer[numSize];
Double[] clust=new Double[cSize];
y=numSize/cSize;
z=0;x=0;
//Presigning Clusters And Getting Numerical Ready For First Iteration
for(i=0;i<cSize-1;i++)
{l=0;
for(j=0;j<y;j++){
clustvalue[z]=i;
z++;
}
}
for(;z<numSize;z++)
{
clustvalue[z]=cSize-1;
}
System.out.println("Array\tAssigned-cluster");
for(i=0;i<numSize;i++){
System.out.println(array[i]+"\t"+clustvalue[i]);
}
//At this stage The first assignment of clusters to the data sets is done/
while(true){
for(i=0;i<cSize;i++){
l=0;
for(j=0;j<numSize;j++){
if(clustvalue[j]==i){
l+=array[j];
x++;
}
}
clust[i]=(double)l/x;
x=0;
}
for(i=0;i<numSize;i++)
{ temp=9999.9;
for (j=0;j<cSize;j++){
tempa=Math.abs(array[i]-clust[j]);
if(temp>tempa){
clusttemp[i]=j;
temp=tempa;
}
}
}
for(i=0;i<cSize;i++){
System.out.println("Current Cluster Value="+clust[i]);
}
System.out.println("\n\nArray\t\tPre-clus\tCurr-Clus");
for(i=0;i<numSize;i++){
System.out.println(array[i]+"\t\t"+clustvalue[i]+"\t\t"+clusttemp[i]);
}
//Condition that matches current and previous data set
if(!Arrays.equals(clusttemp,clustvalue)){
clustvalue=clusttemp.clone();
}
else {
for(i=0;i<cSize;i++){
l=0;
for(j=0;j<numSize;j++){
if(clustvalue[j]==i){
l+=array[j];
x++;
}
}
clust[i]=(double)l/x;
x=0;
}
System.out.println("\nFinal Cluster Values:");
for(i=0;i<cSize;i++){
System.out.println("Cluster Value "+i+"="+clust[i]);
}
break;}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment