AGraphLearner
总是包装 a Graph
,它接受一个Task
作为输入并产生一个Prediction
作为输出。然而, AGraph
可以代表任何类型的计算,甚至可以接受多个输入/产生多个输出。在构建对Graph
单个任务进行训练的 a时,您通常会将它们用作中间构建块,给出单个预测,然后将其包装为.GraphLearner
在某些情况下,如果您进行某种预处理(例如插补或 PCA),这也可能会有所帮助,这些预处理也应该应用于某种看不见的数据(即应用与 PCA 相同的旋转),即使您的整个过程不是经典机器学习产生预测模型:
data <- tsk("pima")
trainingset <- sample(seq(0, 1, length.out = data$nrow) < 2/3)
data.t <- data$clone(deep = TRUE)$filter(which(trainingset))
data.p <- data$clone(deep = TRUE)$filter(which(!trainingset))
# Operation:
# 1. impute missing values with mean of non-missings in same column
# 2. rotate to principal component axes
imputepca <- po("imputemean") %>>% po("pca")
# Need to take element 1 of result here: 'Graph' could have multiple
# outputs and therefore returns a list. In our case we only have one
# result that we care about.
rotated.t <- imputepca$train(data.t)[[1]]
rotated.t$head(2)
#> diabetes PC1 PC2 PC3 PC4 PC5 PC6 PC7 PC8
#> 1: pos -4.744963 27.76824 -5.2432401 9.817512 -9.042784 0.4979002 0.4574355 -0.1058608
#> 2: neg 6.341357 -37.18033 -0.1210501 3.731123 -1.451952 3.6890699 2.3901156 0.0755521
# this data is imputed using the column means of the training data, and then
# rotated by the same rotation as the training data.
rotated.p <- imputepca$predict(data.p)[[1]]
rotated.p$head(2)
#> diabetes PC1 PC2 PC3 PC4 PC5 PC6 PC7 PC8
#> 1: pos -11.535952 9.358736 25.1073705 4.761627 -23.313410 -9.743428 3.412071 -1.6403521
#> 2: neg 1.189971 -7.098455 -0.2785817 -3.280845 -0.281516 -2.277787 -6.746323 0.3434535
然而,由于mlr3pipelines
主要是为 s 构建的mlr3
,它是关于让Learner
s 可以被训练和重新采样等,你通常最终会将你Graph
的 s 包装在GraphLearner
s 中。