Clear Filters
Clear Filters

Split array into training and testing

8 views (last 30 days)
Hi,
I have a set of data (DataA has 106x14). I want to split the rows into 2 section, one for training and one for testing. Here is my code:
[trainA,testA] = divideblock(DataA.', .7, .3); % 70% for training 30% for testing.
trainData = trainA.';
testData = testA.';
Result: but the total data I have after executing the code is only 93 (66x14 for traindata, 27x14 for testdata) I don't want to use valInd since I don't need it.
PLEASE correct me.
  1 Comment
Cyrus
Cyrus on 21 Dec 2016
I would manually find the 70 and 30 % and use "for loop" to split the data

Sign in to comment.

Accepted Answer

Jos (10584)
Jos (10584) on 21 Dec 2016
dataA = cumsum(ones(20,3)) % some test data
p = .7 % proportion of rows to select for training
N = size(dataA,1) % total number of rows
tf = false(N,1) % create logical index vector
tf(1:round(p*N)) = true
tf = tf(randperm(N)) % randomise order
dataTraining = dataA(tf,:)
dataTesting = dataA(~tf,:)
  1 Comment
Ihsan Yassin
Ihsan Yassin on 22 Dec 2016
Edited: Stephen23 on 22 Dec 2016
Thanks, it works exactly like I want. but may I ask another question,
tf = false(N,1) % create logical index vector
tf(1:round(p*N)) = true
tf = tf(randperm(N)) % randomise order
what are these for, I don't quite understand. Thanks again for your help.

Sign in to comment.

More Answers (5)

Greg Heath
Greg Heath on 22 Dec 2016
Edited: Greg Heath on 22 Dec 2016
Your answer should be simply obtained from the divideblock documentation (help and/or doc). From the help documentation example
>> clear all, help divideblock
[trainInd,valInd,testInd] = divideblock(250,0.7,0.15,0.15);
whos
Name Size Bytes Class
testInd 1x37 296 double
trainInd 1x176 1408 double
valInd 1x37 296 double
>> [ 176 37 37 ]/250
ans =
0.7040 0.1480 0.1480
However, DIVIDEBLOCK (MATLAB 2016A) HAS A BUG
>> clear all, clc
[trainInd,valInd,testInd] = divideblock(250,0.7,0.0,0.3);
whos
Subscript indices must either be real positive integers or logicals
Error in divideblock>divide_indices (line 108)
testInd = (1:numTest)+valInd(end);
Error in divideblock (line 65)
[out1,out2,out3] = divide_indices(in1,params);
Hope this helps.
Thank you for formally accepting my answer
Greg
P.S. What version of MATLAB do you have?
  1 Comment
Ihsan Yassin
Ihsan Yassin on 23 Dec 2016
I have 2015 ver..yes I get those error when i wrote that, as I don't want to use that valInd

Sign in to comment.


Jaeseok Kim
Jaeseok Kim on 13 Nov 2017
dividerand is what you want...

Satyam Agarwal
Satyam Agarwal on 5 Aug 2018
Edited: Satyam Agarwal on 5 Aug 2018
[Trainset,Testset]= splitEachLabel(datastore,p)
p is ratio 0<p<1

MUHAMMAD SAJAD
MUHAMMAD SAJAD on 3 Sep 2018
% Split 60% of the files from each label into ds60 and the rest into dsRest [ds60,dsRest] = splitEachLabel(imds,0.6) ds60 is a trainingset while dsRest is testset. we can also divide it for validset. like this [TrianSet,ValidSet,TestSet]=splitEachLabel(DataStore,0.7,0.2). In this case 70% of files split for TrainingSet,20% for ValidSet and the remaining for TestSet.

indhumathi karuppaiya
indhumathi karuppaiya on 2 Jun 2020
hi my name indhu i try to do project for my studies .i have choosed parkinson diease speech recognition in matlab coding how to split the data to train data and test data please let me know just i want use only 1to 60 patiend data onlu use thank u

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!