Split array into training and testing based on label ?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mohammed odwan
am 7 Aug. 2020
Beantwortet: Sudheer Bhimireddy
am 7 Aug. 2020
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 Kommentare
Akzeptierte Antwort
Sudheer Bhimireddy
am 7 Aug. 2020
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Linear Regression finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!