Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.lang.*;
  3. class Kmean
  4. {
  5. public static void main(String args[])
  6. {
  7. int N=9;
  8. int arr[]={2,4,10,12,3,20,30,11,25}; // initial data
  9. int i,m1,m2,a,b,n=0;
  10. boolean flag=true;
  11. float sum1=0,sum2=0;
  12. a=arr[0];b=arr[1];
  13. m1=a; m2=b;
  14. int cluster1[]=new int[9],cluster2[]=new int[9];
  15. for(i=0;i<9;i++)
  16. System.out.print(arr[i]+ "t");
  17. System.out.println();
  18.  
  19. do
  20. {
  21. n++;
  22. int k=0,j=0;
  23. for(i=0;i<9;i++)
  24. {
  25. if(Math.abs(arr[i]-m1)<=Math.abs(arr[i]-m2))
  26. { cluster1[k]=arr[i];
  27. k++;
  28. }
  29. else
  30. { cluster2[j]=arr[i];
  31. j++;
  32. }
  33. }
  34. System.out.println();
  35. for(i=0;i<9;i++)
  36. sum1=sum1+cluster1[i];
  37. for(i=0;i<9;i++)
  38. sum2=sum1+cluster2[i];
  39. a=m1;
  40. b=m2;
  41. m1=Math.round(sum1/k);
  42. m2=Math.round(sum2/j);
  43. if(m1==a && m2==b)
  44. flag=false;
  45. else
  46. flag=true;
  47.  
  48. System.out.println("After iteration "+ n +" , cluster 1 :n"); //printing the clusters of each iteration
  49. for(i=0;i<9;i++)
  50. System.out.print(cluster1[i]+ "t");
  51.  
  52. System.out.println("n");
  53. System.out.println("After iteration "+ n +" , cluster 2 :n");
  54. for(i=0;i<9;i++)
  55. System.out.print(cluster2[i]+ "t");
  56.  
  57. }while(flag);
  58.  
  59. System.out.println("Final cluster 1 :n"); // final clusters
  60. for(i=0;i<9;i++)
  61. System.out.print(cluster1[i]+ "t");
  62.  
  63. System.out.println();
  64. System.out.println("Final cluster 2 :n");
  65. for(i=0;i<9;i++)
  66. System.out.print(cluster2[i]+ "t");
  67. }
  68. }
  69.  
  70. Final cluster 1 :
  71. 2 4 10 12 3 11 0 0 0
  72.  
  73. Final cluster 2 :
  74. 20 30 25 0 0 0 0 0 0
  75.  
  76. if(m1==a && m2==b)
  77. flag=false;
  78. else
  79. flag=true;
  80.  
  81. public class KMeansClustering {
  82.  
  83. public static void main(String args[]) {
  84. int arr[] = {2, 4, 10, 12, 3, 20, 30, 11, 25}; // initial data
  85. int i, m1, m2, a, b, n = 0;
  86. boolean flag;
  87. float sum1, sum2;
  88. a = arr[0];
  89. b = arr[1];
  90. m1 = a;
  91. m2 = b;
  92. int cluster1[] = new int[arr.length], cluster2[] = new int[arr.length];
  93. do {
  94. sum1 = 0;
  95. sum2 = 0;
  96. cluster1 = new int[arr.length];
  97. cluster2 = new int[arr.length];
  98. n++;
  99. int k = 0, j = 0;
  100. for (i = 0; i < arr.length; i++) {
  101. if (Math.abs(arr[i] - m1) <= Math.abs(arr[i] - m2)) {
  102. cluster1[k] = arr[i];
  103. k++;
  104. } else {
  105. cluster2[j] = arr[i];
  106. j++;
  107. }
  108. }
  109. System.out.println();
  110. for (i = 0; i < k; i++) {
  111. sum1 = sum1 + cluster1[i];
  112. }
  113. for (i = 0; i < j; i++) {
  114. sum2 = sum2 + cluster2[i];
  115. }
  116. //printing Centroids/Means
  117. System.out.println("m1=" + m1 + " m2=" + m2);
  118. a = m1;
  119. b = m2;
  120. m1 = Math.round(sum1 / k);
  121. m2 = Math.round(sum2 / j);
  122. flag = !(m1 == a && m2 == b);
  123.  
  124. System.out.println("After iteration " + n + " , cluster 1 :n"); //printing the clusters of each iteration
  125. for (i = 0; i < cluster1.length; i++) {
  126. System.out.print(cluster1[i] + "t");
  127. }
  128.  
  129. System.out.println("n");
  130. System.out.println("After iteration " + n + " , cluster 2 :n");
  131. for (i = 0; i < cluster2.length; i++) {
  132. System.out.print(cluster2[i] + "t");
  133. }
  134.  
  135. } while (flag);
  136.  
  137. System.out.println("Final cluster 1 :n"); // final clusters
  138. for (i = 0; i < cluster1.length; i++) {
  139. System.out.print(cluster1[i] + "t");
  140. }
  141.  
  142. System.out.println();
  143. System.out.println("Final cluster 2 :n");
  144. for (i = 0; i < cluster2.length; i++) {
  145. System.out.print(cluster2[i] + "t");
  146. }
  147. }
  148.  
  149. package k;
  150.  
  151. /**
  152. *
  153. * @author p2csc13002
  154. */
  155.  
  156. import java.io.FileNotFoundException;
  157. import java.io.File;
  158. import java.util.Scanner;
  159. public class K {
  160.  
  161.  
  162. /**
  163. * @param args the command line arguments
  164. */
  165. //GLOBAL VARIABLES
  166. //data_set[][] -------------datast is stored in the data_set[][] array
  167. //initial_centroid[][]------according to k'th value we select initaly k centroid.stored in the initial_centroid[][]
  168. // value is assigned in the 'first_itration()' function
  169. private static double[][] arr;
  170. static int num = 0;
  171. static Double data_set[][]=new Double[20000][100];
  172. static Double diff[][]=new Double[20000][100];
  173. static Double intial_centroid[][]=new Double[300][400];
  174. static Double center_mean[][]=new Double[20000][100];
  175. static Double total_mean[]=new Double[200000];
  176. static int cnum;
  177. static int it=1;
  178. static int checker=1;
  179. static int row=4;//rows in Your DataSet here i use iris dataset
  180. /////////////////////////////////reading the file/////////////////////////////////////
  181. // discriptin readFile readthe txt file
  182. private static void readFile() throws FileNotFoundException
  183. {
  184. Scanner scanner = new Scanner(new File("E:/aa.txt"));
  185. scanner.useDelimiter(System.getProperty("line.separator"));
  186. int lineNo = 0;
  187. while (scanner.hasNext())
  188. {
  189. parseLine(scanner.next(),lineNo);
  190. lineNo++;
  191. System.out.println();
  192. }
  193. // System.out.println("total"+num); PRINT THE TOTAL
  194. scanner.close();
  195. }
  196. //read file is copey to the data_set
  197. public static void parseLine(String line,int lineNo)
  198. {
  199. Scanner lineScanner = new Scanner(line);
  200. lineScanner.useDelimiter(",");
  201. for(int col=0;col<row;col++)
  202. {
  203. Double arry=lineScanner.nextDouble();
  204. data_set[num][col]=arry; ///here read data set is assign the variable data_set
  205. }
  206. num++;
  207.  
  208. }
  209. public static void first_itration()
  210. { double a = 0;
  211. System.out.println("ENTER K");
  212. Scanner sc=new Scanner(System.in);
  213. cnum=sc.nextInt(); //enter the number of cenroid
  214.  
  215. int result[]=new int[cnum];
  216. double re=0;
  217.  
  218. System.out.println("centroid");
  219. for(int i=0;i<cnum;i++)
  220. {
  221. for(int j=0;j<row;j++)
  222. {
  223. intial_centroid[i][j]=data_set[i][j]; //// CENTROID ARE STORED IN AN intial_centroid variable
  224. System.out.print(intial_centroid[i][j]);
  225. }
  226. System.out.println();
  227. }
  228. System.out.println("------------");
  229.  
  230. int counter1=0;
  231. for(int i=0;i<num;i++)
  232. {
  233. for(int j=0;j<row;j++)
  234. {
  235. //System.out.println("hii");
  236. System.out.print(data_set[i][j]);
  237.  
  238. }
  239. counter1++;
  240. System.out.println();
  241. }
  242. System.out.println("total="+counter1); //print the total number of data
  243. //----------------------------------
  244.  
  245. ///////////////////EUCLIDEAN DISTANCE////////////////////////////////////
  246. /// find the Euclidean Distance
  247. for(int i=0;i<num;i++)
  248. {
  249. for(int j=0;j<cnum;j++)
  250. {
  251. re=0;
  252. for(int k=0;k<row;k++)
  253. {
  254. a= (intial_centroid[j][k]-data_set[i][k]);
  255. //System.out.println(a);
  256. a=a*a;
  257. re=re+a; // store the row sum
  258.  
  259. }
  260.  
  261. diff[i][j]= Math.sqrt(re);// find the squre root
  262.  
  263. }
  264. }
  265. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  266.  
  267. ///////////////////////////////////////////////FIND THE SMALLEST VALUE////////////////////////////////////////////////
  268. double aaa;
  269. double counter;
  270. int ccc=1;
  271. for(int i=0;i<num;i++)
  272. {
  273. int c=1;
  274. counter=c;
  275. aaa=diff[i][0];
  276. for(int j=0;j<cnum;j++)
  277. {
  278. //System.out.println(diff[i][j]);
  279.  
  280. if(aaa>=diff[i][j] ) //change
  281. {
  282. aaa=diff[i][j];
  283. counter=j;
  284.  
  285.  
  286. // Jth value are stord in the counter variable
  287. // System.out.println(counter);
  288. }
  289.  
  290.  
  291. }
  292.  
  293. data_set[i][row]=counter; //assign the counter to last position of data set
  294.  
  295. //System.out.println("--");
  296. } //print the first itration
  297. System.out.println("**FIRST ITRATION**");
  298.  
  299. for(int i=0;i<num;i++)
  300. {
  301. for(int j=0;j<=row;j++)
  302. {
  303. //System.out.println("hii");
  304. System.out.print(data_set[i][j]+ " ");
  305. }
  306. System.out.println();
  307. }
  308.  
  309. it++;
  310. }
  311.  
  312.  
  313. public static void calck_mean()
  314. {
  315. for(int i=0;i<20000;i++)
  316. {
  317. for(int j=0;j<100;j++)
  318. {
  319. center_mean[i][j]=0.0;
  320. }
  321. }
  322.  
  323.  
  324. double c = 0;
  325. int a=0;
  326. int p;
  327. int abbb = 0;
  328. if(it%2==0)
  329. {
  330. abbb=row;
  331. }
  332. else if(it%2==1)
  333. {
  334. abbb=row+1;
  335. }
  336. for(int k=0;k<cnum;k++)
  337. {
  338. double counter = 0;
  339. for(int i=0;i<num;i++)
  340. {
  341. for(int j=0;j<=row;j++)
  342. {
  343. if(data_set[i][abbb]==a)
  344. {
  345. System.out.print(data_set[i][j]);
  346. center_mean[k][j] += data_set[i][j];
  347.  
  348. }
  349.  
  350. }
  351. System.out.println();
  352. if(data_set[i][abbb]==a)
  353. {
  354. counter++;
  355. }
  356. System.out.println();
  357. }
  358.  
  359. a++;
  360. total_mean[k]=counter;
  361.  
  362. }
  363. for(int i=0;i<cnum;i++)
  364. {
  365. System.out.println("n");
  366. for(int j=0;j<row;j++)
  367. {
  368. if(total_mean[i]==0)
  369. {
  370. center_mean[i][j]=0.0;
  371. }
  372. else
  373. {
  374. center_mean[i][j]=center_mean[i][j]/total_mean[i];
  375. }
  376. }
  377. }
  378. for(int k=0;k<cnum;k++)
  379. {
  380. for(int j=0;j<row;j++)
  381. {
  382. //System.out.print(center_mean[k][j]);
  383. }
  384. System.out.println();
  385.  
  386. }
  387. /* for(int j=0;j<cnum;j++)
  388. {
  389. System.out.println(total_mean[j]);
  390. }*/
  391.  
  392. }
  393. public static void kmeans1()
  394. {
  395. double a = 0;
  396. int result[]=new int[cnum];
  397. double re=0;
  398.  
  399. //// CENTROID ARE STORED IN AN data_set VARIABLE intial_centroid
  400. System.out.println(" new centroid");
  401. for(int i=0;i<cnum;i++)
  402. {
  403. for(int j=0;j<row;j++)
  404. {
  405. intial_centroid[i][j]=center_mean[i][j];
  406. System.out.print(intial_centroid[i][j]);
  407. }
  408. System.out.println();
  409. }
  410.  
  411. //----------------------------------------------JUST PRINT THE data_set
  412.  
  413. //----------------------------------
  414. for(int i=0;i<num;i++)
  415. {
  416. for(int j=0;j<cnum;j++)
  417. {
  418. re=0;
  419. for(int k=0;k<row;k++)
  420. {
  421.  
  422. a=(intial_centroid[j][k]-data_set[i][k]);
  423. //System.out.println(a);
  424. a=a*a;
  425. re=re+a;
  426.  
  427. }
  428.  
  429. diff[i][j]= Math.sqrt(re);
  430. //System.out.println(diff[i][j]);
  431. }
  432. }
  433. double aaa;
  434. double counter;
  435. for(int i=0;i<num;i++)
  436. {
  437.  
  438. int c=1;
  439. counter=c;
  440. aaa=diff[i][0];
  441. for(int j=0;j<cnum;j++)
  442. {
  443. // System.out.println(diff[i][j]);
  444. if(aaa>=diff[i][j]) //change
  445. {
  446. aaa=diff[i][j];
  447. counter=j;
  448. // System.out.println(counter);
  449. }
  450.  
  451.  
  452. }
  453.  
  454.  
  455. if(it%2==0)
  456. {
  457. // abbb=4;
  458. data_set[i][row+1]=counter;
  459. }
  460. else if(it%2==1)
  461. {
  462. data_set[i][row]=counter;
  463. // abbb=4;
  464. }
  465.  
  466.  
  467. //System.out.println("--");
  468. }
  469. System.out.println(it+" ITRATION**");
  470.  
  471. for(int i=0;i<num;i++)
  472. {
  473. for(int j=0;j<=row+1;j++)
  474. {
  475. //System.out.println("hii");
  476. System.out.print(data_set[i][j]+" ");
  477. }
  478. System.out.println();
  479. }
  480.  
  481. it++;
  482. }
  483. public static void check()
  484. {
  485. checker=0;
  486. for(int i=0;i<num;i++)
  487. {
  488. //System.out.println("hii");
  489. if(Double.compare(data_set[i][row],data_set[i][row+1]) != 0)
  490. {
  491. checker=1;
  492. //System.out.println("hii " + i + " " + data_set[i][4]+ " "+data_set[i][4]);
  493. break;
  494. }
  495. System.out.println();
  496. }
  497.  
  498. }
  499. public static void dispaly()
  500. {
  501.  
  502. System.out.println(it+" ITRATION**");
  503.  
  504. for(int i=0;i<num;i++)
  505. {
  506. for(int j=0;j<=row+1;j++)
  507. {
  508. //System.out.println("hii");
  509. System.out.print(data_set[i][j]+" ");
  510. }
  511. System.out.println();
  512. }
  513. }
  514.  
  515.  
  516. public static void print()
  517. {
  518. System.out.println();
  519. System.out.println();
  520. System.out.println();
  521. System.out.println("----OUTPUT----");
  522. int c=0;
  523. int a=0;
  524. for(int i=0;i<cnum;i++)
  525. {
  526. System.out.println("---------CLUSTER-"+i+"-----");
  527. a=0;
  528. for(int j=0;j<num;j++)
  529. {
  530. if(data_set[j][row]==i)
  531. {a++;
  532. for(int k=0;k<row;k++)
  533. {
  534.  
  535. System.out.print(data_set[j][k]+" ");
  536. }
  537. c++;
  538. System.out.println();
  539. }
  540. //System.out.println(num);
  541.  
  542. }
  543. System.out.println("CLUSTER INSTANCES="+a);
  544.  
  545.  
  546. }
  547. System.out.println("TOTAL INSTANCE"+c);
  548. }
  549.  
  550.  
  551. public static void main(String[] args) throws FileNotFoundException
  552. {
  553. readFile();
  554. first_itration();
  555.  
  556. while(checker!=0)
  557. {
  558. calck_mean();
  559. kmeans1();
  560. check();
  561. }
  562. dispaly();
  563. print();
  564. }
  565.  
  566.  
  567.  
  568.  
  569. }
  570.  
  571.  
  572. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement