Nothing
#' @title Divisive Analysis Clustering Learner
#'
#' @name mlr_learners_clust.diana
#'
#' @description
#' Divisive hierarchical clustering.
#' Calls [cluster::diana()] from package \CRANpkg{cluster}.
#'
#' The predict method uses [stats::cutree()] which cuts the tree resulting from hierarchical clustering into specified
#' number of groups (see parameter `k`). The default value for `k` is 2.
#'
#' @section Initial parameter values:
#' - `keep.diss`:
#' - Actual default: `n < 100`, where `n` is the number of observations.
#' - Adjusted default: `FALSE`.
#' - Reason for change: Avoid storing the dissimilarity matrix in the model to save memory.
#' - `keep.data`:
#' - Actual default: `TRUE`.
#' - Adjusted default: `FALSE`.
#' - Reason for change: Avoid storing the training data in the model to save memory.
#'
#' @templateVar id clust.diana
#' @template learner
#'
#' @references
#' `r format_bib("kaufman2009finding")`
#'
#' @export
#' @template seealso_learner
#' @template example
LearnerClustDiana = R6Class(
"LearnerClustDiana",
inherit = LearnerClust,
public = list(
#' @description
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function() {
param_set = ps(
metric = p_fct(c("euclidean", "manhattan"), default = "euclidean", tags = "train"),
stand = p_lgl(default = FALSE, tags = "train"),
stop.at.k = p_uty(
default = FALSE,
tags = "train",
custom_check = crate(function(x) check_false(x) %check||% check_int(x, lower = 1L))
),
keep.diss = p_lgl(tags = "train"),
keep.data = p_lgl(default = TRUE, tags = "train"),
trace.lev = p_int(0L, default = 0L, tags = "train"),
k = p_int(1L, tags = c("train", "cutree", "predict"))
)
param_set$set_values(k = 2L, keep.diss = FALSE, keep.data = FALSE)
super$initialize(
id = "clust.diana",
feature_types = c("logical", "integer", "numeric"),
predict_types = "partition",
param_set = param_set,
properties = c("hierarchical", "exclusive", "complete"),
packages = "cluster",
man = "mlr3cluster::mlr_learners_clust.diana",
label = "Divisive Analysis"
)
}
),
private = list(
.train = function(task) {
ps = self$param_set
m = invoke(
cluster::diana,
x = task$data(),
diss = FALSE,
.args = remove_named(ps$get_values(tags = "train"), "k")
)
if (self$save_assignments) {
self$assignments = invoke(stats::cutree, tree = m, .args = ps$get_values(tags = c("train", "cutree")))
}
m
},
.predict = function(task) {
pv = self$param_set$get_values(tags = "predict")
if (pv$k > task$nrow) {
error_input("`k` needs to be between 1 and %i.", task$nrow)
}
warn_prediction_useless(self$id)
partition = self$assignments %??%
invoke(stats::cutree, tree = self$model, .args = self$param_set$get_values(tags = c("train", "cutree")))
PredictionClust$new(task = task, partition = partition)
}
)
)
#' @include zzz.R
register_learner("clust.diana", LearnerClustDiana)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.