Skip to content

Commit f684f52

Browse files
committed
Merge pull request #584 from mlr-org/refactor_bench_plots
refactor benchplots
2 parents b24f4ca + a19f051 commit f684f52

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+543
-902
lines changed

NAMESPACE

+3-5
Original file line numberDiff line numberDiff line change
@@ -639,14 +639,12 @@ export(fp)
639639
export(fpr)
640640
export(friedmanPostHocTestBMR)
641641
export(friedmanTestBMR)
642-
export(generateBenchmarkSummaryData)
643642
export(generateCalibrationData)
644643
export(generateCritDifferencesData)
645644
export(generateFilterValuesData)
646645
export(generateLearningCurveData)
647646
export(generatePartialPredictionData)
648647
export(generateROCRCurvesData)
649-
export(generateRankMatrixAsBarData)
650648
export(generateThreshVsPerfData)
651649
export(getBMRAggrPerformances)
652650
export(getBMRFeatSelResults)
@@ -819,8 +817,9 @@ export(normalizeFeatures)
819817
export(npv)
820818
export(oversample)
821819
export(performance)
822-
export(plotBenchmarkResult)
823-
export(plotBenchmarkSummary)
820+
export(plotBMRBoxplots)
821+
export(plotBMRRanksAsBarChart)
822+
export(plotBMRSummary)
824823
export(plotCalibration)
825824
export(plotCritDifferences)
826825
export(plotFilterValues)
@@ -833,7 +832,6 @@ export(plotPartialPredictionGGVIS)
833832
export(plotROCCurves)
834833
export(plotROCRCurves)
835834
export(plotROCRCurvesGGVIS)
836-
export(plotRankMatrixAsBar)
837835
export(plotThreshVsPerf)
838836
export(plotThreshVsPerfGGVIS)
839837
export(plotTuneMultiCritResult)

R/BenchmarkResultOrderLevels.R

+14-23
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
11
# order levels of task.ids in a BenchmarkResult or a similar data.frame
2-
# usefull for plotting in ggplot2
3-
2+
# useful for plotting in ggplot2
3+
# if order.tsks is NULL, just return the df
44
orderBMRTasks = function(bmr, df = NULL, order.tsks) {
5-
6-
assertClass(bmr, "BenchmarkResult")
7-
assertVector(order.tsks, len = length(getBMRTaskIds(bmr)))
8-
95
if (is.null(df))
106
df = as.data.frame(bmr)
11-
assertClass(df, "data.frame")
12-
assertCharacter(order.tsks)
13-
assertSetEqual(order.tsks, getBMRTaskIds(bmr), ordered = FALSE)
14-
15-
df$task.id = factor(df$task.id, order.tsks)
7+
if (!is.null(order.tsks)) {
8+
assertCharacter(order.tsks, len = length(getBMRTaskIds(bmr)))
9+
assertSetEqual(order.tsks, getBMRTaskIds(bmr), ordered = FALSE)
10+
df$task.id = factor(df$task.id, order.tsks)
11+
}
1612
return(df)
1713
}
1814

19-
20-
2115
# order levels of learner.ids of a BenchmarkResult or similar data.frame
22-
# usefull for plotting in ggplot2
16+
# useful for plotting in ggplot2
17+
# if order.tsks is NULL, just return the df
2318
orderBMRLrns = function(bmr, df = NULL, order.lrns){
24-
25-
assertClass(bmr, "BenchmarkResult")
26-
assertVector(order.lrns, len = length(getBMRLearnerIds(bmr)))
27-
28-
# create df and/or getLearnerIds
2919
if (is.null(df))
3020
df = as.data.frame(bmr)
31-
32-
assertCharacter(order.lrns)
33-
assertSetEqual(order.lrns, getBMRLearnerIds(bmr), ordered = FALSE)
34-
df$learner.id = factor(df$learner.id, order.lrns)
21+
if (!is.null(order.lrns)) {
22+
assertCharacter(order.lrns, len = length(getBMRLearnerIds(bmr)))
23+
assertSetEqual(order.lrns, getBMRLearnerIds(bmr), ordered = FALSE)
24+
df$learner.id = factor(df$learner.id, order.lrns)
25+
}
3526
return(df)
3627
}

