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

29 次查看(过去 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