Scilab Website | Contribute with GitLab | Scilab Community | ATOMS toolboxes
Scilab Online Help
2027.0.0 - English


silhouette

Compute the silhouette values of a clustering

Calling Sequence

s = silhouette(X, labels)
s = silhouette(X, labels, metric)
s = silhouette(X, labels, metric, param)

Arguments

X

A double matrix of size n-by-p, where n is the number of samples/observations and p is the number of features.

labels

A double vector of length n containing the cluster label assigned to each sample. At least two distinct clusters must be present, and the number of clusters must be strictly less than n.

metric

A string specifying the distance metric used to compute pairwise distances between samples. Its default value is "euclidean". The supported metrics and their parameters are those of the pdist function.

param

An optional parameter passed to pdist along with the selected metric. Its default value is []. The accepted values depend on the selected metric.

s

A column vector of doubles of length n containing the silhouette coefficient of each sample, where n is the number of samples in X. Each element is in the range [-1, 1].

Description

The silhouette function computes the silhouette coefficient for each sample in a clustering.

The silhouette coefficient measures how similar a sample is to its own cluster compared with other clusters. It is based on the average distance between samples, using th edistance metric specified by metric.

For each sample i, two quantities are computed:

  • a(i): the mean distance between sample i and all other samples belonging to the same cluster. If the cluster contains only one sample, a(i) is set to zero.

  • b(i): the minimum, over all other clusters, of the mean distance between sample i and the samples belonging to that cluster.

The silhouette coefficient s(i) is defined as s(i) = (b(i) - a(i)) / max(a(i), b(i))

If both a(i) and b(i) are zero, the silhouette coefficient is set to zero.

The silhouette coefficient ranges from -1 to 1. A value close to 1 indicates that the sample is well matched to its own cluster and poorly matched to neighboring clusters. A value close to zero indicates that the sample lies near a cluster boundary. A negative value indicates that the sample may be closer, on average, to another cluster than its assigned cluster.

Examples

rand("seed", 0);
X1 = rand(20, 2);
X2 = rand(20, 2) + [3 0];
X3 = rand(20, 2) + [1.5 3];

X = [X1; X2; X3];
labels = [ones(20, 1); 2 * ones(20, 1); 3 * ones(20, 1)];

// compute the silhouette coefficients
s = silhouette(X, labels);
s(1:5)

// compute the score (average) silhouette
m = mean(s)
 
--> s(1:5)

 ans = [5x1 double]

   0.8639779
   0.7643916
   0.8208359
   0.8769499
   0.8803262

--> // compute the score (average) silhouette

--> m = mean(s)

 m = 

    0.8400074
   

rand("seed", 0);
data = [rand(30, 2) + 1; rand(30, 2) + 5; rand(30, 2) + 9];
idx = kmeans(data, 3);
s = silhouette(data, idx);
s(1:5)
 
--> s(1:5)

 ans = [5x1 double]

   0.9304514
   0.9230544
   0.9052566
   0.9317198
   0.9324727               
                

See Also

  • pdist — Pairwise distances between observations
  • squareform — Conversion between a distance vector and a symmetric distance matrix
  • kmeans — K-means clustering
  • dbscan — Density-based clustering (DBSCAN)
  • meanshift — Mean Shift clustering algorithm
  • agglomerative — Perform agglomerative hierarchical clustering

History

VersionDescription
2027.0.0 Function added.
Report an issue
<< polyval Statistics squareform >>

Copyright (c) 2022-2026 (Dassault Systèmes S.E.)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 15 13:01:45 CEST 2026