how to change crossvalind to cvpartition

7 visualizaciones (últimos 30 días)
kav
kav el 8 de Nov. de 2019
Comentada: Anmol Dhiman el 13 de Nov. de 2019
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);

Respuesta aceptada

Anmol Dhiman
Anmol Dhiman el 11 de Nov. de 2019
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 comentarios
kav
kav el 13 de Nov. de 2019
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 el 13 de Nov. de 2019
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.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by