% Same as knnapp1, except 5-fold CV for num neighbors. NumFolds=5; load wineclassification; %Loads data in matrix X, t targets=t; %Make things match with the pre-written code (dim x numpts) X=X'; targets=targets'; [Xtrain,Xtest,Ttrain,Ttest]=TrainTestSplit(X,targets,0.3); Data=KfoldCV(Xtrain,Ttrain,NumFolds); count=1 for NumN=2:30 for j=1:NumFolds Pred=fitknn(Data.Train{j},Data.TrainTarget{j},Data.Test{j},NumN); [C1,err1(j)]=conf_matrix(Data.TestTarget{j},Pred,3); end Overall(count)=mean(err1); count=count+1; end plot(2:30,Overall) % Build the full model on the best number of neighbors (13): Pred=fitknn(Xtrain,Ttrain,Xtest,13); [C,err]=conf_matrix(Ttest,Pred,3);