kmeans_spec <- k_means(num_clusters = 5) %>%
set_engine("stats")
kmeans_fit <- fit(kmeans_spec, ~., mtcars)
kmeans_fit %>%
extract_cluster_assignment()
kmeans_fit %>%
extract_cluster_assignment(prefix = "C_")
# Some models such as `hier_clust()` fits in such a way that you can specify
# the number of clusters after the model is fit
hclust_spec <- hier_clust() %>%
set_engine("stats")
hclust_fit <- fit(hclust_spec, ~., mtcars)
hclust_fit %>%
extract_cluster_assignment(num_clusters = 2)
hclust_fit %>%
extract_cluster_assignment(cut_height = 250)
Run the code above in your browser using DataLab