KNN for regression and use cross-validation for calculating the error

49 views (last 30 days)
I want to learn KNN model for regression problem, and then use k-fold cross-validation dataset to calculate the error of my model. Then repeat this procedure with different number for neighbor(numberofneighbor=1,numberofneighbor=2,..) to have error of my model with considering diffrent neighbor. Then calculate error of my test data set with the best number for number of neighbours that is calculated from previous step.
At first I divide my Data set into test and train via this line:
cv = cvpartition(size(Dataset,2),'HoldOut',0.3);
1. Now I want to learn KNN model and then use k-fold cross validation to calculate the error of the model. how should I do that?
2. After the model learnt, how should I find the error of my test dataset?

Accepted Answer

Varun Sai Alaparthi
Varun Sai Alaparthi on 23 Nov 2022
Hello Roozbeh,
Please try the following code
% let X,Y be train features and target
%xtest, ytest be test features and targets
k = 5;
% k for number of nearest neighbours parameter
metric = 'euclidean';
weights = 'uniform';
mdl = kNNeighborsRegressor(k,metric,weights);
indices = crossvalind('Kfold',Y,5);
for i = 1:5
test = (indices == i);
train = ~test;
mdl = mdl.fit(X(train,:),Y(train));
Ypred = mdl.predict(X(test,:);
error = loss(mdl,Ypred,Y(test))
% you can either display is error or save it in a array
end
%The following code can be used to calculate error on test data
ypred = mdl.predict(xtest)
test_error = loss(mdl,ypred,ytest)
Please refer to this links for more information on Knn for regression and k-fold cross validation:
I hope this information helps and please reach out for any further issues.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!