# Partition of data based on percentages (for cross-validation)

24 views (last 30 days)

Show older comments

Hi all,

I have a matrix A made of x rows and y columns.

I would like to take 80% of my matrix A based on the number of rows, and do so 5 times, so as to equally partition my data set. So A1 would be the first 80% of the rows (and all columns), etc.

I had a look at this article https://uk.mathworks.com/help/nnet/ug/divide-data-for-optimal-neural-network-training.html but I am not sure any of these functions does what I want.

Please could anyone help me?

Thanks a lot

##### 0 Comments

### Answers (4)

Walter Roberson
on 1 Jul 2017

If you are using a neural network, then you would configure net.divideFcn to dividerand() (the default) and set net.divideParam to the percentages you want.

Otherwise, use

nrow = size(A,1);

ntrain = floor(nrow * 80/100);

train_ind = randperm(nrow, ntrain);

train_rows = A(train_ind, :);

##### 10 Comments

MA-Winlab
on 3 May 2019

Yes @ Walter Roberson, thank you for the note. M should be between within the rang [0 1].

One more question:

With HoldOut, we do not have a loop like the one with k-fold! but how to do cross validation?

I mean, with HoldOut, we do not repeat the partition and traing a nd testing for k times as with k-fold!

I tried it in a loop of k=5 and kept checking cp.CorrectRate and cp.LastCorrectRate. They were the same, i.e cp.CorrectRate is not rolling.

Please correct me if I mistaken.

I am saying this because I read this

Using this method within a loop is similar to using K-fold cross-validation one time outside the loop, except that nondisjointed subsets are assigned to each evaluation.

This mote is from here

Greg Heath
on 2 Jul 2017

Edited: Greg Heath
on 25 Jul 2017

1. NOTE: Contrary to most statistical regression subroutines, MATLAB Neural Network subroutines operate on COLUMN VECTORS!

2. For N O-dimensional "O"utput target vectors corresponding to N I-dimensional "I"nput vectors:

[ I N ] = size(input)

[ O N ] = size(target)

3. Correspondingly, the data in the MATLAB NN database is stored columnwise. See the results of the keyboard commands

help nndatabase

doc nndatabase

4. The MATLAB NN Toolbox DEFAULT data division ratio is 0.7/0.15/0.15 with

Ntst = floor(0.15*N)

Nval = Ntst

Ntrn = N - Nval -Ntst

4. Instead of TRYING to evenly divide the data for m-fold crossvalidation, it is far easier to just use Ntrials designs with RANDOM datadivision AND RANDOM initial weights.

5. I gave up the nitpicking index considerations of worrying about the number of times each data point was in each of the trn/val/tst subsets. If you have concerns, just increase Ntrials!

6. Somewhere in several of my NEWSGROUP and/or ANSWERS posts, I did use nitpicking XVAL index considerations. Good Luck if you want to find some. I would first search using XVAL.

Hope this helps.

Thak you for formally accepting my answer

Greg

##### 0 Comments

Lulu Dulac
on 2 Jul 2017

##### 4 Comments

Walter Roberson
on 3 Jul 2017

"I don't have NN toolbox"

Then that was the operative limitation, not the fact that you are not using R2017a.

In my Answer I posted code for row-wise random division without any toolboxes. I did use a syntax of randperm that did not become available until R2011a.

ranjana roy chowdhury
on 14 Jul 2019

##### 2 Comments

ranjana roy chowdhury
on 15 Jul 2019

### See Also

### Community Treasure Hunt

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

Start Hunting!