Hi Subhasmita,
In MATLAB, you can perform k-fold cross-validation to split your dataset into training and test sets. In k-fold cross-validation, the dataset is divided into k subsets (folds). The model is trained k times, each time using a different fold as the test set and the remaining folds as the training set.
Here's how you can perform 5-fold cross-validation in MATLAB:
cv = cvpartition(y, 'KFold', k);
trainIdx = training(cv, i);
model = fitcsvm(XTrain, yTrain);
predictions = predict(model, XTest);
accuracy(i) = sum(predictions == yTest) / length(yTest);
fprintf('Fold %d Accuracy: %.2f%%\n', i, accuracy(i) * 100);
averageAccuracy = mean(accuracy);
fprintf('Average Accuracy: %.2f%%\n', averageAccuracy * 100);
Explanation
- Data Loading: We use the fisheriris dataset for demonstration, where X contains the features and y contains the labels.
- Cross-Validation Partition: We create a 5-fold partition using cvpartition with the option 'KFold', k.
- Loop Through Folds: For each fold, we:
- Extract training and test indices.
- Split the data into training and test sets.
- Train a support vector machine (SVM) model using fitcsvm.
- Predict on the test set and calculate accuracy.
4. Accuracy Calculation: We calculate and print the accuracy for each fold and the average accuracy across all folds.
Additional Notes
- You can replace fitcsvm with any other classifier that suits your needs.
- Ensure that your dataset is suitable for cross-validation, especially regarding class balance.
- You might also want to explore MATLAB's crossval function, which automates some parts of this process.