Clear Filters
Clear Filters

Crossvalidate cdataset for preparation

1 view (last 30 days)
Ethan
Ethan on 26 Nov 2022
Answered: Adithya on 25 May 2023
I have a cdataset containing reference to 124 images like this:
CroppedImageDatabase
18 27
124
+1 binary_faces/FacesImage#50.png
+1 binary_faces/FacesImage#51.png
+1 binary_faces/FacesImage#52.png
+1 binary_faces/FacesImage#53.png
+1 binary_faces/FacesImage#54.png
+1 binary_faces/FacesImage#55.png
+1 binary_faces/FacesImage#56.png
+1 binary_faces/FacesImage#57.png
+1 binary_faces/FacesImage#58.png
......
I was wondering how I can cross validate this set. I have seen answers referencing cvpartition(), crossvalind(), crossval(). I have attemped these but to no avail. Any help would be greatly appreciated. I am looking to cross validate the dataset to be used in a NN,KNN and SVM script ive prepared.
  1 Comment
Aditya
Aditya on 29 Nov 2022
Hi,
Could you please elaborate what your expectation is and why crossvalind not work ?

Sign in to comment.

Answers (1)

Adithya
Adithya on 25 May 2023
To cross-validate a cdataset in MATLAB, you can use the 'cvpartition' function to create a partitioning object, and then use it with the cross-validation functions provided by the Machine Learning Toolbox.
Here's an example of how you can cross-validate your cdataset:
% Assuming your cdataset is stored in the variable 'CroppedImageDatabase'
% Get the number of observations in the cdataset
numObservations = length(CroppedImageDatabase);
% Create a stratified partitioning object for cross-validation
% Set the 'Kfold' parameter to the desired number of folds (e.g., 5)
cv = cvpartition(numObservations, 'KFold', 5);
% Perform cross-validation
for fold = 1:cv.NumTestSets
% Get the training indices for the current fold
trainIndices = cv.training(fold);
% Get the testing indices for the current fold
testIndices = cv.test(fold);
% Get the training and testing subsets from the cdataset
trainSubset = CroppedImageDatabase(trainIndices, :);
testSubset = CroppedImageDatabase(testIndices, :);
% Perform your training and testing on the subsets
% Use trainSubset for training and testSubset for testing
% For example, train your NN, KNN, or SVM models here
% ...
% Your training and testing code goes here
% ...
% Print fold-specific results, accuracy, or any other metrics of interest
fprintf('Fold %d - Training Size: %d, Testing Size: %d\n', fold, numel(trainSubset), numel(testSubset));
% Display or record your metrics here
end
In the above example, the cdataset is partitioned using 'cvpartition' with a specified number of folds (in this case, 5). The 'cv.training' and 'cv.test' methods are used to obtain the training and testing indices for each fold. Then, you can extract the corresponding subsets from the cdataset and perform your training and testing on those subsets.
Make sure to replace the comment placeholders (% ...) with your actual training and testing code for your NN, KNN, or SVM models. You can calculate metrics such as accuracy, precision, recall, or F1 score based on your classification results and record them for each fold.
Note: This example assumes that your cdataset is properly formatted and contains the necessary information for training and testing your models. Adjust the code accordingly based on your specific cdataset structure and model requirements.
I hope this helps you cross-validate your cdataset. Let me know if you have any further questions!

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!