| KMedoids {banditpam} | R Documentation |
KMedoids Class
Description
This class wraps around the C++ KMedoids class and exposes methods and fields of the C++ object.
Active bindings
k(
integer(1))
The number of medoids/clusters to createmax_iter(
integer(1))
max_iter the maximum number of SWAP steps the algorithm runsbuild_conf(
integer(1))
Parameter that affects the width of BUILD confidence intervals, default 1000swap_conf(
integer(1))
Parameter that affects the width of SWAP confidence intervals, default 10000loss_fn(
character(1))
The loss function, "lp" (for p integer > 0) or one of "manhattan", "cosine", "inf" or "euclidean"
Methods
Public methods
Method new()
Create a new KMedoids object
Usage
KMedoids$new(
k = 5L,
algorithm = c("BanditPAM", "PAM", "FastPAM1"),
max_iter = 1000L,
build_conf = 1000,
swap_conf = 10000L
)Arguments
knumber of medoids/clusters to create, default 5
algorithmthe algorithm to use, one of "BanditPAM", "PAM", "FastPAM1"
max_iterthe maximum number of SWAP steps the algorithm runs, default 1000
build_confparameter that affects the width of BUILD confidence intervals, default 1000
swap_confparameter that affects the width of SWAP confidence intervals, default 10000
Returns
a KMedoids object which can be used to fit the banditpam algorithm to data
Method get_algorithm()
Return the algorithm used
Usage
KMedoids$get_algorithm()
Returns
a string indicating the algorithm
Method fit()
Fit the KMedoids algorthm given the data and loss. It is advisable to set the seed before calling this method for reproducible results.
Usage
KMedoids$fit(data, loss, dist_mat = NULL)
Arguments
datathe data matrix
lossthe loss function, either "lp" (p, integer indicating L_p loss) or one of "manhattan", "cosine", "inf" or "euclidean"
dist_matan optional distance matrix
Method get_medoids_final()
Return the final medoid indices after clustering
Usage
KMedoids$get_medoids_final()
Returns
a vector indices of the final mediods
Method get_statistic()
Get the specified statistic after clustering
Usage
KMedoids$get_statistic(what)
Arguments
whata string which should one of
"dist_computations","dist_computations_and_misc","misc_dist","build_dist","swap_dist","cache_writes","cache_hits", or"cache_misses"returnthe statistic
Method print()
Printer.
Usage
KMedoids$print(...)
Arguments
...(ignored).
Method clone()
The objects of this class are cloneable with this method.
Usage
KMedoids$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Generate data from a Gaussian Mixture Model with the given means:
set.seed(10)
n_per_cluster <- 40
means <- list(c(0, 0), c(-5, 5), c(5, 5))
X <- do.call(rbind, lapply(means, MASS::mvrnorm, n = n_per_cluster, Sigma = diag(2)))
obj <- KMedoids$new(k = 3)
obj$fit(data = X, loss = "l2")
meds <- obj$get_medoids_final()
plot(X[, 1], X[, 2])
points(X[meds, 1], X[meds, 2], col = "red", pch = 19)