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

조회 수: 17 (최근 30일)
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?

채택된 답변

Varun Sai Alaparthi
Varun Sai Alaparthi 2022년 11월 23일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by