Any help plz???

# how to divide a data set randomly into training and testing data set?

1,134 views (last 30 days)

Show older comments

Hello guys, I have a dataset of a matrix of size 399*6 type double and I want to divide it randomly into 2 subsets training and testing sets by using the cross-validation.

i have tried this code but did get what i want https://www.mathworks.com/help/stats/cvpartition-class.html

Could anyone help me to do that?

Expected outputs:

training_data: k*6 double

testing_data: l*6 double

### Accepted Answer

### More Answers (7)

ALDO
on 2 Feb 2020

you can use The helper function 'helperRandomSplit', It performs the random split. helperRandomSplit accepts the desired split percentage for the training data and Data. The helperRandomSplit function outputs two data sets along with a set of labels for each. Each row of trainData and testData is an signal. Each element of trainLabels and testLabels contains the class label for the corresponding row of the data matrices.

percent_train = 70;

[trainData,testData,trainLabels,testLabels] = ...

helperRandomSplit(percent_train,Data);

make sure to have the proper toolbox to use it.

##### 1 Comment

Jeremy Breytenbach
on 24 May 2019

Edited: Jeremy Breytenbach
on 24 May 2019

Hi there.

If you have the Deep Learning toolbox, you can use the function dividerand: https://www.mathworks.com/help/deeplearning/ref/dividerand.html

[trainInd,valInd,testInd] = dividerand(Q,trainRatio,valRatio,testRatio) separates targets into three sets: training, validation, and testing.

Pramod Hullole
on 5 Mar 2019

hello sir,

iI'm new to the neuralnetworks..now i am working on my projects which is leaf disease detections using image processing. i am done with feature extraction and now not getting what is the next step..i know that i should apply nn and divide it in training and testing data set.. but in practically how to procced that's what i am not getting .please help me through this... please send steps..each steps in details. .

##### 1 Comment

Savas Yaguzluk
on 8 Mar 2019

Dear Pramod,

Open a new topic and ask your question there. So, people can see your topic title and help you.

Hossein Amini
on 15 Jul 2019

##### 0 Comments

Hossein Amini
on 15 Jul 2019

[z,r] = size(X);

idx = randperm(z);

TrainX = (X(idx(1:round(Ptrain.*z)),:))';

TrainY = (Y(idx(1:round(Ptrain.*z)),:))';

TestX = (X(idx(round(Ptrain.*z)+1:end),:))';

TestY = (Y(idx(round(Ptrain.*z)+1:end),:))';

If I'm not mistaken, in newrb doc, the size of input data and output data should be same like (4x266 and 1x266), that's why I transposed that matrixes. But the error which I got is specifying zeros matrix. I don't know how to prepare that.

##### 0 Comments

ranjana roy chowdhury
on 15 Jul 2019

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!