0

I am working on a Pattern Recognition project and I face some problems. I have loaded the Fisher's Iris data set on my project and I want to run the k-NN classifier(for k = 1,3,5) on the above data set. But I want the following division: 80% training set and 20% test set. I want the partition to be repeated 5 times. How can it be done?

I have some code about that, but I do not even know whether I am in the right way or not.

% Regarding the random permutation that I want
[Xtrain,Xval,Xtest] = dividerand(150,0.8,0,0.2); 


% Regarding the k-NN classification
X = meas;
Y = species;
z1 = fitcknn(X,Y,'NumNeighbors',5,'Standardize',1);

I do not know if my code is in the right way, what is missing or even whether there is a better way to do my job than mine or not.

Could anyone help me to complete my task?

Jimbo_ai
  • 167
  • 1
  • 13
  • 1
    Either use a `for`-loop or use [crossval](http://www.mathworks.com/help/stats/crossval.html) instead – Dan Jan 15 '16 at 08:51
  • Do you mean to use cvmodel = crossval(z1,kfold,5)? – Jimbo_ai Jan 15 '16 at 09:35
  • instead of `z1` you would need to create a function that incorporates `fitcknn(X,Y,'NumNeighbors',5,'Standardize',1)` – Dan Jan 15 '16 at 10:56
  • And what about X,Y? Ok, Y is the response set, but X contains the all data. I would like to have a 'Xtrain' (80%) and a 'Xtest' (20%). Should I write 'z1(say) = fitcknn(Xtrain, Y, 'NumNeighbors',5,'Standardize',1);'?? My issue lies into separating the training set and the test set. If done, then I can get by... – Jimbo_ai Jan 15 '16 at 11:50
  • 1
    `crossval` will separate it for you. Have a look at the docs. Here's an example using it for NNs instead of KNN but it's the same idea http://stackoverflow.com/a/34784285/1011724 – Dan Jan 15 '16 at 12:12

0 Answers0