1.进行权重排序
/**
********************************
* Voting using the instances.
*
* @param paraNeighbors The sorted indices of the neighbors.
* @return The predicted label.
********************************
*/
public int weightedVoting(int[] paraNeighbors) {
int[] tempVotes = new int[dataset.numClasses()];
for (int i = 0; i < paraNeighbors.length; i++) {
//投票,由于i=0的点即对应距离最小点,因此可直接根据距离顺序决定投票权重
tempVotes[(int) dataset.instance(paraNeighbors[i]).classValue()] += 2/(i+1);
}//of for i
int tempMaximalVotingIndex = 0;
int tempMaximalVoting = 0;
//统票
for (int i = 0; i < dataset.numClasses(); i++) {
if (tempVotes[i] > tempMaximalVoting) {
tempMaximalVoting = tempVotes[i];
tempMaximalVotingIndex = i;
}//of if
}//of for i
return tempMaximalVotingIndex;
}//of weightedVoting
2.leave-one-out
/**
********************************
* Split the data into training and testing parts.
*
* @param tempIndex
********************************
*/
public void oneSplitTrainingTesting(int tempIndex) {
int tempSize = dataset.numInstances();
int tempTrainingSize = (int) (tempSize - 1);
trainingSet = new int[tempTrainingSize];
testingSet = new int[tempSize - tempTrainingSize];
int tempTrainIdx = 0;
for (int i = 0; i < tempSize; i++) {
if (i != tempIndex)
trainingSet[tempTrainIdx++] =(int) dataset.instance(i).classValue();
}//of for i
testingSet[0] = (int)dataset.instance(tempIndex).classValue();
}//of oneSplitTrainingTesting