Guest User

Untitled

a guest
Sep 9th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. import java.io.InputStreamReader;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. public class Kmeans_f
  6. {
  7. private static int NUM_CLUSTERS; // Total clusters. Ввод и присвоение в main
  8. private static int TOTAL_DATA; // Total data points. Ввод и присвоение в main
  9. private static double SAMPLES[][] = new double[TOTAL_DATA][2];
  10. private static ArrayList<Data> dataSet = new ArrayList<Data>();
  11. private static ArrayList<Centroid> centroids = new ArrayList<Centroid>();
  12.  
  13.  
  14. private static void initialize()
  15. {
  16. System.out.println("Centroids initialized at:");
  17. centroids.add(new Centroid(SAMPLES[lowest()][0], SAMPLES[lowest()][1])); // lowest set.
  18. centroids.add(new Centroid(SAMPLES[hightest()][0], SAMPLES[hightest()][1])); // highest set.
  19. System.out.println(" (" + centroids.get(0).X() + ", " + centroids.get(0).Y() + ")");
  20. System.out.println(" (" + centroids.get(1).X() + ", " + centroids.get(1).Y() + ")");
  21. System.out.print("\n");
  22. return;
  23. }
  24.  
  25. private static void kMeanCluster()
  26. {
  27. final double bigNumber = Math.pow(10, 10); // some big number that's sure to be larger than our data range.
  28. double minimum = bigNumber; // The minimum value to beat.
  29. double distance = 0.0; // The current minimum value.
  30. int sampleNumber = 0;
  31. int cluster = 0;
  32. boolean isStillMoving = true;
  33. Data newData = null;
  34.  
  35. // Add in new data, one at a time, recalculating centroids with each new one.
  36. while(dataSet.size() < TOTAL_DATA)
  37. {
  38. newData = new Data(SAMPLES[sampleNumber][0], SAMPLES[sampleNumber][1]);
  39. dataSet.add(newData);
  40. minimum = bigNumber;
  41. for(int i = 0; i < NUM_CLUSTERS; i++)
  42. {
  43. distance = dist(newData, centroids.get(i));
  44. if(distance < minimum){
  45. minimum = distance;
  46. cluster = i;
  47. }
  48. }
  49. newData.cluster(cluster);
  50.  
  51. // calculate new centroids.
  52. for(int i = 0; i < NUM_CLUSTERS; i++)
  53. {
  54. int totalX = 0;
  55. int totalY = 0;
  56. int totalInCluster = 0;
  57. for(int j = 0; j < dataSet.size(); j++)
  58. {
  59. if(dataSet.get(j).cluster() == i){
  60. totalX += dataSet.get(j).X();
  61. totalY += dataSet.get(j).Y();
  62. totalInCluster++;
  63. }
  64. }
  65. if(totalInCluster > 0){
  66. centroids.get(i).X(totalX / totalInCluster);
  67. centroids.get(i).Y(totalY / totalInCluster);
  68. }
  69. }
  70. sampleNumber++;
  71. }
  72.  
  73. // Now, keep shifting centroids until equilibrium occurs.
  74. while(isStillMoving)
  75. {
  76. // calculate new centroids.
  77. for(int i = 0; i < NUM_CLUSTERS; i++)
  78. {
  79. int totalX = 0;
  80. int totalY = 0;
  81. int totalInCluster = 0;
  82. for(int j = 0; j < dataSet.size(); j++)
  83. {
  84. if(dataSet.get(j).cluster() == i){
  85. totalX += dataSet.get(j).X();
  86. totalY += dataSet.get(j).Y();
  87. totalInCluster++;
  88. }
  89. }
  90. if(totalInCluster > 0){
  91. centroids.get(i).X(totalX / totalInCluster);
  92. centroids.get(i).Y(totalY / totalInCluster);
  93. }
  94. }
  95.  
  96. // Assign all data to the new centroids
  97. isStillMoving = false;
  98.  
  99. for(int i = 0; i < dataSet.size(); i++)
  100. {
  101. Data tempData = dataSet.get(i);
  102. minimum = bigNumber;
  103. for(int j = 0; j < NUM_CLUSTERS; j++)
  104. {
  105. distance = dist(tempData, centroids.get(j));
  106. if(distance < minimum){
  107. minimum = distance;
  108. cluster = j;
  109. }
  110. }
  111. tempData.cluster(cluster);
  112. if(tempData.cluster() != cluster){
  113. tempData.cluster(cluster);
  114. isStillMoving = true;
  115. }
  116. }
  117. }
  118. return;
  119. }
  120.  
  121. /**
  122. * // Calculate Euclidean distance.
  123. * @param d - Data object.
  124. * @param c - Centroid object.
  125. * @return - double value.
  126. */
  127. private static double dist(Data d, Centroid c)
  128. {
  129. return Math.sqrt(Math.pow((c.Y() - d.Y()), 2) + Math.pow((c.X() - d.X()), 2));
  130. }
  131.  
  132. private static class Data
  133. {
  134. private double mX = 0;
  135. private double mY = 0;
  136. private int mCluster = 0;
  137.  
  138. public Data()
  139. {
  140. return;
  141. }
  142.  
  143. public Data(double x, double y)
  144. {
  145. this.X(x);
  146. this.Y(y);
  147. return;
  148. }
  149.  
  150. public void X(double x)
  151. {
  152. this.mX = x;
  153. return;
  154. }
  155.  
  156. public double X()
  157. {
  158. return this.mX;
  159. }
  160.  
  161. public void Y(double y)
  162. {
  163. this.mY = y;
  164. return;
  165. }
  166.  
  167. public double Y()
  168. {
  169. return this.mY;
  170. }
  171.  
  172. public void cluster(int clusterNumber)
  173. {
  174. this.mCluster = clusterNumber;
  175. return;
  176. }
  177.  
  178. public int cluster()
  179. {
  180. return this.mCluster;
  181. }
  182. }
  183.  
  184. private static class Centroid
  185. {
  186. private double mX = 0.0;
  187. private double mY = 0.0;
  188.  
  189. public Centroid()
  190. {
  191. return;
  192. }
  193.  
  194. public Centroid(double newX, double newY)
  195. {
  196. this.mX = newX;
  197. this.mY = newY;
  198. return;
  199. }
  200.  
  201. public void X(double newX)
  202. {
  203. this.mX = newX;
  204. return;
  205. }
  206.  
  207. public double X()
  208. {
  209. return this.mX;
  210. }
  211.  
  212. public void Y(double newY)
  213. {
  214. this.mY = newY;
  215. return;
  216. }
  217.  
  218. public double Y()
  219. {
  220. return this.mY;
  221. }
  222. }
  223. private static int lowest() {
  224. double lowx = SAMPLES[0][0];
  225. double lowy = SAMPLES[0][1];
  226. int minX=0;
  227. for (int i = 0; i < SAMPLES.length; i++) {
  228. double numx = SAMPLES[i][0];
  229. double numy = SAMPLES[i][1];
  230. // double newLow = (Math.min(numx,lowx));
  231. if (numx < lowx && numy < lowy) {
  232. // (Math.min(numy,lowy)== numy)
  233. lowx = numx;
  234. lowy = numy;
  235. minX = i;
  236. }
  237. else if ((numx == lowx || numx< lowx) && numy < lowy){
  238. lowx = numx;
  239. lowy = numy;
  240. minX = i;
  241. }
  242. else if (numx < lowx && (numy<lowy || numy == lowy)){
  243. lowx = numx;
  244. lowy = numy;
  245. minX = i;
  246. }
  247. }
  248. return minX;
  249. }
  250. private static int hightest(){
  251. double hightx = SAMPLES[0][0];
  252. double highty = SAMPLES[0][1];
  253. int maxX=0;
  254. for (int i = 0; i < SAMPLES.length; i++){
  255. double numx = SAMPLES[i][0];
  256. double numy = SAMPLES[i][1];
  257. if (numx > hightx && numy > highty){
  258. hightx = numx;
  259. highty = numy;
  260. maxX = i;
  261. }
  262. else if ((numx == hightx || numx > hightx) && numy > highty){
  263. hightx = numx;
  264. highty = numy;
  265. maxX = i;
  266. }
  267. else if (numx > hightx && (numy > highty || numy == highty)){
  268. hightx = numx;
  269. highty = numy;
  270. maxX = i;
  271. }
  272. }
  273. return maxX;
  274. }
  275.  
  276.  
  277. public static void main(String[] args)
  278. {
  279. // вот это все до initialize() было в отдельных методах, но они не инициализировались
  280. Scanner c = new Scanner(System.in);
  281. System.out.println("Введите количество кластеров:");
  282. NUM_CLUSTERS = c.nextInt(); //присвоение введенной цифры
  283. System.out.println("Введите кол-во точек:");
  284. TOTAL_DATA = c.nextInt(); //присвоение
  285. //ввод коорд точек
  286. for (int i = 0; i < TOTAL_DATA; i++) {
  287. for (int j = 0; j < 2; j++) {
  288. if (j==0)
  289. {
  290. System.out.print("Введите значение x элемента Matrix[" + i + "][" + j + "]:");
  291. SAMPLES[i][j] = c.nextDouble();
  292. }
  293. else if (j==1){
  294. System.out.print("Введите значение y элементa Matrix[" + i + "][" + j + "]:");
  295. SAMPLES[i][j] = c.nextDouble();
  296. }
  297. }
  298. }
  299. c.close();
  300. // работа алгоритма, там все нормально
  301. initialize();
  302. kMeanCluster();
  303.  
  304.  
  305. // Print out clustering results.
  306. for(int i = 0; i < NUM_CLUSTERS; i++)
  307. {
  308. System.out.println("Cluster " + i + " includes:");
  309. for(int j = 0; j < TOTAL_DATA; j++)
  310. {
  311. if(dataSet.get(j).cluster() == i){
  312. System.out.println(" (" + dataSet.get(j).X() + ", " + dataSet.get(j).Y() + ")");
  313. }
  314. } // j
  315. System.out.println();
  316. } // i
  317.  
  318. // Print out centroid results.
  319. System.out.println("Centroids finalized at:");
  320. for(int i = 0; i < NUM_CLUSTERS; i++)
  321. {
  322. System.out.println(" (" + centroids.get(i).X() + ", " + centroids.get(i).Y());
  323. }
  324. System.out.print("\n");
  325. return;
  326. }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment