Categories &

Functions List

Function Reference: spectralcluster

statistics: idx = spectralcluster (X, k)
statistics: idx = spectralcluster (S, k, 'Distance', 'precomputed')
statistics: [idx, V] = spectralcluster (…)
statistics: [idx, V, D] = spectralcluster (…)
statistics: […] = spectralcluster (…, name, value)

Partition observations into k clusters using spectral clustering.

idx = spectralcluster (X, k) partitions the N×P numeric matrix X into k clusters and returns the N×1 vector idx of cluster indices. Rows of X correspond to observations and columns to features. Spectral clustering builds a similarity graph over the observations, embeds them with the eigenvectors of the graph Laplacian, and clusters that embedding, which lets it recover clusters that are not linearly separable in the original space.

[idx, V, D] = spectralcluster (…) also returns the N×k matrix V whose columns are the eigenvectors associated with the k smallest eigenvalues of the Laplacian, and the k×1 vector D of those eigenvalues. The signs of the eigenvectors, and the basis within a repeated eigenvalue, are arbitrary.

Additional parameters can be specified by Name-Value pair arguments.

NameValue
'Distance'the distance metric used to build the similarity graph, one of 'euclidean' (default), 'seuclidean', 'mahalanobis', 'cityblock', 'minkowski', 'chebychev', 'cosine', 'correlation', 'hamming', 'jaccard', 'spearman', or a function handle accepted by pdist2, or the string 'precomputed' to interpret the first input as an N×N similarity matrix.
'SimilarityGraph''knn' (default) to connect each observation to its nearest neighbors, or 'epsilon' to connect observations that are within a fixed radius.
'NumNeighbors'the number of nearest neighbors for the 'knn' graph, a positive integer. The default is ceil (log (N)).
'KNNGraphType''complete' (default) to connect i and j when either is a nearest neighbor of the other, or 'mutual' to connect them only when each is a nearest neighbor of the other.
'Radius'the radius for the 'epsilon' graph, a nonnegative scalar. Required when 'SimilarityGraph' is 'epsilon'.
'KernelScale'the positive scale factor sigma in the Gaussian similarity kernel exp (-(dist / sigma)^2) applied to the graph edges. The default is 1.
'LaplacianNormalization''randomwalk' (default), 'symmetric', or 'none', selecting how the graph Laplacian is normalized before the eigendecomposition.
'ClusterMethod''kmeans' (default) or 'kmedoids' to cluster the eigenvector embedding.
'P'the Minkowski exponent (default 2), used only with the 'minkowski' distance.
'Cov'the covariance matrix used only with the 'mahalanobis' distance.
'Scale'the scaling vector used only with the 'seuclidean' distance.

Source Code: spectralcluster

See also: kmeans, kmedoids, dbscan, linkage, pdist2

Source Code: spectralcluster

Two concentric rings are not separable by kmeans but are by spectral clustering.

 t = linspace (0, 2*pi, 100)';
 Xin  = [cos(t), sin(t)] + randn (100, 2) * 0.05;
 Xout = 4 * [cos(t), sin(t)] + randn (100, 2) * 0.05;
 X = [Xin; Xout];
 idx = spectralcluster (X, 2, "NumNeighbors", 10);
 gscatter (X(:,1), X(:,2), idx);
 axis equal;
 title ("spectralcluster: two concentric rings");
plotted figure