MATLAB Answers

Split array into training and testing based on label ?

24 ビュー (過去 30 日間)
Mohammed odwan
Mohammed odwan 2020 年 8 月 7 日
回答済み: Sudheer Bhimireddy 2020 年 8 月 7 日
I have 500*4 array and the colum 4 contane the labels.The labels are 1,2,3,4. How can split the array to train data =70% form each label and the test data is the rest of data.
Thanks in advance.

  0 件のコメント



Sudheer Bhimireddy
Sudheer Bhimireddy 2020 年 8 月 7 日
Try this:
A = rand(500,4);
labels = randi([1,4],500,1);
A(:,4) = labels;
% Filter data based on labels
A_1 = A(A(:,4)==1,:);
A_2 = A(A(:,4)==2,:);
A_3 = A(A(:,4)==3,:);
A_4 = A(A(:,4)==4,:);
% Training and test data for first label
% Generate training indices
train_ind = sort(randperm(size(A_1,1),ceil(0.7*size(A_1,1))));
% Get the remaining indices for testing dataset
test_ind = 1:size(A_1,1);
test_ind(train_ind) = [];
A_train_1 = A_1(train_ind,:);
A_test_1 = A_1(test_ind,:);
% Repeat the same for other labels
Change 0.7 in the train_ind line to whatever percent data you would like for training set.
Hope this helps.

  0 件のコメント


その他の回答 (0 件)


Community Treasure Hunt

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

Start Hunting!

Translated by