File: selectFeaturesRandom.R

package info (click to toggle)
r-cran-mlr 2.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,264 kB
  • sloc: ansic: 65; sh: 13; makefile: 5
file content (26 lines) | stat: -rw-r--r-- 898 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)
}