R/LearnerClustSimpleKMeans.R

#' @title K-Means Clustering Learner from Weka
#'
#' @name mlr_learners_clust.SimpleKMeans
#' @include LearnerClust.R
#' @include aaa.R
#'
#' @description
#' A [LearnerClust] for Simple K Means clustering implemented in [RWeka::SimpleKMeans()].
#' The predict method uses [RWeka::predict.Weka_clusterer()] to compute the
#' cluster memberships for new data.
#'
#' @templateVar id clust.SimpleKMeans
#' @template learner
#' @examples
#' \dontrun{
#' if (requireNamespace("RWeka")) {
#'   learner = mlr3::lrn("clust.SimpleKMeans")
#'   print(learner)
#'
#'   # available parameters:
#'   learner$param_set$ids()
#' }}
#'
#' @export
LearnerClustSimpleKMeans = R6Class("LearnerClustSimpleKMeans",
  inherit = LearnerClust,
  public = list(
    #' @description
    #' Creates a new instance of this [R6][R6::R6Class] class.
    initialize = function() {
      ps = ps(
        A                 = p_uty(default = "weka.core.EuclideanDistance", tags = "train"),
        C                 = p_lgl(default = FALSE, tags = "train"),
        fast              = p_lgl(default = FALSE, tags = "train"),
        I                 = p_int(default = 100L, lower = 1L, tags = "train"),
        init              = p_int(default = 0L, lower = 0L, upper = 3L, tags = "train"),
        M                 = p_lgl(default = FALSE, tags = "train"),
        max_candidates    = p_int(default = 100L, lower = 1L, tags = "train"),
        min_density       = p_int(default = 2L, lower = 1L, tags = "train"),
        N                 = p_int(default = 2L, lower = 1L, tags = "train"),
        num_slots         = p_int(default = 1L, lower = 1L, tags = "train"),
        O                 = p_lgl(default = FALSE, tags = "train"),
        periodic_pruning  = p_int(default = 10000L, lower = 1L, tags = "train"),
        S                 = p_int(default = 10L, lower = 0L, tags = "train"),
        t2                = p_dbl(default = -1, tags = "train"),
        t1                = p_dbl(default = -1.5, tags = "train"),
        V                 = p_lgl(default = FALSE, tags = "train"),
        output_debug_info = p_lgl(default = FALSE, tags = "train")
      )

      super$initialize(
        id = "clust.SimpleKMeans",
        feature_types = c("logical", "integer", "numeric"),
        predict_types = "partition",
        param_set = ps,
        properties = c("partitional", "exclusive", "complete"),
        packages = "RWeka",
        man = "mlr3cluster::mlr_learners_clust.SimpleKMeans",
        label = "K-Means (Weka)"
      )
    }
  ),

  private = list(
    .train = function(task) {
      pv = self$param_set$get_values(tags = "train")
      names(pv) = chartr("_", "-", names(pv))
      ctrl = do.call(RWeka::Weka_control, pv)
      m = invoke(RWeka::SimpleKMeans, x = task$data(), control = ctrl)
      if (self$save_assignments) {
        self$assignments = unname(m$class_ids + 1L)
      }

      return(m)
    },

    .predict = function(task) {
      partition = predict(self$model, newdata = task$data(), type = "class") + 1L
      PredictionClust$new(task = task, partition = partition)
    }
  )
)

learners[["clust.SimpleKMeans"]] = LearnerClustSimpleKMeans

Try the mlr3cluster package in your browser

Any scripts or data that you put into this service are public.

mlr3cluster documentation built on March 31, 2023, 11:11 p.m.