Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.69 KB | None | 0 0
  1. ///////////////////////////////////
  2. // KNN Algorithme with JAVA //
  3. // Developped by Ihab Zaidi //
  4. // Thanks To Noureddin Sadawi //
  5. ///////////////////////////////////
  6. import java.util.*;
  7.  
  8. class KNN
  9. {
  10. // our Data Base
  11. static double[][] instances = {
  12. {6.300000,2.500000,5.000000,1.900000,3.000000},
  13. {6.900000,3.200000,5.700000,2.300000,3.000000},
  14. {5.100000,3.800000,1.900000,0.400000,1.000000},
  15. {6.600000,2.900000,4.600000,1.300000,2.000000},
  16. {5.700000,4.400000,1.500000,0.400000,1.000000},
  17. {4.600000,3.400000,1.400000,0.300000,1.000000},
  18. {4.300000,3.000000,1.100000,0.100000,1.000000},
  19. {6.900000,3.100000,5.100000,2.300000,3.000000},
  20. {5.100000,3.700000,1.500000,0.400000,1.000000},
  21. {6.100000,2.800000,4.700000,1.200000,2.000000},
  22. {5.800000,2.700000,5.100000,1.900000,3.000000},
  23. {4.600000,3.100000,1.500000,0.200000,1.000000},
  24. {6.200000,3.400000,5.400000,2.300000,3.000000},
  25. {6.700000,3.300000,5.700000,2.100000,3.000000},
  26. {4.900000,3.000000,1.400000,0.200000,1.000000},
  27. {7.100000,3.000000,5.900000,2.100000,3.000000},
  28. {5.000000,3.500000,1.600000,0.600000,1.000000},
  29. {6.700000,3.100000,5.600000,2.400000,3.000000},
  30. {4.900000,3.600000,1.400000,0.100000,1.000000},
  31. {5.900000,3.000000,5.100000,1.800000,3.000000},
  32. {5.800000,2.600000,4.000000,1.200000,2.000000},
  33. {6.800000,3.200000,5.900000,2.300000,3.000000},
  34. {5.400000,3.900000,1.300000,0.400000,1.000000},
  35. {5.700000,3.000000,4.200000,1.200000,2.000000},
  36. {6.100000,2.800000,4.000000,1.300000,2.000000},
  37. {6.100000,3.000000,4.900000,1.800000,3.000000},
  38. {6.700000,3.100000,4.700000,1.500000,2.000000},
  39. {5.200000,4.100000,1.500000,0.100000,1.000000},
  40. {6.500000,3.000000,5.800000,2.200000,3.000000},
  41. {4.900000,2.500000,4.500000,1.700000,3.000000},
  42. {5.700000,2.800000,4.100000,1.300000,2.000000},
  43. {4.800000,3.400000,1.900000,0.200000,1.000000},
  44. {5.000000,3.300000,1.400000,0.200000,1.000000},
  45. {5.100000,3.800000,1.600000,0.200000,1.000000},
  46. {6.700000,3.000000,5.200000,2.300000,3.000000},
  47. {5.800000,4.000000,1.200000,0.200000,1.000000},
  48. {5.500000,3.500000,1.300000,0.200000,1.000000},
  49. {6.700000,3.300000,5.700000,2.500000,3.000000},
  50. {6.200000,2.800000,4.800000,1.800000,3.000000},
  51. {6.400000,3.200000,5.300000,2.300000,3.000000},
  52. {5.000000,3.400000,1.500000,0.200000,1.000000},
  53. {6.400000,2.800000,5.600000,2.100000,3.000000},
  54. {5.100000,3.500000,1.400000,0.200000,1.000000},
  55. {5.100000,3.400000,1.500000,0.200000,1.000000},
  56. {6.000000,2.200000,4.000000,1.000000,2.000000},
  57. {5.600000,3.000000,4.500000,1.500000,2.000000},
  58. {7.200000,3.000000,5.800000,1.600000,3.000000},
  59. {5.400000,3.000000,4.500000,1.500000,2.000000},
  60. {7.200000,3.600000,6.100000,2.500000,3.000000},
  61. {6.400000,2.800000,5.600000,2.200000,3.000000},
  62. {6.300000,2.800000,5.100000,1.500000,3.000000},
  63. {5.400000,3.900000,1.700000,0.400000,1.000000},
  64. {6.000000,3.000000,4.800000,1.800000,3.000000},
  65. {6.400000,2.900000,4.300000,1.300000,2.000000},
  66. {6.300000,2.900000,5.600000,1.800000,3.000000},
  67. {5.500000,2.400000,3.800000,1.100000,2.000000},
  68. {4.700000,3.200000,1.600000,0.200000,1.000000},
  69. {6.300000,2.700000,4.900000,1.800000,3.000000},
  70. {7.700000,2.800000,6.700000,2.000000,3.000000},
  71. {5.200000,2.700000,3.900000,1.400000,2.000000},
  72. {5.600000,2.700000,4.200000,1.300000,2.000000},
  73. {6.000000,3.400000,4.500000,1.600000,2.000000},
  74. {6.500000,3.000000,5.500000,1.800000,3.000000},
  75. {4.800000,3.100000,1.600000,0.200000,1.000000},
  76. {4.400000,3.000000,1.300000,0.200000,1.000000},
  77. {6.000000,2.900000,4.500000,1.500000,2.000000},
  78. {6.500000,3.200000,5.100000,2.000000,3.000000},
  79. {4.900000,3.100000,1.500000,0.200000,1.000000},
  80. {6.100000,2.600000,5.600000,1.400000,3.000000},
  81. {5.900000,3.000000,4.200000,1.500000,2.000000},
  82. {6.500000,3.000000,5.200000,2.000000,3.000000},
  83. {5.000000,3.000000,1.600000,0.200000,1.000000},
  84. {5.400000,3.700000,1.500000,0.200000,1.000000},
  85. {7.400000,2.800000,6.100000,1.900000,3.000000},
  86. {5.700000,2.800000,4.500000,1.300000,2.000000},
  87. };
  88. private static String findMajorityClass(String[] array)
  89. {
  90. //add the String array to a HashSet to get unique String values
  91. Set<String> h = new HashSet<String>(Arrays.asList(array));
  92. //convert the HashSet back to array
  93. String[] uniqueValues = h.toArray(new String[0]);
  94. //counts for unique strings
  95. int[] counts = new int[uniqueValues.length];
  96. // loop thru unique strings and count how many times they appear in origianl array
  97. for (int i = 0; i < uniqueValues.length; i++) {
  98. for (int j = 0; j < array.length; j++) {
  99. if(array[j].equals(uniqueValues[i])){
  100. counts[i]++;
  101. }
  102. }
  103. }
  104.  
  105. for (int i = 0; i < uniqueValues.length; i++)
  106. System.out.println(uniqueValues[i]);
  107. for (int i = 0; i < counts.length; i++)
  108. System.out.println(counts[i]);
  109.  
  110.  
  111. int max = counts[0];
  112. for (int counter = 1; counter < counts.length; counter++) {
  113. if (counts[counter] > max) {
  114. max = counts[counter];
  115. }
  116. }
  117. System.out.println("max # of occurences: "+max);
  118. int freq = 0;
  119. for (int counter = 0; counter < counts.length; counter++) {
  120. if (counts[counter] == max) {
  121. freq++;
  122. }
  123. }
  124. int index = -1;
  125. if(freq==1){
  126. for (int counter = 0; counter < counts.length; counter++) {
  127. if (counts[counter] == max) {
  128. index = counter;
  129. break;
  130. }
  131. }
  132. //System.out.println("one majority class, index is: "+index);
  133. return uniqueValues[index];
  134. } else{//we have multiple modes
  135. int[] ix = new int[freq];//array of indices of modes
  136. System.out.println("multiple majority classes: "+freq+" classes");
  137. int ixi = 0;
  138. for (int counter = 0; counter < counts.length; counter++) {
  139. if (counts[counter] == max) {
  140. ix[ixi] = counter;//save index of each max count value
  141. ixi++; // increase index of ix array
  142. }
  143. }
  144.  
  145. for (int counter = 0; counter < ix.length; counter++)
  146. System.out.println("class index: "+ix[counter]);
  147.  
  148. //now choose one at random
  149. Random generator = new Random();
  150. //get random number 0 <= rIndex < size of ix
  151. int rIndex = generator.nextInt(ix.length);
  152. System.out.println("random index: "+rIndex);
  153. int nIndex = ix[rIndex];
  154. //return unique value at that index
  155. return uniqueValues[nIndex];
  156. }
  157.  
  158. }
  159.  
  160.  
  161.  
  162. private static double meanOfArray(double[] m) {
  163. double sum = 0.0;
  164. for (int j = 0; j < m.length; j++){
  165. sum += m[j];
  166. }
  167. return sum/m.length;
  168. }
  169.  
  170.  
  171.  
  172. public static void main(String args[]){
  173. System.out.println("Insert Your Data : ");
  174. System.out.println("\n");
  175. Scanner scan = new Scanner(System.in);
  176. double insertedInt = scan.nextDouble();
  177. double insertedInt2 = scan.nextDouble();
  178. double insertedInt3 = scan.nextDouble();
  179. double insertedInt4 = scan.nextDouble();
  180. double insertedInt5 = scan.nextDouble();
  181. double[] query = {insertedInt, insertedInt2, insertedInt3, insertedInt4, insertedInt5};
  182.  
  183. int k = 9;// K : Welaya List Counts
  184. List<City> cityList = new ArrayList<City>();
  185. List<Result> resultList = new ArrayList<Result>();
  186. cityList.add(new City(instances[0],"IRIS0"));
  187. cityList.add(new City(instances[1],"IRIS1"));
  188. cityList.add(new City(instances[2],"IRIS2"));
  189. cityList.add(new City(instances[3],"IRIS3"));
  190. cityList.add(new City(instances[4],"IRIS4"));
  191. cityList.add(new City(instances[5],"IRIS5"));
  192. cityList.add(new City(instances[6],"IRIS6"));
  193. cityList.add(new City(instances[7],"IRIS7"));
  194. cityList.add(new City(instances[8],"IRIS8"));
  195. cityList.add(new City(instances[9],"IRIS9"));
  196. cityList.add(new City(instances[10],"IRIS10"));
  197. cityList.add(new City(instances[11],"IRIS11"));
  198. cityList.add(new City(instances[12],"IRIS12"));
  199. cityList.add(new City(instances[13],"IRIS13"));
  200. cityList.add(new City(instances[14],"IRIS14"));
  201. cityList.add(new City(instances[15],"IRIS15"));
  202. cityList.add(new City(instances[16],"IRIS16"));
  203. cityList.add(new City(instances[17],"IRIS17"));
  204. cityList.add(new City(instances[18],"IRIS18"));
  205. cityList.add(new City(instances[19],"IRIS19"));
  206. cityList.add(new City(instances[20],"IRIS20"));
  207. cityList.add(new City(instances[21],"IRIS21"));
  208. cityList.add(new City(instances[22],"IRIS22"));
  209. cityList.add(new City(instances[23],"IRIS23"));
  210. cityList.add(new City(instances[24],"IRIS24"));
  211. cityList.add(new City(instances[25],"IRIS25"));
  212. cityList.add(new City(instances[26],"IRIS26"));
  213. cityList.add(new City(instances[27],"IRIS27"));
  214. cityList.add(new City(instances[28],"IRIS28"));
  215. cityList.add(new City(instances[29],"IRIS29"));
  216. cityList.add(new City(instances[30],"IRIS30"));
  217. cityList.add(new City(instances[31],"IRIS31"));
  218. cityList.add(new City(instances[32],"IRIS32"));
  219. cityList.add(new City(instances[33],"IRIS33"));
  220. cityList.add(new City(instances[34],"IRIS34"));
  221. cityList.add(new City(instances[35],"IRIS35"));
  222. cityList.add(new City(instances[36],"IRIS36"));
  223. cityList.add(new City(instances[37],"IRIS37"));
  224. cityList.add(new City(instances[38],"IRIS38"));
  225. cityList.add(new City(instances[39],"IRIS39"));
  226. cityList.add(new City(instances[40],"IRIS40"));
  227. cityList.add(new City(instances[41],"IRIS41"));
  228. cityList.add(new City(instances[42],"IRIS42"));
  229. cityList.add(new City(instances[43],"IRIS43"));
  230. cityList.add(new City(instances[44],"IRIS44"));
  231. cityList.add(new City(instances[45],"IRIS45"));
  232. cityList.add(new City(instances[46],"IRIS46"));
  233. cityList.add(new City(instances[47],"IRIS47"));
  234. cityList.add(new City(instances[48],"IRIS48"));
  235. cityList.add(new City(instances[49],"IRIS49"));
  236. cityList.add(new City(instances[50],"IRIS50"));
  237. cityList.add(new City(instances[51],"IRIS51"));
  238. cityList.add(new City(instances[52],"IRIS52"));
  239. cityList.add(new City(instances[53],"IRIS53"));
  240. cityList.add(new City(instances[54],"IRIS54"));
  241. cityList.add(new City(instances[55],"IRIS55"));
  242. cityList.add(new City(instances[56],"IRIS56"));
  243. cityList.add(new City(instances[57],"IRIS57"));
  244. cityList.add(new City(instances[58],"IRIS58"));
  245. cityList.add(new City(instances[59],"IRIS59"));
  246. cityList.add(new City(instances[60],"IRIS60"));
  247. cityList.add(new City(instances[61],"IRIS61"));
  248. cityList.add(new City(instances[62],"IRIS62"));
  249. cityList.add(new City(instances[63],"IRIS63"));
  250. cityList.add(new City(instances[64],"IRIS64"));
  251. cityList.add(new City(instances[65],"IRIS65"));
  252. cityList.add(new City(instances[66],"IRIS66"));
  253. cityList.add(new City(instances[67],"IRIS67"));
  254. cityList.add(new City(instances[68],"IRIS68"));
  255. cityList.add(new City(instances[69],"IRIS69"));
  256. cityList.add(new City(instances[70],"IRIS70"));
  257. cityList.add(new City(instances[71],"IRIS71"));
  258. cityList.add(new City(instances[72],"IRIS72"));
  259. cityList.add(new City(instances[73],"IRIS73"));
  260. cityList.add(new City(instances[74],"IRIS74"));
  261. cityList.add(new City(instances[75],"IRIS75"));
  262. cityList.add(new City(instances[76],"IRIS76"));
  263. //data about user city
  264. //double[] query = {1.65,1.78,1.21,1.29,1.58};
  265. //find distances
  266. for(City city : cityList){
  267. double dist = 0.0;
  268. for(int j = 0; j < city.cityAttributes.length; j++){
  269. dist += Math.pow(city.cityAttributes[j] - query[j], 2) ;
  270. //System.out.print(city.cityAttributes[j]+" ");
  271. }
  272. double distance = Math.sqrt( dist );
  273. resultList.add(new Result(distance,city.cityName));
  274. //System.out.println(distance);
  275. }
  276.  
  277. //System.out.println(resultList);
  278. Collections.sort(resultList, new DistanceComparator());
  279. String[] ss = new String[k];
  280. for(int x = 0; x < k; x++){
  281. System.out.println(resultList.get(x).cityName+ " .... " + resultList.get(x).distance);
  282. //get classes of k nearest instances (city names) from the list into an array
  283. ss[x] = resultList.get(x).cityName;
  284. }
  285. String majClass = findMajorityClass(ss);
  286. System.out.println("The Nearest IRIS Class is : "+majClass);
  287. }//end main
  288.  
  289. //simple class to model instances (features + class)
  290. static class City {
  291. double[] cityAttributes;
  292. String cityName;
  293. public City(double[] cityAttributes, String cityName){
  294. this.cityName = cityName;
  295. this.cityAttributes = cityAttributes;
  296. }
  297. }
  298. //simple class to model results (distance + class)
  299. static class Result {
  300. double distance;
  301. String cityName;
  302. public Result(double distance, String cityName){
  303. this.cityName = cityName;
  304. this.distance = distance;
  305. }
  306. }
  307. //simple comparator class used to compare results via distances
  308. static class DistanceComparator implements Comparator<Result> {
  309. @Override
  310. public int compare(Result a, Result b) {
  311. return a.distance < b.distance ? -1 : a.distance == b.distance ? 0 : 1;
  312. }
  313. }
  314.  
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement