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!