The initialization is the same as seeded kmeans, the difference is that in the following steps the allocation of the clusters in the labelled data does not change

constrained_kmeans(max_iter = 10, method = "euclidean")

Arguments

max_iter

maximum iterations in KMeans. Default is 10

method

distance method in KMeans: "euclidean", "maximum", "manhattan", "canberra", "binary" or "minkowski"

References

Sugato Basu, Arindam Banerjee, Raymond Mooney
Semi-supervised clustering by seeding
July 2002 In Proceedings of 19th International Conference on Machine Learning

Examples

library(tidyverse) library(caret) library(SSLR) library(tidymodels) data <- iris set.seed(1) #% LABELED cls <- which(colnames(iris) == "Species") labeled.index <- createDataPartition(data$Species, p = .2, list = FALSE) data[-labeled.index,cls] <- NA m <- constrained_kmeans() %>% fit(Species ~ ., data) #Get labels (assing clusters), type = "raw" return factor labels <- m %>% cluster_labels() print(labels)
#> # A tibble: 150 x 1 #> .pred_class #> <fct> #> 1 1 #> 2 1 #> 3 1 #> 4 1 #> 5 1 #> 6 1 #> 7 1 #> 8 1 #> 9 1 #> 10 1 #> # ... with 140 more rows
#Get centers centers <- m %>% get_centers() print(centers)
#> [,1] [,2] [,3] [,4] #> [1,] 5.006000 3.428000 1.462000 0.246000 #> [2,] 5.850909 2.747273 4.320000 1.400000 #> [3,] 6.764444 3.024444 5.622222 2.013333