# How to perform stratified 10 fold cross validation for classification in MATLAB?

24 visualizzazioni (ultimi 30 giorni)
Commentato: uma il 9 Mag 2022
My implementation of usual K-fold cross-validation is pretty much like:
K = 10;
CrossValIndices = crossvalind('Kfold', size(B,2), K);
for i = 1: K
display(['Cross validation, folds ' num2str(i)])
IndicesI = CrossValIndices==i;
TempInd = CrossValIndices;
TempInd(IndicesI) = [];
xTraining = B(:, CrossValIndices~=i);
tTrain = T_new1(:, CrossValIndices~=i);
xTest = B(:, CrossValIndices ==i);
tTest = T_new1(:, CrossValIndices ==i);
end
But To ensure that the training, testing, and validating dataset have similar proportions of classes (e.g., 20 classes).I want use stratified sampling technique.Basic purpose is to avoid class imbalance problem.I know about SMOTE technique but i want to apply this one.
##### 3 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
uma il 9 Mag 2022
what is T_new1

Accedi per commentare.

### Risposta accettata

Tom Lane il 25 Lug 2017
If you have the Statistics and Machine Learning Toolbox, consider the cvpartition function. It can define stratified samples.
##### 3 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Olympia Gallou il 6 Mag 2021
How did you solve your problem?

Accedi per commentare.

### Più risposte (1)

ashik khan il 18 Nov 2018
What are the value of B and T_new1 ??
K = 10;
CrossValIndices = crossvalind('Kfold', size(B,2), K);
for i = 1: K
display(['Cross validation, folds ' num2str(i)])
IndicesI = CrossValIndices==i;
TempInd = CrossValIndices;
TempInd(IndicesI) = [];
xTraining = B(:, CrossValIndices~=i);
tTrain = T_new1(:, CrossValIndices~=i);
xTest = B(:, CrossValIndices ==i);
tTest = T_new1(:, CrossValIndices ==i);
end
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Language Support in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!