文章链接:日撸 Java 三百行(总述)_minfanphd的博客-CSDN博客
day52 kNN 分类器 (续) 52.1 重新实现 computeNearests, 仅需要扫描一遍训练集, 即可获得 k 个邻居分析:
扫描一次训练集,当时我第一想法是先扫描训练集,计算测试点到训练集的距离并用距离数组进行存储,在对距离数组进行插入排序获取前k个数据,但是这样并不满足时间复杂度的要求,后面对其进行改进,在计算距离的同时对数据进行排序,这样就满足要求了(因为这个方法返回的邻居,这里借助了一个数组来存储训练集的索引并与距离数组一一对应)。经过测试,符合要求。
当时想着不用两个数组来做,用 List
如下代码:
public int[] computeNearestsNew(int paraCurrent) {
int[] resultNearests = new int[numNeighbors];
int[] trainingIndex = new int[trainingSet.length];
double[] trainingDistance = new double[trainingSet.length];
int j;
double tempData;
//在计算距离的同时进行插入排序
for (int i = 0; i < trainingSet.length; i++) {
trainingDistance[i] = distance(paraCurrent, trainingSet[i]);
tempData = trainingDistance[i];
for (j = i-1; j >= 0 && trainingDistance[j] > tempData; j--) {
trainingDistance[j+1] = trainingDistance[j];
trainingIndex[j+1] = trainingIndex[j];
}
trainingDistance[j+1] = tempData;
trainingIndex[j+1] = i;
}
//获取前面k个邻居
for (int i = 0; i < numNeighbors; i++) {
resultNearests[i] = trainingSet[trainingIndex[i]];
}
System.out.println("The nearest of " + paraCurrent + " are: " + Arrays.toString(resultNearests));
return resultNearests;
}
运行结果:
The nearest of 46 are: [19, 48, 4, 27, 10, 0, 17] The nearest of 50 are: [52, 76, 58, 75, 77, 51, 56] The nearest of 128 are: [132, 104, 103, 111, 112, 147, 140] The nearest of 1 are: [45, 12, 34, 37, 25, 30, 35] The nearest of 54 are: [58, 75, 76, 74, 51, 91, 72] The nearest of 44 are: [19, 43, 48, 26, 24, 27, 23] The nearest of 21 are: [19, 17, 4, 48, 0, 27, 43] The nearest of 53 are: [80, 69, 92, 90, 59, 82, 99] The nearest of 40 are: [17, 0, 4, 7, 49, 39, 27] The nearest of 16 are: [10, 48, 33, 19, 36, 14, 18] The nearest of 89 are: [69, 80, 92, 99, 59, 82, 90] The nearest of 5 are: [18, 10, 48, 19, 33, 27, 31] The nearest of 116 are: [103, 147, 111, 112, 132, 104, 124] The nearest of 88 are: [96, 95, 99, 82, 66, 55, 92] The nearest of 81 are: [80, 69, 79, 92, 82, 64, 62] The nearest of 144 are: [140, 143, 124, 104, 100, 112, 115] The nearest of 101 are: [142, 113, 121, 83, 127, 114, 123] The nearest of 122 are: [105, 135, 125, 117, 102, 131, 129] The nearest of 9 are: [34, 37, 30, 12, 25, 49, 29] The nearest of 126 are: [123, 127, 146, 83, 133, 72, 78] The nearest of 136 are: [148, 115, 100, 140, 124, 104, 143] The nearest of 119 are: [72, 83, 68, 146, 113, 123, 133] The nearest of 32 are: [33, 19, 48, 10, 4, 27, 0] The nearest of 67 are: [92, 82, 99, 69, 95, 96, 71] The nearest of 6 are: [47, 2, 42, 29, 3, 30, 7] The nearest of 106 are: [84, 59, 90, 66, 121, 55, 113] The nearest of 141 are: [145, 139, 112, 147, 115, 140, 77] The nearest of 85 are: [56, 51, 91, 78, 66, 127, 97] The nearest of 61 are: [96, 78, 95, 99, 97, 71, 66] The nearest of 28 are: [27, 0, 39, 17, 7, 49, 4] The nearest of 118 are: [105, 135, 117, 102, 125, 131, 109] The nearest of 86 are: [52, 58, 75, 76, 77, 51, 56] The nearest of 110 are: [147, 115, 77, 145, 139, 127, 112] The nearest of 149 are: [127, 142, 83, 121, 123, 133, 113] The nearest of 138 are: [127, 123, 78, 91, 83, 56, 142] The nearest of 63 are: [91, 73, 78, 97, 127, 55, 133] The nearest of 70 are: [127, 56, 91, 78, 121, 66, 142] The nearest of 130 are: [102, 125, 129, 135, 105, 108, 143] The nearest of 107 are: [125, 105, 102, 129, 135, 143, 108] The nearest of 120 are: [143, 140, 124, 112, 139, 102, 104] The nearest of 65 are: [75, 58, 51, 74, 76, 52, 97] The nearest of 94 are: [99, 96, 90, 92, 55, 95, 82] The nearest of 13 are: [38, 42, 8, 47, 2, 3, 12] The nearest of 137 are: [103, 147, 111, 112, 124, 132, 104] The nearest of 11 are: [29, 7, 26, 24, 30, 49, 39] The accuracy of the classifier is: 0.955555555555555652.2增加 setDistanceMeasure() , setNumNeighors() 方法.
public void setDistanceMeasure(int distanceMeasure) {
this.distanceMeasure = distanceMeasure;
}
public void setNumNeighbors(int numNeighbors) {
this.numNeighbors = numNeighbors;
}
总结:
今天结合昨天的内容,对昨天的内容又进行了一遍梳理,更熟悉这个内容了,对computeNearests ()方法进行重写,提高了其运行效率。



