2017-07-07 155 views
-1

我有一個名爲的包列表。它是從mlr包(僞)代碼的一些迴歸方法的結果如下所示。我想從中提取彙總的性能結果每個任務。例如,如何從$ visc.1 $ regr.rpart和$ visc.2 $ regr.rpart訪問「Aggr。perf」值。我可以單獨訪問它們,但我認爲必須有一個更簡單的方法。從R列表中提取結果

> class(bag) 

[1] "list" 

製造袋(不包括數據集)編碼:

library('mlr') 
dataset = read.csv("dataset.csv") 
regr.task = makeRegrTask(id = "dataset", data = dataset, target = "target") 
# feature reduction 
fv = generateFilterValuesData(regr.task) 

# resampling description  
rdesc = makeResampleDesc("Holdout") 

#################### 
bag = NULL 
#################### 

for (i in 1:2) 
{ 

    cols = c(order(fv$data$randomForestSRC.rfsrc, decreasing = TRUE)[1:i], ncol(dataset)) 
    dataset.ig = dataset[, cols] 
    iid = paste(c("dataset", i), collapse = ".") 
    regr.task = makeRegrTask(id = iid, 
          data = dataset.ig, 
          target = "dataset") 

    # learners 
    lrns = list(
    "regr.bcart" 
    ,"regr.fnn" 
    ,"regr.rpart" 
) 

     set.seed(0, "L'Ecuyer") 
     bmr = benchmark(lrns, regr.task, rdesc, show.info = FALSE) 

     ######################### 
     bag = c(bag, bmr) 
     ######################### 
} 

MWC:

for (i in seq(1,42,3)) 
{ 
    print (bag[i]$results) 
} 

輸出:

$visc.1 
$visc.1$regr.bcart 
Resample Result 
Task: visc.1 
Learner: regr.bcart 
Aggr perf: mse.test.mean=5.14e+03 
Runtime: 0.181672 

$visc.1$regr.cforest 
Resample Result 
Task: visc.1 
Learner: regr.cforest 
Aggr perf: mse.test.mean=4.92e+03 
Runtime: 0.103091 

$visc.1$regr.fnn 
Resample Result 
Task: visc.1 
Learner: regr.fnn 
Aggr perf: mse.test.mean=2.44e+03 
Runtime: 0.0151947 

$visc.1$regr.mars 
Resample Result 
Task: visc.1 
Learner: regr.mars 
Aggr perf: mse.test.mean=3.77e+03 
Runtime: 0.0163326 

$visc.1$regr.rpart 
Resample Result 
Task: visc.1 
Learner: regr.rpart 
Aggr perf: mse.test.mean=4.77e+03 
Runtime: 0.0265007 


$visc.2 
$visc.2$regr.bcart 
Resample Result 
Task: visc.2 
Learner: regr.bcart 
Aggr perf: mse.test.mean=5.14e+03 
Runtime: 0.177014 

$visc.2$regr.cforest 
Resample Result 
Task: visc.2 
Learner: regr.cforest 
Aggr perf: mse.test.mean=4.95e+03 
Runtime: 0.115235 

$visc.2$regr.fnn 
Resample Result 
Task: visc.2 
Learner: regr.fnn 
Aggr perf: mse.test.mean=3.25e+03 
Runtime: 0.0116491 

$visc.2$regr.mars 
Resample Result 
Task: visc.2 
Learner: regr.mars 
Aggr perf: mse.test.mean=2.67e+03 
Runtime: 0.0153017 

$visc.2$regr.rpart 
Resample Result 
Task: visc.2 
Learner: regr.rpart 
Aggr perf: mse.test.mean=4.77e+03 
Runtime: 0.0252295 

編輯: 我也保存了dput(bag [1:2])here

+1

請使用'dput'顯示一個SMaL公司重複的例子, – akrun

+0

你的意思,而不是使用打印dput?它產生更多的結果。 – remo

+0

因爲它是一個列表,所以最好只有幾個列表元素,即'dput(bag [1:2])',你想要提取什麼,即你的預期輸出 – akrun

回答

0

mtcars列創建列表根據上面的bag示例。

io <-list(mtcars$mpg, mtcars$cyl, mtcars$disp) 
names(io) <- c("mpg","cyl","disp") 
class(io) 
[1] "list" 

io 
$mpg 
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 
[22] 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4 

$cyl 
[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4 

$disp 
[1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6 167.6 275.8 275.8 275.8 472.0 460.0 440.0 
[18] 78.7 75.7 71.1 120.1 318.0 304.0 350.0 400.0 79.0 120.3 95.1 351.0 145.0 301.0 121.0 

爲了實現MWC

req_rows <- seq(1,length(io$mpg),3) 
req_rows 
[1] 1 4 7 10 13 16 19 22 25 28 31 


# rather than using loop, use this 
io$mpg[io_seq] 
[1] 21.0 21.4 14.3 19.2 17.3 10.4 30.4 15.5 19.2 30.4 15.0 
+0

我怎樣才能從$ visc.1 $ regr.rpart和$ visc.2 $ regr.rpart? – remo

+0

@remo獲得聚合函數,你可以給''函數'用來獲取'bag' ''包'< - mlr :: some_function()'這樣一個例子可以被轉載,甚至可以添加'str(bag)'將有助於 – parth

+0

我已經添加了一個簡化版本的代碼。爲什麼downvote?:( – remo