2

可以使用 mlr 包生成 用于比较多个数据集 ( Demšar2006 ) 上的分类器的关键差异 (CD) 图,如下所示:

# THIS WORKS
library(mlr)
lrns = list(makeLearner("classif.knn"), makeLearner("classif.svm"))
tasks = list(iris.task, sonar.task)
rdesc = makeResampleDesc("CV", iters = 2L)
meas = list(acc)
bmr = benchmark(lrns, tasks, rdesc, measures = meas)
cd = generateCritDifferencesData(bmr)
plotCritDifferences(cd)

这需要评估结果驻留在一个相当复杂的BenchmarkResult对象中,尽管数据基本上是一个矩阵(其中包含数据集M[i, j]的分类器分数)。我之前在 Python 工作流程中生成了此类数据并导入到一个(因为似乎没有用于此类绘图的 Python 包)。ijRdata.frame

如何从这些数据中生成 CD 图?

我想BenchmarkResult从 中创建一个data.frame,但不知道从哪里开始:

# THIS DOES NOT WORK
library(mlr)
# Here I would import results from my experiments instead of using random data
# e.g. scores for 5 classifiers and 30 data sets, each
results = data.frame(replicate(5, runif(30, 0, 1)))
# This is the functionality I'm looking for
bmr = benchmarkResultFromDataFrame(results)
cd = generateCritDifferencesData(bmr)
plotCritDifferences(cd)
4

1 回答 1

1

我终于设法创造了情节。只需设置少数几个BenchmarkResult's属性:

  • leaners对于每个分类idshort.name
  • measures
  • results对于aggr每个数据集/分类器组合

然后代码可能如下所示(5 个数据集的较小示例):

library(mlr)
# Here I would import results from my experiments instead of using random data
# e.g. scores for 5 classifiers and 30 data sets, each
results <- data.frame(replicate(5, runif(30, 0, 1)))
clf <- c('clf1', 'clf2', 'clf3', 'clf4', 'clf5')
clf.short.name <- c('c1', 'c2', 'c3', 'c4', 'c5')
dataset <- c('dataset1', 'dataset2', 'dataset3', 'dataset4', 'dataset5')
score <- list(acc)

# Setting up the learners: id, short.name
bmr <- list()
for (i in 1:5){
    bmr$learners[[clf[i]]]$id <- clf[i]
    bmr$learners[[clf[i]]]$short.name <- clf.short.name[i]
}

# Setting up the measures
bmr$measures <- list(acc)

# Setting up the results
for (i in 1:5){
  bmr$results$`dataset1`[[clf[i]]]$aggr <- list('acc.test.mean' = results[1, i])
}
for (i in 1:5){
  bmr$results$`dataset2`[[clf[i]]]$aggr <- list('acc.test.mean' = results[2, i])
}
for (i in 1:5){
  bmr$results$`dataset3`[[clf[i]]]$aggr <- list('acc.test.mean' = results[3, i])
}
for (i in 1:5){
  bmr$results$`dataset4`[[clf[i]]]$aggr <- list('acc.test.mean' = results[4, i])
}
for (i in 1:5){
  bmr$results$`dataset5`[[clf[i]]]$aggr <- list('acc.test.mean' = results[5, i])
}

# Set BenchmarkResult class
class(bmr) <- "BenchmarkResult"

# Statistics and plot
cd = generateCritDifferencesData(bmr)
plotCritDifferences(cd)

任何可以更好地教我R避免这些for循环和代码重复的人仍然会非常受欢迎!

于 2016-12-12T11:17:06.023 回答