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



Background

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

目标

  1. 使用数据集中train_or_test列指示的训练行批量训练所有学习者。
  2. 使用相应训练的学习者批量预测test_or_train列中"测试"指定的行。

法典

  1. 带有测试训练指示器列的占位符数据集。(在实际的数据训练-测试拆分不是人为的(
  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

创建于 2020-06-22 由 reprex 软件包 (v0.3.0(

注意

我尝试将 benchmark_grid 与 row_ids 一起使用,以仅使用火车行训练学习者,但这不起作用,并且也无法使用比使用行索引容易得多的列指示符。使用列测试训练指示符,可以使用一个规则(用于拆分(,而使用行索引仅在任务包含相同行时才有效。

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

您可以将benchmark与自定义设计一起使用。

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

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)

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

最新更新