How to manually construct or modify a cross-validation object in MATLAB?
이전 댓글 표시
Short question:
I want your help to manually construct or modify Matlab cross-validation (CV) object created using function cvpartition when it's known which observations belong to training and which to validation partitions.
Extended question:
I have a kind of specific dataset in which several observations (each represented by a separate row) belong to the same object of investigation (e.g. measurements of the same object were performed several times). Each object (and it's observation) belongs to a certain class (e.g. yellow object, green object, etc.) as well. The problem is this: it's needed a CV object in which all observations of the same object belong to the same (either training or validation) partition as a block of data (unfortunately I'm not sure how to implement this) and the data would be distributed to partitions in stratified way (luckily, this is implemented in function cvpartition). I tried to access indices of CV object (cvo.indices) but they are locked. Here cvo is:
cvType = 'kfold';
nPartitions = 5;
cvo = cvpartition(my_classes,cvType,nPartitions);
Your insights and ideas how to construct or modify cvo manually are welcomed.
댓글 수: 2
Jan
2018년 12월 20일
This solution no longer works - was there a concurrent patch that provided a workaround?
@James: This is the question. Which solution do you mean?
Comment posted as flag by patrck rich:
the solution is no longer working in recent versions
That is always a risk when trying to modify internal files. However, I ran the lines below in the online interface (I cleared the output, as I don't expect Mathworks would be happy with me sharing their files..)
%type(which('cvpartition'))
OS={'Windows','macOS','Linux'};fprintf('This is version %s, running on %s.',version,OS{[ispc ismac isunix&&~ismac]})
As far as I can judge from the code I can read, I don't see a reason why it wouldn't still work, but feel free to post a comment explaining which exact steps you took and which lines you modified.
채택된 답변
추가 답변 (3개)
Jo
2016년 11월 4일
5 개 추천
Hi,
It seems that one year later your solution is not working anymore, maybe they changed the function.
Indeed, I tried to modify as you suggested "properties(GetAccess = 'public', SetAccess = 'public')", but it only allows me to access to the number of observations and not the indices. Indices are created by "methods" defined in cvpartition.m (test, training). And I do not understand how to change these to choose my own indices.
Maybe if you have progessed in all that since you first posted your question.... I would be really greatful to have some help in there.
Using random crossvalidation is of none interest for me, I need to choose the block myself to have a more robust solution. Thanks to anyone that can help me!!!
댓글 수: 3
Albert Sama
2017년 11월 2일
I have done a similar modification on the source code that works on MATLAB R2017b.
Basically, the file cvpartitionImpl.m (at ...\toolbox\stats\stats\+internal\+stats) has to be modified as the first answer specifies, making indices public:
properties(GetAccess = public, SetAccess = public)
indices = [];
Group = [];
holdoutT = [];
end
This way, indices are updateable, for instance:
cvp = cvpartition(500,'KFold',9);
cvp.Impl.indices=cvp.Impl.indices(end:-1:1);
Evelyn Tang
2018년 11월 9일
This is wonderful and just what I need, thank you so much! Works perfectly :)
Alex Ryabov
2024년 5월 30일
This solution works for me also, maltab 2023a.
In windows I had to run Notepad as admin to be able to edit the file
Roberto Herrera-Lara
2015년 4월 14일
0 개 추천
try to do this https://chrisjmccormick.wordpress.com/2013/07/31/k-fold-cross-validation-with-matlab-code/
댓글 수: 1
Vilmantas Gegzna
2015년 4월 22일
편집: Vilmantas Gegzna
2015년 4월 22일
John Smith
2018년 11월 21일
0 개 추천
Two small additions:
1.You need to update the toolbox cache https://www.mathworks.com/help/matlab/matlab_env/toolbox-path-caching-in-the-matlab-program.html
2.Restart Matlab after that
카테고리
도움말 센터 및 File Exchange에서 Language Support에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!