Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
Saadia Ali
Saadia Ali le 8 Août 2022
Réponse apportée : Rik le 8 Août 2022
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!

Réponses (2)

Dnyanesh Suryavanshee
Dnyanesh Suryavanshee le 8 Août 2022
data = 2000;
per = 20;
ans=data(per/100);

Rik
Rik le 8 Août 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!

Translated by