Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
Add AdaBoost, IBk, LMT, OneR, PART (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
henrifnk authored Apr 30, 2020
1 parent f9b05e8 commit 9debe41
Show file tree
Hide file tree
Showing 37 changed files with 1,711 additions and 88 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
Package: mlr3learners.rweka
Title: M5Rules, JRip and J48 Learner for mlr3
Title: AdaBoostM1, IBk ,JRip, J48, LMT, M5Rules, OneR and PART
Learner for mlr3
Version: 0.1.0.9000
Authors@R:
person(given = "Henri",
family = "Funk",
role = "cre",
email = "[email protected]")
Description: JRip and J48 Learner for mlr3.
Description: AdaBoostM1, IBk ,JRip, J48, LMT, M5Rules, OneR and
PART Learner for mlr3.
License: LGPL-3
Depends:
R (>= 3.1.0)
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Generated by roxygen2: do not edit by hand

export(LearnerClassifAdaBoostM1)
export(LearnerClassifIBk)
export(LearnerClassifJ48)
export(LearnerClassifJRip)
export(LearnerClassifLMT)
export(LearnerClassifOneR)
export(LearnerClassifPART)
export(LearnerRegrIBk)
export(LearnerRegrM5Rules)
import(data.table)
import(mlr3misc)
Expand Down
104 changes: 104 additions & 0 deletions R/LearnerClassifAdaBoostM1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#' @title Classification AdaBoostM1 Learner
#'
#' @name mlr_learners_classif.AdaBoostM1
#'
#' @description
#' A [mlr3::LearnerClassif] implementing classification AdaBoostM1 from package \CRANpkg{RWeka}.
# Calls [RWeka::AdaBoostM1()].
#'
#' @section Custom mlr3 defaults:
#' - `output_debug_info`:
#' - original id: output-debug-info
#'
#' - `do_not_check_capabilities`:
#' - original id: do-not-check-capabilities
#'
#' - `num_decimal_places`:
#' - original id: num-decimal-places
#'
#' - `batch_size`:
#' - original id: batch-size
#'
#' - Reason for change: This learner contains changed ids of the following control arguments
#' since their ids contain irregular pattern
#'
#' @templateVar id classif.AdaBoostM1
#' @template section_dictionary_learner
#'
#' @references
#' Freund Y, Schapire, R (1993).
#' Experiments with a New Boosting Algorithm
#' \url{http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.51.6252&rep=rep1&type=pdf}
#'
#' @export
LearnerClassifAdaBoostM1 = R6Class("LearnerClassifAdaBoostM1",
inherit = LearnerClassif,
public = list(
#' @description
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function() {
ps = ParamSet$new(
params = list(
ParamUty$new(id = "subset", tags = c("train", "pars")),
ParamUty$new(id = "na.action", tags = c("train", "pars")),
ParamInt$new(
id = "P", default = 100L, lower = 90L, upper = 100L,
tags = c("train", "control")),
ParamLgl$new(id = "Q", default = FALSE, tags = c("train", "control")),
ParamInt$new(id = "S", default = 1L, lower = 1L, tags = c("train", "control")),
ParamInt$new(id = "I", default = 10L, lower = 1L, tags = c("train", "control")),
ParamUty$new(
id = "W", default = "weka.classifiers.trees.DecisionStump",
tags = c("train", "control")),
ParamLgl$new(id = "output_debug_info", default = FALSE, tags = c("train", "control")),
ParamLgl$new(
id = "do_not_check_capabilities", default = FALSE,
tags = c("train", "control")),
ParamInt$new(
id = "num_decimal_places", default = 2L, lower = 1L,
tags = c("train", "control")),
ParamInt$new(id = "batch_size", default = 100L, lower = 1L, tags = c("train", "control")),
ParamUty$new(id = "options", default = NULL, tags = c("train", "pars"))
)
)

super$initialize(
id = "classif.AdaBoostM1",
packages = "RWeka",
feature_types = c("numeric", "factor", "ordered"),
predict_types = c("response", "prob"),
param_set = ps,
properties = c("twoclass", "multiclass"),
man = "mlr3learners.rweka::mlr_learners_classif.AdaBoostM1"
)
}
),

private = list(
.train = function(task) {
ctrl = self$param_set$get_values(tags = "control")
if (length(ctrl) > 0L) {
names(ctrl) = gsub("_", replacement = "-", x = names(ctrl))
ctrl = mlr3misc::invoke(RWeka::Weka_control, ctrl)
}

pars = self$param_set$get_values(tags = "pars")
f = task$formula()
data = task$data()
mlr3misc::invoke(RWeka::AdaBoostM1, formula = f, data = data, control = ctrl, .args = pars)
},

.predict = function(task) {
response = NULL
prob = NULL
newdata = task$data(cols = task$feature_names)

if (self$predict_type == "response") {
response = mlr3misc::invoke(predict, self$model, newdata = newdata, type = "class")
} else {
prob = mlr3misc::invoke(predict, self$model, newdata = newdata, type = "prob")
}
PredictionClassif$new(task = task, response = response, prob = prob)
}
)
)
104 changes: 104 additions & 0 deletions R/LearnerClassifIBk.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#' @title Classification IBk Learner
#'
#' @name mlr_learners_classif.IBk
#'
#' @description
#' A [mlr3::LearnerClassif] implementing classification IBk from package \CRANpkg{RWeka}.
#' Calls [RWeka::IBk()].
#'
#' @section Custom mlr3 defaults:
#' - `output_debug_info`:
#' - original id: output-debug-info
#'
#' - `do_not_check_capabilities`:
#' - original id: do-not-check-capabilities
#'
#' - `num_decimal_places`:
#' - original id: num-decimal-places
#'
#' - `batch_size`:
#' - original id: batch-size
#'
#' - Reason for change: This learner contains changed ids of the following control arguments
#' since their ids contain irregular pattern
#'
#' @templateVar id classif.IBk
#' @template section_dictionary_learner
#'
#' @references
#' Aha D, Kibbler D, Alber M (1991).
#' Instance-based learning algorithms
#' \url{https://link.springer.com/content/pdf/10.1007/BF00153759.pdf}
#'
#' @export
LearnerClassifIBk = R6Class("LearnerClassifIBk",
inherit = LearnerClassif,
public = list(
#' @description
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function() {
ps = ParamSet$new(
params = list(
ParamUty$new(id = "subset", tags = c("train", "pars")),
ParamUty$new(id = "na.action", tags = c("train", "pars")),
ParamLgl$new(id = "I", default = FALSE, tags = c("train", "control")),
ParamLgl$new(id = "F", default = FALSE, tags = c("train", "control")),
ParamInt$new(id = "K", default = 1L, lower = 1L, tags = c("train", "control")),
ParamLgl$new(id = "E", default = FALSE, tags = c("train", "control")),
ParamInt$new(id = "W", default = 0L, lower = 0L, tags = c("train", "control")),
ParamLgl$new(id = "X", default = FALSE, tags = c("train", "control")),
ParamUty$new(
id = "A", default = "weka.core.neighboursearch.LinearNNSearch",
tags = c("train", "control")),
ParamLgl$new(id = "output_debug_info", default = FALSE, tags = c("train", "control")),
ParamLgl$new(
id = "do_not_check_capabilities", default = FALSE,
tags = c("train", "control")),
ParamInt$new(
id = "num_decimal_places", default = 2L, lower = 1L,
tags = c("train", "control")),
ParamInt$new(id = "batch_size", default = 100L, lower = 1L, tags = c("train", "control")),
ParamUty$new(id = "options", default = NULL, tags = c("train", "pars"))
)
)

super$initialize(
id = "classif.IBk",
packages = "RWeka",
feature_types = c("numeric", "factor", "ordered"),
predict_types = c("response", "prob"),
param_set = ps,
properties = c("twoclass", "multiclass"),
man = "mlr3learners.rweka::mlr_learners_classif.IBk"
)
}
),

private = list(
.train = function(task) {
ctrl = self$param_set$get_values(tags = "control")
if (length(ctrl) > 0L) {
names(ctrl) = gsub("_", replacement = "-", x = names(ctrl))
ctrl = mlr3misc::invoke(RWeka::Weka_control, ctrl)
}

pars = self$param_set$get_values(tags = "pars")
f = task$formula()
data = task$data()
mlr3misc::invoke(RWeka::IBk, formula = f, data = data, control = ctrl, .args = pars)
},

.predict = function(task) {
response = NULL
prob = NULL
newdata = task$data(cols = task$feature_names)

if (self$predict_type == "response") {
response = mlr3misc::invoke(predict, self$model, newdata = newdata, type = "class")
} else {
prob = mlr3misc::invoke(predict, self$model, newdata = newdata, type = "prob")
}
PredictionClassif$new(task = task, response = response, prob = prob)
}
)
)
36 changes: 24 additions & 12 deletions R/LearnerClassifJ48.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@
#' A [mlr3::LearnerClassif] implementing classification J48 from package \CRANpkg{RWeka}.
#' Calls [RWeka::J48()].
#'
#' This learner contains changed ids of the following control agruments
#' since their ids contain irregular pattern:
#' * mlr3learner: output_debug_info RWeka: output-debug-info
#' * mlr3learner: do_not_check_capabilities RWeka: do-not-check-capabilities
#' * mlr3learner: num_decimal_places RWeka: num-decimal-places
#' * mlr3learner: batch_size RWeka: batch-size
#' @section Custom mlr3 defaults:
#' - `output_debug_info`:
#' - original id: output-debug-info
#'
#' - `do_not_check_capabilities`:
#' - original id: do-not-check-capabilities
#'
#' - `num_decimal_places`:
#' - original id: num-decimal-places
#'
#' - `batch_size`:
#' - original id: batch-size
#'
#' - Reason for change: This learner contains changed ids of the following control arguments
#' since their ids contain irregular pattern
#'
#' @templateVar id classif.J48
#' @template section_dictionary_learner
Expand All @@ -34,7 +43,8 @@ LearnerClassifJ48 = R6Class("LearnerClassifJ48",
ParamUty$new(id = "na.action", tags = c("train", "pars")),
ParamLgl$new(id = "U", default = FALSE, tags = c("train", "control")),
ParamLgl$new(id = "O", default = FALSE, tags = c("train", "control")),
ParamDbl$new(id = "C", default = 0.25, lower = .Machine$double.eps,
ParamDbl$new(
id = "C", default = 0.25, lower = .Machine$double.eps,
upper = 1 - .Machine$double.eps, tags = c("train", "control")),
ParamInt$new(id = "M", default = 2L, lower = 1L, tags = c("train", "control")),
ParamLgl$new(id = "R", default = FALSE, tags = c("train", "control")),
Expand All @@ -45,13 +55,15 @@ LearnerClassifJ48 = R6Class("LearnerClassifJ48",
ParamLgl$new(id = "A", default = FALSE, tags = c("train", "control")),
ParamLgl$new(id = "J", default = FALSE, tags = c("train", "control")),
ParamInt$new(id = "Q", default = 1L, lower = 1L, tags = c("train", "control")),
ParamLgl$new(id = "doNotMakeSplitPointActualValue", default = FALSE,
tags = c("train", "control")),
ParamLgl$new(id = "output_debug_info", default = FALSE,
ParamLgl$new(
id = "doNotMakeSplitPointActualValue", default = FALSE,
tags = c("train", "control")),
ParamLgl$new(id = "do_not_check_capabilities", default = FALSE,
ParamLgl$new(id = "output_debug_info", default = FALSE, tags = c("train", "control")),
ParamLgl$new(
id = "do_not_check_capabilities", default = FALSE,
tags = c("train", "control")),
ParamInt$new(id = "num_decimal_places", default = 2L, lower = 1L,
ParamInt$new(
id = "num_decimal_places", default = 2L, lower = 1L,
tags = c("train", "control")),
ParamInt$new(id = "batch_size", default = 100L, lower = 1L, tags = c("train", "control")),
ParamUty$new(id = "options", default = NULL, tags = c("train", "pars"))
Expand Down
27 changes: 19 additions & 8 deletions R/LearnerClassifJRip.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@
#' A [mlr3::LearnerClassif] implementing classification JRip from package \CRANpkg{RWeka}.
#' Calls [RWeka::JRip()].
#'
#' This learner contains changed ids of the following control agruments
#' since their ids contain irregular pattern:
#' * mlr3learner: output_debug_info RWeka: output-debug-info
#' * mlr3learner: do_not_check_capabilities RWeka: do-not-check-capabilities
#' * mlr3learner: num_decimal_places RWeka: num-decimal-places
#' * mlr3learner: batch_size RWeka: batch-size
#' @section Custom mlr3 defaults:
#' - `output_debug_info`:
#' - original id: output-debug-info
#'
#' - `do_not_check_capabilities`:
#' - original id: do-not-check-capabilities
#'
#' - `num_decimal_places`:
#' - original id: num-decimal-places
#'
#' - `batch_size`:
#' - original id: batch-size
#'
#' - Reason for change: This learner contains changed ids of the following control arguments
#' since their ids contain irregular pattern
#'
#' @templateVar id classif.Jrip
#' @template section_dictionary_learner
Expand Down Expand Up @@ -41,9 +50,11 @@ LearnerClassifJRip = R6Class("LearnerClassifJRip",
ParamLgl$new(id = "E", default = FALSE, tags = c("train", "control")),
ParamLgl$new(id = "P", default = FALSE, tags = c("train", "control")),
ParamLgl$new(id = "output_debug_info", default = FALSE, tags = c("train", "control")),
ParamLgl$new(id = "do_not_check_capabilities", default = FALSE,
ParamLgl$new(
id = "do_not_check_capabilities", default = FALSE,
tags = c("train", "control")),
ParamInt$new(id = "num_decimal_places", default = 2L, lower = 1L,
ParamInt$new(
id = "num_decimal_places", default = 2L, lower = 1L,
tags = c("train", "control")),
ParamInt$new(id = "batch_size", default = 100L, lower = 1L, tags = c("train", "control")),
ParamUty$new(id = "options", default = NULL, tags = c("train", "pars"))
Expand Down
Loading

0 comments on commit 9debe41

Please sign in to comment.