java|日撸代码300行(第52天(kNN分类器续))

1.重新实现computeNearests,根据闵老师提示,参照插入排序的思想进行。这里需要设置一个距离的序列,在对距离值进行排序的同时,序列也要同步排序。最后得到前k个邻居的序列,这样才能将距离最近的k个邻居选出来。代码保存了调试语句。

/** * ********************************************************************* * Recalculate the nearest k neighbors * * @param paraCurrentCurrent instance. We are comparing it with all others. * @param paraNumNeighborsThe number of neighbors. * @param paraDistanceMeasureThe distance measure. * @returnThe indices of the nearest instances. * ********************************************************************* */ public int[] computeNearests(int paraCurrent, int paraNumNeighbors, int paraDistanceMeasure) { double[] tempResultDistance = new double[trainingSet.length]; int[] tempResultNearests = new int[paraNumNeighbors]; int[] tempResultIndiex = new int[trainingSet.length]; double tempDistance; int j; //Calculate and store distances. The indices are sorted simultaneouslyfor (int i = 0; i < trainingSet.length; i++) { tempResultDistance[i] = distance(paraCurrent, trainingSet[i], paraDistanceMeasure); tempResultIndiex[i] = i; }//of for i//Select the nearest paraK indices. for (int i = 1; i < trainingSet.length; i++) { tempDistance = tempResultDistance[i]; System.out.println("tempDistance = " + tempDistance + ", i = " + i ); for (j = i - 1; j >= 0 && tempDistance < tempResultDistance[j]; j--) { tempResultDistance[j + 1] = tempResultDistance[j]; tempResultIndiex[j + 1] = tempResultIndiex[j]; }//of for j tempResultDistance[j + 1] = tempDistance; tempResultIndiex[j + 1] = i; }//of for i System.out.println("tempResultDistance = " + Arrays.toString(tempResultDistance)); System.out.println(Arrays.toString(tempResultIndiex)); for (int i = 0; i < paraNumNeighbors; i++) { tempResultNearests[i] = trainingSet[tempResultIndiex[i]]; }//of for System.out.println(Arrays.toString(tempResultNearests)); return tempResultNearests; }//of computeNearests

2.设定距离计算的度量
/** * ************************************************************************* * Set the measure of distance. * * @param paraSetDistanceMeasureThe measure * @return * ************************************************************************* */ public int setDistanceMeasure(int paraSetDistanceMeasure) { int resultDistanceMeasure = -1; switch (paraSetDistanceMeasure) { case 0: resultDistanceMeasure = 0; break; case 1: resultDistanceMeasure = 1; break; default: break; } return resultDistanceMeasure; }//of setDistanceMeasure

【java|日撸代码300行(第52天(kNN分类器续))】3.设定邻居的数量,即k值
/** * ****************************************************** * Set the number of neighbors. * * @param paraNumNeighorsThe neighbors number * @returnThe neighbors number * ****************************************************** */ public int setNumNeighbors(int paraNumNeighors) { int tempnumNeighbors = paraNumNeighors; return tempnumNeighbors; }//of setNumNeighors

    推荐阅读