Splitting data using loop
6 次查看(过去 30 天)
显示 更早的评论
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.
回答(1 个)
Jalaj Gambhir
2020-2-25
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Preprocessing Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!