set.seed(1234)
kmeans_spec <- k_means(num_clusters = 5) %>%
set_engine("stats")
kmeans_fit <- fit(kmeans_spec, ~., mtcars)
kmeans_fit %>%
extract_centroids()
# Some models such as `hier_clust()` fits in such a way that you can specify
# the number of clusters after the model is fit.
# A Hierarchical (Agglomerative) Clustering method doesn't technically have
# clusters, so the center of the observation within each cluster is returned
# instead.
hclust_spec <- hier_clust() %>%
set_engine("stats")
hclust_fit <- fit(hclust_spec, ~., mtcars)
hclust_fit %>%
extract_centroids(num_clusters = 2)
hclust_fit %>%
extract_centroids(cut_height = 250)
Run the code above in your browser using DataLab