KNN for regression and use cross-validation for calculating the error
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Roozbeh ehsani
el 16 de Nov. de 2022
Respondida: Varun Sai Alaparthi
el 23 de Nov. de 2022
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?
0 comentarios
Respuesta aceptada
Varun Sai Alaparthi
el 23 de Nov. de 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.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Statistics and Machine Learning Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!