R/benchmark.R

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
#' @return [\code{\link{BenchmarkResult}}].
2727
#' @family benchmark
2828
#' @export
29+
#' @examples
30+
#' lrns = list(makeLearner("classif.lda"), makeLearner("classif.rpart"))
31+
#' tasks = list(iris.task, sonar.task)
32+
#' rdesc = makeResampleDesc("CV", iters = 2L)
33+
#' meas = list(acc, ber)
34+
#' bmr = benchmark(lrns, tasks, rdesc, measures = meas)
35+
#' rmat = convertBMRToRankMatrix(bmr)
36+
#' print(rmat)
37+
#' plotBMRSummary(bmr)
38+
#' plotBMRBoxplots(bmr, ber, style = "violin")
39+
#' plotBMRRanksAsBarChart(bmr, pos = "stack")
2940
benchmark = function(learners, tasks, resamplings, measures, keep.pred = TRUE, models = TRUE, show.info = getMlrOption("show.info")) {
3041
learners = ensureVector(learners, 1L, "Learner")
3142
assertList(learners, min.len = 1L)

R/checkBMRMeasure.R

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# small arg checker for a selected measure for a BMR
2+
# if NULL, the 1st measure in the BMR is returned
3+
checkBMRMeasure = function(measure, bmr) {
4+
if (is.null(measure)) {
5+
measure = getBMRMeasures(bmr)[[1]]
6+
} else {
7+
assertClass(measure, "Measure")
8+
assertChoice(measure$id, getBMRMeasureIds(bmr))
9+
}
10+
return(measure)
11+
}

R/convertBMRToRankMatrix.R

+25-36
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
11
#' @title Convert BenchmarkResult to a rank-matrix.
2-
#'
3-
#' @description Computes a matrix of all the ranks of different algorithms
4-
#' over different datasets (tasks). Ranks are computed from aggregated
5-
#' measures.
6-
#'
2+
#'
3+
#' @description
4+
#' Computes a matrix of all the ranks of different algorithms over different datasets (tasks).
5+
#' Ranks are computed from aggregated measures.
6+
#' Smaller ranks imply better methods, so for measures that are minimized, small ranks imply small scores.
7+
#' for measures that are maximized, small ranks imply large scores.
8+
#'
79
#' @template arg_bmr
810
#' @template arg_measure
9-
#' @template arg_aggregation_method
1011
#' @param ties.method [\code{character(1)}]\cr
11-
#' see \code{\link{rank}} for details.
12+
#' See \code{\link{rank}} for details.
13+
#' @template arg_aggregation_method
1214
#' @return [\code{matrix}]
13-
#' Matrix, with measure ranks as entries. \cr
14-
#' The matrix has one row for each \code{learner},
15+
#' Matrix, with rank of learners per task as entries. The matrix has one row for each \code{learner},
1516
#' and one column for each \code{task}.
16-
#'
17-
#'
18-
#' @examples
19-
#' # see benchmark
20-
#' # convertBMRToRankMatrix(res, acc)
21-
#'
2217
#' @family benchmark
2318
#' @export
24-
25-
convertBMRToRankMatrix = function(bmr, measure = NULL, ties.method = "average",
26-
aggregation = "default") {
27-
19+
#' @examples
20+
#' # see benchmark
21+
convertBMRToRankMatrix = function(bmr, measure = NULL, ties.method = "average", aggregation = "default") {
2822
assertClass(bmr, "BenchmarkResult")
29-
if (is.null(measure))
30-
measure = getBMRMeasures(bmr)[[1]]
31-
assertClass(measure, "Measure")
32-
assertChoice(measure$id, getBMRMeasureIds(bmr))
23+
measure = checkBMRMeasure(measure, bmr)
3324
assertChoice(aggregation, c("mean", "default"))
34-
25+
3526
# aggregate mean over iterations
36-
if (aggregation == "mean") {
27+
if (aggregation == "mean") {
3728
df = as.data.frame(bmr)
3829
df = aggregate(df[[measure$id]],
3930
by = list(task.id = df$task.id,
@@ -45,22 +36,20 @@ convertBMRToRankMatrix = function(bmr, measure = NULL, ties.method = "average",
4536
df = df[, c("task.id", "learner.id", aggr.meas)]
4637
names(df)[names(df) == aggr.meas] = c("x")
4738
}
48-
49-
# calculate ranks, rank according to minimize option of the measure
50-
# due to a bug in plyr, eval(parse()) has to be called in order to
51-
# dynamically adjust the ties.method
39+
40+
# calculate ranks, rank according to minimize option of the measure
5241
if (!measure$minimize)
53-
df$x = desc(df$x)
54-
eval(parse(text = paste0(
55-
"df = ddply(df, .(task.id), transform, alg.rank = rank(x, ties.method = '",
56-
ties.method, "'))"
57-
)))
58-
42+
df$x = -df$x
43+
df = ddply(df, "task.id", function(d) {
44+
d$alg.rank = rank(d$x, ties.method = ties.method)
45+
return(d)
46+
})
47+
5948
# convert into matrix, rows = leaner, cols = tasks
6049
df = melt(df, c("task.id", "learner.id"), "alg.rank")
6150
df = dcast(df, learner.id ~ task.id )
6251
rownames(df) = df$learner.id
6352
mat = as.matrix(df[,colnames(df) != "learner.id"])
64-
53+
6554
return(mat)
6655
}

R/generateBenchmarkSummary.R

-123
This file was deleted.

0 commit comments

Comments
 (0)