MATLAB: How to change crossvalind to cvpartition

crossvalidationcrossvalind

Hello,
What changes should i make to below code, to use cvpartition for cross validation?
TREES = [2 4 6 8 10 20 40:20:80 100:50:300 400 500];
FEATURES = [1:size(X1,2)]; % Breiman's rule: round(sqrt(size(X, 2)),0)
grid_AUC_crossval = zeros(length(TREES), length(FEATURES)); % to store train AUC scores
grid_F1_crossval = zeros(length(TREES), length(FEATURES));
for t=1:length(TREES)
for f=1:length(FEATURES)
trees = TREES(t);
features = FEATURES(f);
% run cross-validation on every model iteration
numFolds = 10;
Indices = crossvalind('Kfold', y1, numFolds);
final_preds = [];
final_scores = [];
yT = [];
for i = 1:numFolds
X2_fold = X1(Indices == i, :);
X1_fold = X1(Indices ~= i, :);
y1_fold = y1(Indices ~= i, :);
testIdx = (Indices == i); % index numbers of test items
Mdl = TreeBagger(trees, X1_fold, y1_fold, 'NumPredictorsToSample', features,'MinLeafSize', 5, 'Method', 'classification');
[preds, scores] = predict(Mdl, X2_fold);

Best Answer

cvpartition is used for creating cross validation partition of the data.
You can use in the method as shown below
TREES = [2 4 6 8 10 20 40:20:80 100:50:300 400 500];
FEATURES = [1:size(X1,2)]; % Breiman's rule: round(sqrt(size(X, 2)),0)
X1 = meas;
grid_AUC_crossval = zeros(length(TREES), length(FEATURES)); % to store train AUC scores
grid_F1_crossval = zeros(length(TREES), length(FEATURES));
y1 = species;
for t=1:length(TREES)
for f=1:length(FEATURES)
trees = TREES(t);
features = FEATURES(f);
% run cross-validation on every model iteration
numFolds = 10;
c = cvpartition(y1,'KFold',numFolds);
final_preds = [];
final_scores = [];
yT = [];
for i = 1:numFolds
idx = training(c,i); % get indices for all trainings data
testIdx = (idx ~= i); % index numbers of test items
X2_fold = X1(idx ~= i, :);
X1_fold = X1(idx == i, :);
y1_fold = y1(idx == i, :);
Mdl = TreeBagger(trees, X1_fold, y1_fold, 'NumPredictorsToSample', features,'MinLeafSize', 5, 'Method', 'classification');
[preds, scores] = predict(Mdl, X2_fold);
end
end
end
Related Question