MATLAB Answers

How to use cross validation/ leave one out in algorithm

99 views (last 30 days)
CHHAVI
CHHAVI on 7 Jul 2020
Answered: Pranav Verma on 12 Aug 2020

  0 Comments

Sign in to comment.

Answers (1)

Pranav Verma
Pranav Verma on 12 Aug 2020
Hi Chhavi,
The cvpartition(group,'KFold',k) function with k=n creates a random partition for leave-one-out cross-validation on n observations. Below example demonstrates the aforementioned function,
load('fisheriris');
CVO = cvpartition(species,'k',150); %number of observations 'n' = 150
err = zeros(CVO.NumTestSets,1);
for i = 1:CVO.NumTestSets
trIdx = CVO.training(i);
teIdx = CVO.test(i);
ytest = classify(meas(teIdx,:),meas(trIdx,:),...
species(trIdx,:));
err(i) = sum(~strcmp(ytest,species(teIdx)));
end
cvErr = sum(err)/sum(CVO.TestSize);
Alternatively, you can use cvpartition(n,'LeaveOut') leave-one-out cross-validation.
For further information about the cross-validation in MATLAB, please refer to the link: https://www.mathworks.com/help/stats/cvpartition.html

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by