knncEval
K-nearest neighbor classifier (KNNC)
Contents
Syntax
- [computedClass, overallComputedClass, nnIndex, knncMat]=knncEval(testSet, knncPrm)
Description
computedClass = knncEval(testSet, knncPrm) returns the results of KNNC, where
- testSet: the test set
- knncPrm: parameters for KNNC
- knncPrm.k: the value of k for k-nearest neighbor classification
- computedClass: output vector by KNNC
[computedClass, logLike, recogRate hitIndex, nnIndex] = knncEval(testSet, knncPrm) returns extra info:
- testSet: the test set
- logLike: log likelihood for each of the test set
- recogRate: recognition rate
- hitIndex: index of correctly classified test pairs
- nnIndex: index of knncPrm.input that are closest to testSet.input
Example
[trainSet, testSet]=prData('iris'); trainNum=size(trainSet.input, 2); testNum =size(testSet.input, 2); fprintf('Use of KNNC for Iris data:\n'); fprintf('\tSize of train set (odd-indexed data) = %d\n', trainNum); fprintf('\tSize of test set (even-indexed data) = %d\n', testNum); knncTrainPrm.method='none'; knncPrm=knncTrain(trainSet, knncTrainPrm); fprintf('\tRecognition rates as K varies:\n'); kMax=15; for k=1:kMax knncPrm.k=k; computed=knncEval(testSet, knncPrm); correctCount=sum(testSet.output==computed); recogRate(k)=correctCount/testNum; fprintf('\t%d-NNC ===> RR = 1-%d/%d = %.2f%%.\n', k, testNum-correctCount, testNum, recogRate(k)*100); end plot(1:kMax, recogRate*100, 'b-o'); grid on; title('Recognition rates of Iris data using KNN classifier'); xlabel('K'); ylabel('Recognition rates (%)');
Use of KNNC for Iris data: Size of train set (odd-indexed data) = 75 Size of test set (even-indexed data) = 75 Recognition rates as K varies: 1-NNC ===> RR = 1-3/75 = 96.00%. 2-NNC ===> RR = 1-4/75 = 94.67%. 3-NNC ===> RR = 1-3/75 = 96.00%. 4-NNC ===> RR = 1-3/75 = 96.00%. 5-NNC ===> RR = 1-2/75 = 97.33%. 6-NNC ===> RR = 1-2/75 = 97.33%. 7-NNC ===> RR = 1-2/75 = 97.33%. 8-NNC ===> RR = 1-1/75 = 98.67%. 9-NNC ===> RR = 1-4/75 = 94.67%. 10-NNC ===> RR = 1-2/75 = 97.33%. 11-NNC ===> RR = 1-2/75 = 97.33%. 12-NNC ===> RR = 1-3/75 = 96.00%. 13-NNC ===> RR = 1-3/75 = 96.00%. 14-NNC ===> RR = 1-2/75 = 97.33%. 15-NNC ===> RR = 1-2/75 = 97.33%.
![](knncEval_help_01.png)