Splitting data using loop
1 view (last 30 days)
Show older comments
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.
Answers (1)
Jalaj Gambhir
on 25 Feb 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 Comments
See Also
Categories
Find more on Hypothesis Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!