R/LearnerClustCobweb.R

#' @title Cobweb Clustering Learner
#'
#' @name mlr_learners_clust.cobweb
#' @include LearnerClust.R
#'
#' @description
#' Cobweb clustering.
#' Calls [RWeka::Cobweb()] from package \CRANpkg{RWeka}.
#'
#' The predict method uses [RWeka::predict.Weka_clusterer()] to compute the cluster memberships for new data.
#'
#' @templateVar id clust.cobweb
#' @template learner
#'
#' @references
#' `r format_bib("witten2002data", "fisher1987knowledge", "gennari1989models")`
#'
#' @export
#' @template seealso_learner
#' @template example
LearnerClustCobweb = R6Class(
  "LearnerClustCobweb",
  inherit = LearnerClust,
  public = list(
    #' @description
    #' Creates a new instance of this [R6][R6::R6Class] class.
    initialize = function() {
      param_set = ps(
        A = p_dbl(0, default = 1, tags = "train"),
        C = p_dbl(0, default = 0.0028209479177387815, tags = "train"),
        save_data = p_lgl(default = FALSE, tags = "train"),
        S = p_int(0L, default = 42L, tags = "train"),
        output_debug_info = p_lgl(default = FALSE, tags = "train"),
        do_not_check_capabilities = p_lgl(default = FALSE, tags = "train")
      )

      super$initialize(
        id = "clust.cobweb",
        feature_types = c("logical", "integer", "numeric"),
        predict_types = "partition",
        param_set = param_set,
        properties = c("hierarchical", "exclusive", "complete", "missings", "marshal"),
        packages = "RWeka",
        man = "mlr3cluster::mlr_learners_clust.cobweb",
        label = "Cobweb"
      )
    },

    #' @description
    #' Marshal the learner's model.
    #' @param ... (any)\cr
    #'   Additional arguments passed to [mlr3::marshal_model()].
    marshal = function(...) {
      learner_marshal(.learner = self, ...)
    },

    #' @description
    #' Unmarshal the learner's model.
    #' @param ... (any)\cr
    #'   Additional arguments passed to [mlr3::unmarshal_model()].
    unmarshal = function(...) {
      learner_unmarshal(.learner = self, ...)
    }
  ),

  active = list(
    #' @field marshaled (`logical(1)`)\cr
    #' Whether the learner's model is marshaled.
    marshaled = function() {
      learner_marshaled(self)
    }
  ),

  private = list(
    .train = function(task) {
      pv = self$param_set$get_values(tags = "train")
      ctrl = weka_control(pv)
      m = invoke(RWeka::Cobweb, x = task$data(), control = ctrl)
      if (self$save_assignments) {
        self$assignments = unname(m$class_ids + 1L)
      }
      m
    },

    .predict = function(task) {
      partition = invoke(predict, self$model, newdata = ordered_features(task, self), type = "class_ids") + 1L
      list(partition = partition)
    }
  )
)

#' @include zzz.R
register_learner("clust.cobweb", LearnerClustCobweb)

Try the mlr3cluster package in your browser

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

mlr3cluster documentation built on Sept. 17, 2026, 5:09 p.m.