@Override public int compare(KNNNode o1, KNNNode o2) { if (o1.getDistance() >= o2.getDistance()) { return 1; } else { return 0; } }
/** * 执行KNN算法,获取测试元组的类别 * * @param list 训练数据集 * @param test 测试元组 * @param k 设定的K值 * @return 测试元组的类别 */ public String knn(List<List<Double>> list, List<Double> test, int k) { PriorityQueue<KNNNode> pq = new PriorityQueue<KNNNode>(k, comparator); for (int i = 0; i < k; i++) { List<Double> trainTuple = list.get(i); Double c = trainTuple.get(0); KNNNode node = new KNNNode(i, getDistance(test, trainTuple), c); pq.add(node); } for (int i = 0; i < list.size(); i++) { List<Double> t = list.get(i); double distance = getDistance(test, t); KNNNode top = pq.peek(); if (top.getDistance() > distance) { pq.remove(); pq.add(new KNNNode(i, distance, t.get(0))); } } return getMostClass(pq); }
/** * 获取所得到的k个最近邻元组的多数类 * * @param pq 存储k个最近近邻元组的优先级队列 * @return 多数类的名称 */ private String getMostClass(PriorityQueue<KNNNode> pq) { Map<Double, Integer> classCount = new HashMap<Double, Integer>(); for (int i = 0; i < pq.size(); i++) { KNNNode node = pq.remove(); double c = node.getC(); if (classCount.containsKey(c)) { classCount.put(c, classCount.get(c) + 1); } else { classCount.put(c, 1); } } int maxIndex = -1; int maxCount = 0; Object[] classes = classCount.keySet().toArray(); for (int i = 0; i < classes.length; i++) { if (classCount.get(classes[i]) > maxCount) { maxIndex = i; maxCount = classCount.get(classes[i]); } } return classes[maxIndex].toString(); }