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 CenterFile Exchange에서 Hypothesis Tests에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by