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
推荐阅读
- Java|社招和校招有什么不同(阿里美团等大厂 JAVA 社招面经分享)
- Java学习|[Java] 弄懂数组,这一篇就够了
- java毕设系列|基于SSM的水果商城(免费源码获取+运行指导+论文)
- Java编程|【java篇】java中数组到底是不是对象()
- nginx|Nginx学习笔记(五)(浅析Nginx原理)
- #|《实战 Java 高并发程序设计》笔记——第4章 锁的优化及注意事项(一)
- Java业务开发常见错误100例(代码篇-1)
- Java业务开发常见错误100例(代码篇-2)
- 数据库开发|通过栗子来学习MySQL高级知识点(学习,复习,面试都可)