Spliting ground truth data into 70% training, 20% validation and 10% Testingdata

2 次查看(过去 30 天)
Hi guys
i am looking for spliting data from ground truth file for following code
70% training, 20% validation and 10% Testingdata
load('gTruth.mat')
Fruits = selectLabels(gTruth,{'orange','Apple'});
if isfolder(fullfile('ExperimentData'))
cd ExperimentData
else
mkdir ExperimentData
end
addpath('ExperimentData');
splitFolder = true;
if splitFolder
valFolderName = "ExperimentData";
files = dir(fullfile(valFolderName));
N = length(files);
tf=randperm(N)>(0.20*N);
mkdir TrainingData;
mkdir ValidationData;
mkdir TestingData;
for i=3:length(tf)
files_re = files(i).name;
if tf(i) == 3
copyfile (fullfile(valFolderName,files_re),'TrainingData')
else
copyfile (fullfile(valFolderName,files_re),'Validation')
ifelse
copyfile (fullfile(valFolderName,files_re),'TestingData')
end
end
end

回答(1 个)

Shantanu Dixit
Shantanu Dixit 2024-12-27
Hi Abdussalam,
To split your ground truth data into training, validation, and testing sets, you can use the 'randperm' function to randomly shuffle the data and 'copyfile' to copy the data from the original data to the corresponding splits.
You can refer to the below code snippet to split data in 70:20:10 ratio
% Generate a random permutation of indices
% N = total data points
indices = randperm(N);
% Determine the number of files for each split
numTrain = round(0.7 * N);
numVal = round(0.2 * N);
numTest = N - numTrain - numVal;
mkdir('TrainingData');
mkdir('ValidationData');
mkdir('TestingData');
% Assuming ExperimentData contains the data files
for i = 1:numTrain
copyfile(fullfile('ExperimentData', files(indices(i)).name), 'TrainingData');
end
for i = numTrain+1:numTrain+numVal
copyfile(fullfile('ExperimentData', files(indices(i)).name), 'ValidationData');
end
for i = numTrain+numVal+1:N
copyfile(fullfile('ExperimentData', files(indices(i)).name), 'TestingData');
end
Additionally, you can refer to the following MathWorks documentation for more information:

类别

Help CenterFile Exchange 中查找有关 Deep Learning Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by