R/selectFeaturesRandom.R

Defines functions createStates selectFeaturesRandom

selectFeaturesRandom = function(learner, task, resampling, measures, bit.names, bits.to.features,
  control, opt.path, show.info) {
  states = lapply(seq_len(control$maxit), function(i) {
    createStates(n = length(bit.names),
      max.features = control$max.features, prob = control$extra.args$prob)
  })
  evalOptimizationStatesFeatSel(learner, task, resampling, measures, bits.to.features,
    control, opt.path, show.info, states, 1L, NA_integer_)
  makeFeatSelResultFromOptPath(learner, measures, resampling, control, opt.path, task = task, bits.to.features = bits.to.features)
}

# help function in order to respect max.features

createStates = function(n, max.features, prob) {
  if (is.na(max.features)) {
    return(rbinom(n, 1, prob))
  }
  run.loop = TRUE
  while (run.loop) {
    x = rbinom(n, 1, prob)
    if (sum(x) <= max.features) {
      run.loop = FALSE
    }
  }
  return(x)
}
mlr-org/mlr documentation built on Jan. 12, 2023, 5:16 a.m.