| new_cluster_metric | R Documentation |
This function provides a convenient wrapper to create the one
type of metric function used in tidyclust: clustering metrics. It adds a
metric-specific class to fn. These features are used by
cluster_metric_set() and by tune_cluster() when tuning.
Use new_cluster_metric() when you want to author your own clustering
metric, for example to call silhouette_avg() with a non-default
dist_fun. A plain function cannot be passed to cluster_metric_set()
directly; it must first be wrapped with new_cluster_metric() so that it
carries the cluster_metric class.
new_cluster_metric(fn, direction)
fn |
A function. It should take |
direction |
A string. One of:
|
A cluster_metric object.
cluster_metric_set()
# Author a custom metric that uses a non-default distance function. Here we
# use the average silhouette with Chebyshev (L-infinity) distance.
linf_dist <- function(x) philentropy::distance(x, method = "chebyshev")
linf_silhouette_avg <- new_cluster_metric(
function(object, new_data = NULL, ...) {
silhouette_avg(object, new_data = new_data, dist_fun = linf_dist, ...)
},
direction = "maximize"
)
# The custom metric can now be combined with others in a metric set.
cluster_metric_set(linf_silhouette_avg, sse_ratio)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.