Nothing
#' @title Mini Batch K-Means Clustering Learner
#'
#' @name mlr_learners_clust.MBatchKMeans
#' @include LearnerClust.R
#'
#' @description
#' Mini-batch k-means clustering.
#' Calls [ClusterR::MiniBatchKmeans()] from package \CRANpkg{ClusterR}.
#'
#' The `clusters` parameter is set to 2 by default since [ClusterR::MiniBatchKmeans()] doesn't have a default value for
#' the number of clusters. The predict method uses [ClusterR::predict_KMeans()] on the fitted centroids to compute the
#' cluster memberships for new data. The learner supports both partitional and fuzzy clustering.
#'
#' @templateVar id clust.MBatchKMeans
#' @template learner
#'
#' @references
#' `r format_bib("sculley2010web")`
#'
#' @export
#' @template seealso_learner
#' @template example
LearnerClustMiniBatchKMeans = R6Class(
"LearnerClustMiniBatchKMeans",
inherit = LearnerClust,
public = list(
#' @description
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function() {
param_set = ps(
clusters = p_int(1L, tags = c("train", "required")),
batch_size = p_int(1L, default = 10L, tags = "train"),
num_init = p_int(1L, default = 1L, tags = "train"),
max_iters = p_int(1L, default = 100L, tags = "train"),
init_fraction = p_dbl(
lower = 0,
upper = 1,
default = 1,
tags = "train",
depends = quote(initializer %in% c("kmeans++", "optimal_init"))
),
initializer = p_fct(
c("optimal_init", "quantile_init", "kmeans++", "random"),
default = "kmeans++",
tags = "train"
),
early_stop_iter = p_int(1L, default = 10L, tags = "train"),
verbose = p_lgl(default = FALSE, tags = "train"),
CENTROIDS = p_uty(default = NULL, tags = "train"),
tol = p_dbl(0, default = 1e-04, tags = "train"),
tol_optimal_init = p_dbl(0, default = 0.3, tags = "train"),
seed = p_int(default = 1L, tags = "train"),
threads = p_int(1L, default = 1L, tags = c("predict", "threads"))
)
param_set$set_values(clusters = 2L)
super$initialize(
id = "clust.MBatchKMeans",
feature_types = c("logical", "integer", "numeric"),
predict_types = c("partition", "prob"),
param_set = param_set,
properties = c("partitional", "exclusive", "complete"),
packages = "ClusterR",
man = "mlr3cluster::mlr_learners_clust.MBatchKMeans",
label = "Mini Batch K-Means"
)
}
),
private = list(
.train = function(task) {
pv = self$param_set$get_values(tags = "train")
assert_centers_param(pv$CENTROIDS, task, "CENTROIDS")
if (test_matrix(pv$CENTROIDS) && nrow(pv$CENTROIDS) != pv$clusters) {
error_config("`CENTROIDS` must have same number of rows as `clusters`.")
}
data = task$data()
m = invoke(ClusterR::MiniBatchKmeans, data = data, .args = pv)
if (self$save_assignments) {
pv_predict = self$param_set$get_values(tags = "predict")
self$assignments = as.integer(
invoke(ClusterR::predict_KMeans, data = data, CENTROIDS = m$centroids, .args = pv_predict)
)
}
m
},
.predict = function(task) {
pv = self$param_set$get_values(tags = "predict")
data = ordered_features(task, self)
centroids = self$model$centroids
partition = as.integer(invoke(ClusterR::predict_KMeans, data = data, CENTROIDS = centroids, .args = pv))
prob = NULL
if (self$predict_type == "prob") {
prob = invoke(ClusterR::predict_KMeans, data = data, CENTROIDS = centroids, fuzzy = TRUE, .args = pv)
colnames(prob) = seq_col(prob)
}
list(partition = partition, prob = prob)
}
)
)
#' @include zzz.R
register_learner("clust.MBatchKMeans", LearnerClustMiniBatchKMeans)
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.