R/LearnerClustFanny.R

#' @title Fuzzy Analysis Clustering Learner
#'
#' @name mlr_learners_clust.fanny
#' @include LearnerClust.R
#' @include aaa.R
#'
#' @description
#' A [LearnerClust] for fuzzy clustering implemented in [cluster::fanny()].
#' [cluster::fanny()] doesn't have a default value for the number of clusters.
#' Therefore, the `k` parameter which corresponds to the number
#' of clusters here is set to 2 by default.
#' The predict method copies cluster assignments and memberships
#' generated for train data. The predict does not work for
#' new data.
#'
#' @templateVar id clust.fanny
#' @template learner
#' @template example
#'
#' @export
LearnerClustFanny = R6Class("LearnerClustFanny",
  inherit = LearnerClust,
  public = list(
    #' @description
    #' Creates a new instance of this [R6][R6::R6Class] class.
    initialize = function() {
      ps = ps(
        k = p_int(lower = 1L, default = 2L, tags = c("required", "train")),
        memb.exp = p_dbl(lower = 1L, default = 2L, tags = "train"),
        metric = p_fct(default = "euclidean", levels = c("euclidean", "manhattan", "SqEuclidean"), tags = "train"),
        stand = p_lgl(default = FALSE, tags = "train"),
        maxit = p_int(lower = 0L, default = 500L, tags = "train"),
        tol = p_dbl(lower = 0L, default = 1e-15, tags = "train"),
        trace.lev = p_int(lower = 0L, default = 0L, tags = "train")
      )
      ps$values = list(k = 2L)

      super$initialize(
        id = "clust.fanny",
        feature_types = c("logical", "integer", "numeric"),
        predict_types = c("partition", "prob"),
        param_set = ps,
        properties = c("partitional", "fuzzy", "complete"),
        packages = "cluster",
        man = "mlr3cluster::mlr_learners_clust.fanny",
        label = "Fuzzy Analysis Clustering"
      )
    }
  ),

  private = list(
    .train = function(task) {
      pv = self$param_set$get_values(tags = "train")
      m = invoke(cluster::fanny, x = task$data(), .args = pv)
      if (self$save_assignments) {
        self$assignments = m$clustering
      }

      return(m)
    },

    .predict = function(task) {
      warn_prediction_useless(self$id)

      partition = self$model$clustering

      prob = self$model$membership
      colnames(prob) = seq_len(ncol(prob))

      PredictionClust$new(task = task, partition = partition, prob = prob)
    }
  )
)

learners[["clust.fanny"]] = LearnerClustFanny

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.