Splitting data using loop

2 ビュー (過去 30 日間)
SBS
SBS 2020 年 2 月 20 日
回答済み: Jalaj Gambhir 2020 年 2 月 25 日
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 件のコメント
SBS
SBS 2020 年 2 月 20 日
Any help please?

サインインしてコメントする。

回答 (1 件)

Jalaj Gambhir
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

カテゴリ

Help Center および File ExchangePreprocessing Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by