Implementation of Single Go Algorithm for Clustering – BE(IT) CLP-II Sensible
Purpose : To implement Single Go Algorithm for Clustering in Paperwork and Information .
Goal : To review Clustering in information or Paperwork utilizing single cross algorithm
Given beneath is the Single Go Algorithm for Clustering with source code in Java Language . For this code to work you must have three information for pattern enter (Textual content Information ) .
Supply Code for Single Go Algorithm
1 |
bundle com.prac.prac; <br /> import java.io.BufferedReader; <br /> import java.io.IOException; <br /> import java.io.InputStreamReader; <br /> import java.util.ArrayList; <br /> public class singlepass { <br /> public static void important(String[] args) throws IOException{ <br /> BufferedReader stdInpt = new BufferedReader(new InputStreamReader(System.in)); <br /> System.out.println("Enter the no of Tokens"); <br /> int noOfDocuments=Integer.parseInt(stdInpt.readLine()); <br /> System.out.println("Enter the no of Paperwork"); <br /> int noOfTokens=Integer.parseInt(stdInpt.readLine()); <br /> System.out.println("Enter the threshhold"); <br /> float threshhold=Float.parseFloat(stdInpt.readLine()); <br /> System.out.println("Enter the Doc Token Matrix"); <br /> int [][]enter= new int [noOfDocuments][noOfTokens]; <br /> for(int i=0;i<noOfDocuments;++i) <br /> { <br /> for(int j=0;j<noOfTokens;++j) <br /> { <br /> System.out.println("Enter("+i+","+j+")"); <br /> enter[i][j]=Integer.parseInt(stdInpt.readLine()); <br /> } <br /> } <br /> SinglePassAlgorithm(noOfDocuments, noOfTokens, threshhold, enter); <br /> } <br /> personal static void SinglePassAlgorithm(int noOfDocuments,int noOfTokens,float threshhold,int [][]enter) <br /> { <br /> int [][] cluster = new int [noOfDocuments][noOfDocuments+1]; <br /> ArrayList<float[]> clusterRepresentative = new ArrayList<float[]>(); <br /> cluster [0][0]=1; <br /> cluster [0][1]=0; <br /> int noOfClusters=1; <br /> Float []temp= new Float[noOfTokens]; <br /> temp=convertintArrToFloatArr(enter[0]); <br /> clusterRepresentative.add(temp); <br /> for(int i=1;i<noOfDocuments;++i) <br /> { <br /> float max=-1; <br /> int clusterId=-1; <br /> for(int j=0;j<noOfClusters;++j) <br /> { <br /> float similarity=calculateSimilarity(convertintArrToFloatArr(enter[i]),clusterRepresentative.get(j) ); <br /> if(similarity>threshhold) <br /> { <br /> if(similarity>max) <br /> { <br /> max=similarity; <br /> clusterId=j; <br /> } <br /> } <br /> } <br /> if(max==-1) <br /> { <br /> cluster[noOfClusters][0]=1; <br /> cluster[noOfClusters][1]=i; <br /> noOfClusters++; <br /> clusterRepresentative.add(convertintArrToFloatArr(enter[i])); <br /> } <br /> else <br /> { <br /> cluster[clusterId][0]+=1; <br /> int index=cluster[clusterId][0]; <br /> cluster[clusterId][index]=i; <br /> clusterRepresentative.set(clusterId,calculateClusterRepresentative(cluster[clusterId],enter, noOfTokens)); <br /> } <br /> } <br /> for(int i=0;i<noOfClusters;++i) <br /> { <br /> System.out.print("n"+i+"t"); <br /> for(int j=1;j<=cluster[i][0];++j) <br /> { <br /> System.out.print(" "+cluster[i][j]); <br /> } <br /> } <br /> } <br /> personal static Float[] convertintArrToFloatArr(int[] enter) <br /> { <br /> int measurement=enter.size; <br /> Float[] reply = new Float[size]; <br /> for(int i=0;i<enter.size;++i) <br /> { <br /> reply[i]=(float)enter[i]; <br /> } <br /> return reply; <br /> } <br /> personal static float calculateSimilarity(Float[] a,Float[] b) <br /> { <br /> float reply=0; <br /> for(int i=0;i<a.size;++i) <br /> { <br /> reply+=a[i]*b[i]; <br /> } <br /> return reply; <br /> } <br /> personal static Float[] calculateClusterRepresentative(int[] cluster,int [][] enter,int noOFTokens) <br /> { <br /> Float[] reply= new Float[noOFTokens]; <br /> for(int i=0;i<noOFTokens;++i) <br /> { <br /> reply[i]=Float.parseFloat("0"); <br /> } <br /> for(int i=1;i<=cluster[0];++i) <br /> { <br /> for(int j=0;j<noOFTokens;++j) <br /> { <br /> reply[j]+=enter[cluster[i]][j]; <br /> } <br /> } <br /> for(int i=0;i<noOFTokens;++i) <br /> { <br /> reply[i]/=cluster[0]; <br /> } <br /> return reply; <br /> } <br /> } <br /> |
Output of Single Go Algorithm
Enter the no of Tokens
5
Enter the no of Paperwork
5
Enter the threshhold
10
Enter the Doc Token Matrix
Enter(0,0)
1
Enter(0,1)
3
Enter(0,2)
3
Enter(0,3)
2
Enter(0,4)
2
Enter(1,0)
2
Enter(1,1)
1
Enter(1,2)
0
Enter(1,3)
1
Enter(1,4)
2
Enter(2,0)
0
Enter(2,1)
2
Enter(2,2)
0
Enter(2,3)
0
Enter(2,4)
1
Enter(3,0)
0
Enter(3,1)
3
1
Enter(3,3)
0
Enter(3,4)
5
Enter(4,0)
1
Enter(4,1)
0
Enter(4,2)
1
Enter(4,3)
0
Enter(4,4)
1
0 0 1 3
1 2
2 4
Supply projectgeek.com