Measures silhouette between clusters
Usage
silhouette(
object,
new_data = NULL,
dists = NULL,
dist_fun = philentropy::distance
)Arguments
- object
A fitted tidyclust model
- new_data
A dataset to predict on. If
NULL, uses trained clustering.- dists
A distance matrix. Used if
new_dataisNULL.- dist_fun
A function of the form
function(x)that takes a data frame or matrix and returns adistobject. Defaults tophilentropy::distancewith Euclidean distance. Seephilentropy::getDistMethods()for a list of supported methods, andvignette("tuning_and_metrics", package = "tidyclust")for usage examples.
Details
silhouette_avg() is the corresponding cluster metric function that
returns the average of the values given by silhouette().
Examples
kmeans_spec <- k_means(num_clusters = 5) |>
set_engine("stats")
kmeans_fit <- fit(kmeans_spec, ~., mtcars)
dists <- mtcars |>
as.matrix() |>
dist()
silhouette(kmeans_fit, dists = dists)
#> # A tibble: 32 × 3
#> cluster neighbor sil_width
#> <fct> <fct> <dbl>
#> 1 Cluster_1 Cluster_2 0.572
#> 2 Cluster_1 Cluster_2 0.572
#> 3 Cluster_1 Cluster_2 0.752
#> 4 Cluster_2 Cluster_1 0.540
#> 5 Cluster_3 Cluster_4 0.149
#> 6 Cluster_2 Cluster_1 0.224
#> 7 Cluster_3 Cluster_4 0.649
#> 8 Cluster_1 Cluster_2 0.613
#> 9 Cluster_1 Cluster_2 0.692
#> 10 Cluster_1 Cluster_2 0.460
#> # ℹ 22 more rows
