如何根据mlr3中的指标列和批量训练预测对任务进行子集化?

Shu*_*ras 2 r machine-learning batch-processing mlr3

背景

我正在使用 R 中的 mlr3 包进行建模和预测。我正在处理一个包含测试集和训练集的大数据集。测试集和训练集由指示符列指示(在代码中:test_or_train)。

目标

  1. 使用数据集中的 train_or_test 列指示的训练行对所有学习器进行批量训练。
  2. 使用相应的训练学习器批量预测 test_or_train 列中由“test”指定的行。

代码

  1. 占位符数据集与测试火车指标列。(在实际数据中 train-test split 不是人为的)
  2. 两个任务(在实际代码中任务是不同的,而且还有更多。)
library(readr)
library(mlr3)
library(mlr3learners)
library(mlr3pipelines)
library(reprex)
library(caret)

# Data
urlfile = 'https://raw.githubusercontent.com/shudras/office_data/master/office_data.csv'
data = read_csv(url(urlfile))[-1]

## Create artificial partition to test and train sets
art_part = createDataPartition(data$imdb_rating, list=FALSE)
train = data[art_part,]
test = data[-art_part,]

## Add test-train indicators
train$test_or_train = 'train'
test$test_or_train = 'test'

## Data set that I want to work / am working with
data = rbind(test, train)

# Create two tasks (Here the tasks are the same but in my data set they differ.)
task1 = 
  TaskRegr$new(
    id = 'office1', 
    backend = data, 
    target = 'imdb_rating'
  )
task2 = 
  TaskRegr$new(
    id = 'office2', 
    backend = data, 
    target = 'imdb_rating'
  )


# Model specification 
graph = 
  po('scale') %>>% 
  lrn('regr.cv_glmnet', 
      id = 'rp', 
      alpha = 1, 
      family = 'gaussian'
  ) 

# Learner creation
learner = GraphLearner$new(graph)

# Goal 
## 1. Batch train all learners with the train rows indicated by the train_or_test column in the data set
## 2. Batch predict the rows designated by the 'test' in the test_or_train column with the respective trained learner
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.3.0)于 2020 年 6 月 22 日创建

笔记

我尝试使用带有 row_ids 的 benchmark_grid 来仅用训练行训练学习者,但这不起作用,而且也不可能使用列指示符,因为它比使用行索引容易得多。使用列测试训练指示符,您可以使用一个规则(用于拆分),而使用行索引仅在任务包含相同行时才有效。

benchmark_grid(
    tasks = list(task1, task2), 
    learners = learner, 
    row_ids = train_rows # Not an argument and not favorable to work with indices
) 
Run Code Online (Sandbox Code Playgroud)

小智 6

您可以使用benchmark自定义设计。

以下应该完成这项工作(请注意,我Resampling为每个Task单独实例化了一个自定义。

library(data.table)
design = data.table(
  task = list(task1, task2),
  learner = list(learner)
)

library(mlr3misc)
design$resampling = map(design$task, function(x) {
  # get train/test split
  split = x$data()[["test_or_train"]]
  # remove train-test split column from the task
  x$select(setdiff(x$feature_names, "test_or_train"))
  # instantiate a custom resampling with the given split
  rsmp("custom")$instantiate(x,
    train_sets = list(which(split == "train")),
    test_sets = list(which(split == "test"))
  )
})

benchmark(design)
Run Code Online (Sandbox Code Playgroud)

您能否batch-processing更清楚地说明您的意思,或者这是否回答了您的问题?