how to change crossvalind to cvpartition

조회 수: 7 (최근 30일)
kav
kav 2019년 11월 8일
댓글: Anmol Dhiman 2019년 11월 13일
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);

채택된 답변

Anmol Dhiman
Anmol Dhiman 2019년 11월 11일
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
  댓글 수: 4
kav
kav 2019년 11월 13일
Thank you Anmol.
Program is running from long time, to make it quicker what changes should i make it?
is it with number of trees?
Anmol Dhiman
Anmol Dhiman 2019년 11월 13일
You may change the number of tree to check the accuracies that you are getting, besides it will take less time for getting the results if you use less number of trees. Also you may try using parfor instead of for loop for decreasing the total time.

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by