silhouette_avg: Measures average silhouette across all observations

View source: R/metric-silhouette.R

silhouette_avgR Documentation

Measures average silhouette across all observations

Description

Measures average silhouette across all observations

Usage

silhouette_avg(object, ...)

## S3 method for class 'cluster_spec'
silhouette_avg(object, ...)

## S3 method for class 'cluster_fit'
silhouette_avg(object, new_data = NULL, dists = NULL, dist_fun = NULL, ...)

## S3 method for class 'workflow'
silhouette_avg(object, new_data = NULL, dists = NULL, dist_fun = NULL, ...)

silhouette_avg_vec(
  object,
  new_data = NULL,
  dists = NULL,
  dist_fun = philentropy::distance,
  ...
)

Arguments

object

A fitted kmeans tidyclust model

...

Other arguments passed to methods.

new_data

A dataset to predict on. If NULL, uses trained clustering.

dists

A distance matrix. Used if new_data is NULL.

dist_fun

A function of the form ⁠function(x)⁠ that takes a data frame or matrix and returns a dist object. Defaults to philentropy::distance with Euclidean distance. See philentropy::getDistMethods() for a list of supported methods, and vignette("tuning_and_metrics", package = "tidyclust") for usage examples.

Details

Not to be confused with silhouette() that returns a tibble with silhouette for each observation. The silhouette coefficient ranges from -1 to 1, where values close to 1 indicate well-separated clusters. This metric has direction = "maximize", so tune::select_best() and tune::show_best() will return models with the highest silhouette values.

Value

A double; the average silhouette.

See Also

Other cluster metric: sse_ratio(), sse_total(), sse_within_total()

Examples

kmeans_spec <- k_means(num_clusters = 5) |>
  set_engine("stats")

kmeans_fit <- fit(kmeans_spec, ~., mtcars)

dists <- mtcars |>
  as.matrix() |>
  dist()

silhouette_avg(kmeans_fit, dists = dists)

silhouette_avg_vec(kmeans_fit, dists = dists)

tidyclust documentation built on June 20, 2026, 9:08 a.m.