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

3 ビュー (過去 30 日間)
ANANDHI
ANANDHI 2013 年 10 月 11 日
回答済み: Sameer 2025 年 5 月 16 日
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,

回答 (1 件)

Sameer
Sameer 2025 年 5 月 16 日
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!

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by