Generate a set of random unique integers from an interval

后端 未结 3 1570
离开以前
离开以前 2021-02-03 21:31

I am trying to build some machine learning models,

so i need a training data and a validation data

so suppose I have N number of examples, I want to select rando

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-03 22:16

    If I understand correctly, you are trying to create a hold-out sampling. This is usually done using probabilities. So if you have n.rows samples and want a fraction of training.fraction to be used for training, you may 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 specify EXACT number of training cases, you may 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"
    

提交回复
热议问题