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

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

/**
	 * *********************************************************************
	 * Recalculate the nearest k neighbors
	 * 
	 * @param paraCurrent    Current instance. We are comparing it with all others.
	 * @param paraNumNeighbors  The number of neighbors.
	 * @param paraDistanceMeasure  The distance measure.
	 * @return   The 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 simultaneously

		for (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 paraSetDistanceMeasure  The 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

3.设定邻居的数量,即k值

	/**
	 * ******************************************************
	 * Set the number of neighbors.
	 * 
	 * @param paraNumNeighors   The neighbors number
	 * @return  The neighbors number
	 * ******************************************************
	 */
	public int setNumNeighbors(int paraNumNeighors) {
		int tempnumNeighbors = paraNumNeighors;
		return tempnumNeighbors;
	}//of setNumNeighors
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值