How can I select 20% of a given data set for training?

4 views (last 30 days)
I have a data set that contains 2000 data points
and I would like to select (randomly) 20% of the data for training. How would i do that?
And suppose i wanted the percentage to be a variable? So i can sample any percentage i define?
Thank you!

Answers (2)

Dnyanesh Suryavanshee
Dnyanesh Suryavanshee on 8 Aug 2022
data = 2000;
per = 20;
ans=data(per/100);

Rik
Rik on 8 Aug 2022
If you want the sample to be repeatable, you will need to specify the state of the random number generator by calling the rng function.
p = randperm(n,k); %returns a row vector containing k unique integers selected randomly from 1 to n.
To calculate k as a function of n and the percentage, you can use the normal arithmatic:
k=round(n*perc/100);
You need to round (or use floor or ceil), because the inputs to randperm must be integer.

Tags

Community Treasure Hunt

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

Start Hunting!