Generate a set of random unique integers from the interval

I am trying to create some machine learning models,

therefore I need training data and verification data

so suppose I have N examples, I want to select random x-examples in a data frame.

For example, suppose I have 100 examples and I need 10 random numbers, is there a way (to efficiently) generate 10 INTEGER random numbers for me to extract training data from my sample data?

I tried using a while loop and slowly changing repeating numbers, but the running time is not very perfect, so I'm looking for a more efficient way to do this.

Can anyone help?

+18
source share
3 answers

sample (or sample.int ) does this:

 sample.int(100, 10) # [1] 58 83 54 68 53 4 71 11 75 90 

will generate ten random numbers from the range of 1–100. You probably want replace = TRUE , which are some examples with replacement:

 sample.int(20, 10, replace = TRUE) # [1] 10 2 11 13 9 9 3 13 3 17 

More generally, sample sample of n observations from a vector of arbitrary values.

+27
source

If I understand correctly, you are trying to create a test sample. This is usually done using probabilities. Therefore, if you have n.rows samples and you want the training.fraction part of the training.fraction be used for training, you can do something like this:

 select.training <- runif(n=n.rows) < training.fraction data.training <- my.data[select.training, ] data.testing <- my.data[!select.training, ] 

If you want to tell EXACT the number of training cases, you can do something like:

 indices.training <- sample(x=seq(n.rows), size=training.size, replace=FALSE) #replace=FALSE makes sure the indices are unique data.training <- my.data[indices.training, ] data.testing <- my.data[-indices.training, ] #note that index negation means "take everything except for those" 
+1
source

from a raster package:

raster :: sampleInt (242, 10, replace = F) [1] 95 230 148 183 38 98 137 110 188 39

sampleInt (1e + 12, 10)

'this may fail:

'sample.int (1e + 12, 10)

'sample.int (1e + 9, 10)

0
source

Source: https://habr.com/ru/post/949930/


All Articles