mosmafs (version 0.1.2)

makeObjective: Create ecr Objective Function

Description

Creates an objective function that resamples learner on task with resampling and measures measure (optional), together with the number of features selected. If measure needs to be maximized, it is multiplied by -1 to make it a minimization task.

The ParamSet used to generate individuals for the ecr must include parameters for learner, not a logical parameter with length equal to getTaskNFeats(task) for feature selection, as it is automatically added named as selector.selection. It can be accessed via getParamSet() with the object created by makeObjective() as input.

learner must not include a cpoSelector() applied to it, this happens automatically within makeObjective.

Usage

makeObjective(
  learner,
  task,
  ps,
  resampling,
  measure = NULL,
  holdout.data = NULL,
  worst.measure = NULL,
  cpo = NULLCPO
)

Value

function an objective function for ecr::ecr.

Arguments

learner

[Learner] A Learner object to optimize.

task

[Task] The mlr::Task object to optimize on.

ps

[ParamSet] The ParamSet to optimize over, only parameters of the actual learner.

resampling

[ResampleDesc | ResampleInst | function] The ResampleDesc or ResampleInst object to use. This may be a function numeric(1) -> ResampleDesc/ResampleInst which maps fidelity to the resampling to use. If this is used, then the resampling should be chosen such that an average value, weighted by fidelity, makes sense. For example, the function could map an integer to a corresponding number of resampling folds or repetitions.

measure

[Measure | NULL] The Measure to optimize for. The default is NULL, which uses the task's default Measure. If measure needs to be maximized, the measure is multiplied by -1, to make it a minimization task.

holdout.data

[Task] Additional data on which to predict each configuration after training on task.

worst.measure

[numeric(1)] worst value for measure to consider, for dominated hypervolume calculation. Will be extracted from the given measure if not given, but will raise an error if the extracted (or given) value is infinite. Measure is multiplied by -1, if measure needs to be maximized.

cpo

[CPO] CPO pipeline to apply before feature selection. (A CPO that should be applied after feature selection should already be part of learner when given). Care should be taken that the selector.selection parameter in ps has the appropriate length of the data that cpo emits.

Examples

Run this code
library("mlr")
library("rpart")

task.whole <- bh.task
rows.whole <- sample(nrow(getTaskData(task.whole)))
task <- subsetTask(task.whole, rows.whole[1:250])
task.hout <- subsetTask(task.whole, rows.whole[251])
lrn <- makeLearner("regr.rpart")

ps.simple <- mlrCPO::pSS(
  maxdepth: integer[1, 30],
  minsplit: integer[2, 30],
  cp: numeric[0.001, 0.999])
  nRes <- function(n) {
  makeResampleDesc("Subsample", split = 0.9, iters = n)
}

fitness.fun.mos <- makeObjective(lrn, task, ps.simple, nRes,
  measure = mse,
  holdout.data = task.hout, worst.measure = 100)

# extract param set from objective
ps.obj  <- getParamSet(fitness.fun.mos)
getParamIds(ps.obj) # automatically added parameter ' for selecting features

exp <- sampleValue(ps.obj)
res <- fitness.fun.mos(exp, fidelity = 2, holdout = FALSE)


Run the code above in your browser using DataCamp Workspace