Knnclassify() matlab I am currently working on matlab and I am new to it. I would like to know how do i average the results from the folds (or otherwise combined) to produce a single estimation.
My dataset has 2 class labels +1 and -1. and k=5. I use the knnclassify() method in matlab in order to perform cross validation.
Kindly help! 
 A: The knnclassify() function uses the k-nearest neighbours classification algorithm to perform classification. The crossvalind() function is a separate one to divide a data set in to folds to perform cross-validation. Both are part of the Bioinformatics toolbox. I suggest that you read the documentation associated with them, so that you can be sure you are using them correctly.
In general, however, with a single n-fold cross-validation (CV) run, you don't really need to average results from folds. You just collect the results from the individual folds.
For example, with 5-fold CV, your dataset is divided into 5 separate folds, used as follows:


*

*Train with Folds 1..4, Test on Fold 5

*Train with Folds 1..3+5, Test on Fold 4

*Train with Folds 1+2+4+5 Test on Fold 3
and so on.


So all of the data is used for testing exactly once. You can just combine the test results in a single list, and count how many it got right and wrong to compute the overall accuracy.
A: load fisheriris
indices = crossvalind('Kfold',species,10);
cp = classperf(species); % initializes the CP object
for i = 1:10
    test = (indices == i); train = ~test;
    class = knnclassify(meas(test,:),meas(train,:),species(train));
    % updates the CP object with the current classification results
    classperf(cp,class,test);  
end
cp.CorrectRate % queries for the correct classification rate

