Split array into training and testing based on label ?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mohammed odwan
le 7 Août 2020
Réponse apportée : Sudheer Bhimireddy
le 7 Août 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 commentaires
Réponse acceptée
Sudheer Bhimireddy
le 7 Août 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Linear Regression dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!