r语言 - MLR 重采样为多标签分类创建单类问题



我正在尝试使用交叉验证来衡量某些 MLR 分类器的多标签分类性能

我尝试使用 MLR resample方法或传递我自己的子集,但是在这两种情况下都会抛出错误(据我所知,当用于训练的子集仅包含某些标签的单个值时,就会发生错误(

下面是发生此问题的小示例:

learner = mlr::makeLearner("classif.logreg")
learner = makeMultilabelClassifierChainsWrapper(learner)
data = data.frame(
    attr1 = c(1, 2, 2, 1, 2, 1, 2),
    attr2 = c(2, 1, 2, 2, 1, 2, 1),
    lab1 = c(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE),
    lab2 = c(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE))
task = mlr::makeMultilabelTask(data=data, target=c('lab1', 'lab2'))

以下是两种方式,有两种方式:

1.

rDesc = makeResampleDesc("CV", iters = 3)
resample(learner, task, rDesc)

阿拉伯数字。

model = mlr::train(learner, task, subset=c(TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE))

错误消息:

checkLearnerBeforeTrain(task, learner, weights(中的错误:任务"lab1"是一个单类问题,但学习者"classif.logreg"不支持!

由于 MLR 中没有支持单类 (https://mlr.mlr-org.com/articles/tutorial/integrated_learners.html( 分类的学习器,拆分数据可能需要太多大惊小怪(特别是对于像 reutersk500 这样的数据集(,我为双类学习器创建了一个包装器,如果给定具有单个目标类的任务,将始终仅返回此类值,对于更多类将使用包装的学习器:

(此代码将成为存储库 https://github.com/lychanl/ChainsOfClassification 的一部分(

makeOneClassWrapper = function(learner) {
    learner = checkLearner(learner, type='classif')
    id = paste("classif.oneClassWrapper", getLearnerId(learner), sep = ".")
    packs = getLearnerPackages(learner)
    type = getLearnerType(learner)
    x = mlr::makeBaseWrapper(id, type, learner, packs, makeParamSet(),
        learner.subclass = c("OneClassWrapper"),
        model.subclass = c("OneClassWrapperModel"))
    x$type = "classif"
    x$properties = c(learner$properties, 'oneclass')
    return(x)
}
trainLearner.OneClassWrapper = function(.learner, .task, .subset = NULL, .weights = NULL, ...) {
    if (length(getTaskDesc(.task)$class.levels) <= 1) {
        x = list(oneclass=TRUE, value=.task$task.desc$positive)
        class(x) = "OneClassWrapperModel"
        return(makeChainModel(next.model = x, cl = c(.learner$model.subclass)))
    }
    model = train(.learner$next.learner, .task, .subset, .weights)
    x = list(oneclass=FALSE, model=model)
    class(x) = "OneClassWrapperModel"
    return(makeChainModel(next.model = x, cl = c(.learner$model.subclass)))
}
predictLearner.OneClassWrapper = function(.learner, .model, .newdata, ...) {
    .model = mlr::getLearnerModel(.model, more.unwrap = FALSE)
    if (.model$oneclass) {
        out = as.logical(rep(.model$value, nrow(.newdata)))
    }
    else {
        pred = predict(.model$model, newdata=.newdata)
        if (.learner$predict.type == "response") {
            out = getPredictionResponse(pred)
        } else {
            out = getPredictionProbabilities(pred, cl="TRUE")
        }
    }
    return(as.factor(out))
}
getLearnerProperties.OneClassWrapper = function(.learner) {
    return(.learner$properties)
}
isFailureModel.OneClassWrapperModel = function(model) {
    model = mlr::getLearnerModel(model, more.unwrap = FALSE)
  return(!model$oneclass && isFailureModel(model$model))
}
getFailureModelMsg.OneClassWrapperModel = function(model) {
    model = mlr::getLearnerModel(model, more.unwrap = FALSE)
  if (model$oneclass)
      return("")
  return(getFailureModelMsg(model$model))
}
getFailureModelDump.OneClassWrapperModel = function(model) {
    model = mlr::getLearnerModel(model, more.unwrap = FALSE)
  if (model$oneclass)
      return("")
  return(getFailureModelDump(model$model))
}
registerS3method("trainLearner", "<OneClassWrapper>", 
  trainLearner.OneClassWrapper)
registerS3method("getLearnerProperties", "<OneClassWrapper>", 
  getLearnerProperties.OneClassWrapper)
registerS3method("isFailureModel", "<OneClassWrapperModel>", 
  isFailureModel.OneClassWrapperModel)
registerS3method("getFailureModelMsg", "<OneClassWrapperModel>", 
  getFailureModelMsg.OneClassWrapperModel)
registerS3method("getFailureModelDump", "<OneClassWrapperModel>", 
  getFailureModelDump.OneClassWrapperModel)

相关内容

  • 没有找到相关文章

最新更新