how to evaluate SVM classifier using 10-fold cross validation method

3 views (last 30 days)
how to calculate accuracy,specificity and sensitivity of SVM classifier using 10-fold cross validation method. plz can any one explain this with matlab code. Thank you,

Answers (1)

Sameer
Sameer on 16 May 2025
To calculate accuracy, sensitivity, and specificity of an SVM classifier using 10-fold cross-validation :
1. Split your data into 10 folds.
2. For each fold:
  • Train the SVM on 9 folds, test on the 1 remaining fold.
  • Collect predictions and compare with true labels.
3. After all folds:
  • Sum up true positives (TP), true negatives (TN), false positives (FP), and false negatives (FN).
  • Calculate: Accuracy: ((TP+TN)/(TP+TN+FP+FN)) , Sensitivity: (TP/(TP+FN)), Specificity: (TN/(TN+FP))
Sample Example:
cv = cvpartition(Y, 'KFold', 10);
TP = 0; TN = 0; FP = 0; FN = 0;
for i = 1:cv.NumTestSets
trainIdx = cv.training(i);
testIdx = cv.test(i);
SVMModel = fitcsvm(X(trainIdx,:), Y(trainIdx));
Ypred = predict(SVMModel, X(testIdx,:));
Ytest = Y(testIdx);
TP = TP + sum((Ytest == 1) & (Ypred == 1));
TN = TN + sum((Ytest == 0) & (Ypred == 0));
FP = FP + sum((Ytest == 0) & (Ypred == 1));
FN = FN + sum((Ytest == 1) & (Ypred == 0));
end
accuracy = (TP + TN) / (TP + TN + FP + FN);
sensitivity = TP / (TP + FN);
specificity = TN / (TN + FP);
fprintf('Accuracy: %.2f%%\n', accuracy*100);
fprintf('Sensitivity: %.2f%%\n', sensitivity*100);
fprintf('Specificity: %.2f%%\n', specificity*100);
For more information, Please go through the following MathWorks documentation links:
Hope this helps!

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!