Split array into training and testing based on label ?

조회 수: 4 (최근 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.

채택된 답변

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개)

카테고리

Help CenterFile Exchange에서 Linear Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by