how to change crossvalind to cvpartition
1 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
采纳的回答
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 个评论
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Classification Ensembles 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!