Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mlrCPO-任务转换为任务_R_Data Processing_Mlr - Fatal编程技术网

mlrCPO-任务转换为任务

mlrCPO-任务转换为任务,r,data-processing,mlr,R,Data Processing,Mlr,我想为mlr::makeClassificationViaRegression包装器构建一个CPO。包装器构建回归模型,预测正类的特定示例是否属于它(1)(-1)。它还使用softmax计算预测概率 在阅读了和formakeCPOTargetOp之后,我的尝试如下: cpoClassifViaRegr = makeCPOTargetOp( cpo.name = 'ClassifViaRegr', dataformat = 'task', #Not sure - will this wo

我想为
mlr::makeClassificationViaRegression
包装器构建一个CPO。包装器构建回归模型,预测正类的特定示例是否属于它(1)(-1)。它还使用softmax计算预测概率

在阅读了和for
makeCPOTargetOp
之后,我的尝试如下:

cpoClassifViaRegr = makeCPOTargetOp(
  cpo.name = 'ClassifViaRegr', 
  dataformat = 'task', #Not sure - will this work if input is df with unknown target values?
  # properties.data = c('numerics', 'factors', 'ordered', 'missings'), #Is this needed?
  properties.adding = 'twoclass', #See https://mlrcpo.mlr-org.com/articles/a_4_custom_CPOs.html#task-type-and-conversion
  properties.needed = character(0),
  properties.target = c('classif', 'twoclass'), 
  task.type.out = 'regr',
  predict.type.map = c(response = 'response', prob = 'response'), 
  constant.invert = TRUE, 
  cpo.train = function(data, target) {
    getTaskDesc(data)
  }, 
  cpo.retrafo = function(data, target, control) {
    cat(class(target))
    td = getTaskData(target, target.extra = T)
    target.name = paste0(control$positive, ".prob")

    data = td$data
    data[[target.name]] = ifelse(td$target == pos, 1, -1)

    makeRegrTask(id = paste0(getTaskId(target), control$positive, '.'), 
                 data = data,
                 target = target.name,
                 weights = target$weights,
                 blocking = target$blocking)

  }, 
  cpo.train.invert = NULL, #Since constant.invert = T
  cpo.invert = function(target, control.invert, predict.type) {


    if(predict.type == 'response') {

      factor(ifelse(target > 0, control.invert$positive, control.invert$positive))

    } else {

      levs = c(control.invert$positive, control.invert$negative)
      propVectorToMatrix(vnapply(target, function(x) exp(x) / sum(exp(x))), levs)

    }

  })
它似乎按预期工作,下面的演示显示反向预测与使用
makeClassificationViaRegr
包装器获得的预测相同:

lrn=makeLearner(“regr.lm”)
#包装纸-----------------------------------------------------------------
lrn2=通过回归包装器(lrn)进行分类
模型=列车(lrn2,sonar.task,子集=1:140)
预测=预测(model,newdata=getTaskData(sonar.task)[141:208,1:60])
#CPO---------------------------------------------------------------------
sonar.train=水下任务(sonar.task,1:140)
sonar.test=水下任务(sonar.task,141:208)
trafd=sonar.train%>>%cpoClassifViaRegr()
mod=列车(lrn、trafd)
retr=sonar.test%>>%retrafo(交通量)
pred=预测(mod,retr)
invpred=逆变器(逆变器(retr),pred)
相同(预测$data$response,invpred$data$response)