Splitting data using loop
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello, I have data of 60users(each user data has different no. Of rows but same no. Of columns)and I have to split each user data into training and test set in 60,40% randomly and then combine all training data(of 60 users)in one matrix and all test data(of 60 users)in another matrix. I am using dividerand to split for each user. Can anyone please suggest how to use loop to do this task efficiently?
Thank you.
Respuestas (1)
Jalaj Gambhir
el 25 de Feb. de 2020
Hi,
This can be achieved using findgroups and splitapply. For the example given below, I have used fisherirs dataset, which contains 3 categories of flowers (can be extended to 60 users) and 50 samples for each category in the dataset. These 50 samples have been split to 60% train and 40% test.
Hope this helps!
load fisheriris;
groups = findgroups(species);
func = @(x) {x};
grouped_data = splitapply(func, meas, groups);
train_data = [];
test_data = [];
for category = 1:length(grouped_data)
train_percent = 0.6
[rows,col] = size(grouped_data{category});
idx = randperm(rows);
training_i = grouped_data{category}(idx(1:round(train_percent*rows)),:);
testing_i = grouped_data{category}(idx(round(train_percent*rows)+1:end),:);
train_data = vertcat(train_data,training_i);
test_data = vertcat(test_data,testing_i);
end
0 comentarios
Ver también
Categorías
Más información sobre Hypothesis Tests en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!