简体   繁体   English

mlr3 - 编辑 `task$data()`

[英]mlr3 - Editing `task$data()`

Is there a way to edit task$data() or replace it with a new data.frame() with exactly the same colnames?有没有办法编辑task$data()或用具有完全相同的 colnames 的新data.frame()替换它?

I've tried the following task_train$data() <- newDF and task_train$data <- newDF .我尝试了以下task_train$data() <- newDFtask_train$data <- newDF They both result in Error in task_train$data() <- di: invalid (NULL) left side of assignment and Error in task_train$data <- newDF: cannot change value of locked binding for 'data' , respectively.它们都会分别导致Error in task_train$data() <- di: invalid (NULL) left side of assignmentError in task_train$data <- newDF: cannot change value of locked binding for 'data'

Once you create the task all further data transformations, augmentations etc. should be performed using pipelines .创建任务后,应使用管道执行所有进一步的数据转换、扩充等。 This is especially handy when performing resampling/tuning since it avoids data leakage.这在执行重采样/调整时特别方便,因为它避免了数据泄漏。

Based on the comment by @pat-s this is not only my opinion but the opinion of the core mlr team.根据@pat-s的评论,这不仅是我的观点,也是核心 mlr 团队的观点。 And this is the reason why direct editing of the task data (in ways you show in the question) fails.这就是直接编辑任务数据(以您在问题中显示的方式)失败的原因。

One use case is for swapping knockoff data in place of the real data for measuring conditional effects of features as in the {cpi} package. This would allow other key parts of the task to remain (eg, weights, coordinates) and only modify the data itself.一个用例是用仿冒数据代替真实数据来测量特征的条件效应,如 {cpi} package 中那样。这将允许保留task的其他关键部分(例如,权重、坐标)并且只修改数据本身。

mlr_pipeops_mutate gets us what we want: help file here mlr_pipeops_mutate我们提供了我们想要的: 帮助文件在这里

library("mlr3")
library("mlr3pipelines")

constant = 1
pom = mlr3pipelines::po("mutate")
pom$param_set$values$mutation = list(
  Sepal.Length_plus_constant = ~ Sepal.Length + constant,
  Sepal.Area = ~ Sepal.Width * Sepal.Length,
  Petal.Area = ~ Petal.Width * Petal.Length,
  Sepal.Area_plus_Petal.Area = ~ Sepal.Area + Petal.Area,
  Sepal.Width = Sepal.Width / 2.54 # modify column in place
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM