How to create indices for the k-fold cross-validation?

조회 수: 12 (최근 30일)
Susan
Susan 2021년 6월 8일
댓글: Susan 2021년 6월 10일
Hi All,
I'd like to create indices for the k-fold cross-validation using
indices = crossvalind('Kfold',Labels,k);
The "Labels" is a 1-by-1000 cell array which contains 1000 cells, as follows
Labels(1: 10) = 1×10 cell array
Columns 1 through 9
{1×1 cell} {1×1 cell} {1×1 cell} {1×1 cell} {1×1 cell} {1×1 cell} {1×1 cell} {1×1 cell} {1×1 cell}
Column 10
{1×1 cell}
Labels{1}= 1×1 cell array
{'2'}
I got this error
Undefined function 'crossvalind' for input arguments of type 'cell'.
So, I took the following steps to solve the issue
Label_1 = cellfun(@(x) str2double(x), Labels, 'UniformOutput',false);
Label_double = [Label_1{:}]
indices = crossvalind('Kfold',Label_double,10);
And now I am dealing with the following error
Undefined function 'crossvalind' for input arguments of type 'char'.
I'm not sure why I get this error. any idea? Thanks!
  댓글 수: 3
Megumi Fukuda
Megumi Fukuda 2021년 6월 9일
Could you upload your label variable (you can upload .mat file) so that I can have a look? I assume your label is what-I-call 'nested' and that's why you cannot use crossvalind.
Susan
Susan 2021년 6월 9일
편집: Susan 2021년 6월 9일
@Megumi Fukuda Sure thing! The label is attached.Thank you so much in advance for your help.

댓글을 달려면 로그인하십시오.

채택된 답변

Megumi Fukuda
Megumi Fukuda 2021년 6월 10일
indices = crossvalind('Kfold',Labels,k);
I am sure this does not work, as the cells in the variable Label are nested and cannot be used for crossvalind without converting the variables.
I have tested this code with MATLAB 2021a and this code looks fine. Would you like to try your code again?
% clear the workspace
load('Label.mat'); % the mat file you uploaded
Label_1 = cellfun(@(x) str2double(x), Giant_bpm, 'UniformOutput',false);
Label_double = [Label_1{:}];
indices = crossvalind('Kfold',Label_double,10);
(... and here is another implementation I came up with. This returns the label as char type)
load('Label.mat');
Giant_bpm_updated = cellfun(@cell2mat, Giant_bpm(:), 'UniformOutput', false);
indices = crossvalind('Kfold',Giant_bpm_updated,10);
Hope this helps!
  댓글 수: 1
Susan
Susan 2021년 6월 10일
Thanks for your response.
Interesting! when I run my code I get the same error as before but your code works fine.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by