Documentation
Classes
SpectralClustering

SpectralClustering

Apply clustering to a projection of the normalized Laplacian.

In practice Spectral Clustering is very useful when the structure of the individual clusters is highly non-convex, or more generally when a measure of the center and spread of the cluster is not a suitable description of the complete cluster, such as when clusters are nested circles on the 2D plane.

If the affinity matrix is the adjacency matrix of a graph, this method can be used to find normalized graph cuts [1], [2].

When calling fit, an affinity matrix is constructed using either a kernel function such the Gaussian (aka RBF) kernel with Euclidean distance d(X, X):

Python Reference (opens in a new tab)

Constructors

constructor()

Signature

new SpectralClustering(opts?: object): SpectralClustering;

Parameters

NameTypeDescription
opts?object-
opts.affinity?string‘nearest_neighbors’: construct the affinity matrix by computing a graph of nearest neighbors. Default Value 'rbf'
opts.assign_labels?"kmeans" | "discretize" | "cluster_qr"The strategy for assigning labels in the embedding space. There are two ways to assign labels after the Laplacian embedding. k-means is a popular choice, but it can be sensitive to initialization. Discretization is another approach which is less sensitive to random initialization [3]. The cluster_qr method [5] directly extract clusters from eigenvectors in spectral clustering. In contrast to k-means and discretization, cluster_qr has no tuning parameters and runs no iterations, yet may outperform k-means and discretization in terms of both quality and speed. Default Value 'kmeans'
opts.coef0?numberZero coefficient for polynomial and sigmoid kernels. Ignored by other kernels. Default Value 1
opts.degree?numberDegree of the polynomial kernel. Ignored by other kernels. Default Value 3
opts.eigen_solver?"arpack" | "lobpcg" | "amg"The eigenvalue decomposition strategy to use. AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities. If undefined, then 'arpack' is used. See [4] for more details regarding 'lobpcg'.
opts.eigen_tol?numberStopping criterion for eigendecomposition of the Laplacian matrix. If eigen\_tol="auto" then the passed tolerance will depend on the eigen\_solver: Default Value 'auto'
opts.gamma?numberKernel coefficient for rbf, poly, sigmoid, laplacian and chi2 kernels. Ignored for affinity='nearest\_neighbors'. Default Value 1
opts.kernel_params?anyParameters (keyword arguments) and values for kernel passed as callable object. Ignored by other kernels.
opts.n_clusters?numberThe dimension of the projection subspace. Default Value 8
opts.n_components?numberNumber of eigenvectors to use for the spectral embedding. If undefined, defaults to n\_clusters.
opts.n_init?numberNumber of time the k-means algorithm will be run with different centroid seeds. The final results will be the best output of n_init consecutive runs in terms of inertia. Only used if assign\_labels='kmeans'. Default Value 10
opts.n_jobs?numberThe number of parallel jobs to run when affinity='nearest\_neighbors' or affinity='precomputed\_nearest\_neighbors'. The neighbors search will be done in parallel. undefined means 1 unless in a joblib.parallel\_backend (opens in a new tab) context. \-1 means using all processors. See Glossary for more details.
opts.n_neighbors?numberNumber of neighbors to use when constructing the affinity matrix using the nearest neighbors method. Ignored for affinity='rbf'. Default Value 10
opts.random_state?numberA pseudo random number generator used for the initialization of the lobpcg eigenvectors decomposition when eigen\_solver \== 'amg', and for the K-Means initialization. Use an int to make the results deterministic across calls (See Glossary).
opts.verbose?booleanVerbosity mode. Default Value false

Returns

SpectralClustering

Defined in: generated/cluster/SpectralClustering.ts:27 (opens in a new tab)

Properties

_isDisposed

boolean = false

Defined in: generated/cluster/SpectralClustering.ts:25 (opens in a new tab)

_isInitialized

boolean = false

Defined in: generated/cluster/SpectralClustering.ts:24 (opens in a new tab)

_py

PythonBridge

Defined in: generated/cluster/SpectralClustering.ts:23 (opens in a new tab)

id

string

Defined in: generated/cluster/SpectralClustering.ts:20 (opens in a new tab)

opts

any

Defined in: generated/cluster/SpectralClustering.ts:21 (opens in a new tab)

Accessors

affinity_matrix_

Affinity matrix used for clustering. Available only after calling fit.

Signature

affinity_matrix_(): Promise<ArrayLike[]>;

Returns

Promise<ArrayLike[]>

Defined in: generated/cluster/SpectralClustering.ts:299 (opens in a new tab)

feature_names_in_

Names of features seen during fit. Defined only when X has feature names that are all strings.

Signature

feature_names_in_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/cluster/SpectralClustering.ts:380 (opens in a new tab)

labels_

Labels of each point

Signature

labels_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/cluster/SpectralClustering.ts:326 (opens in a new tab)

n_features_in_

Number of features seen during fit.

Signature

n_features_in_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/cluster/SpectralClustering.ts:353 (opens in a new tab)

py

Signature

py(): PythonBridge;

Returns

PythonBridge

Defined in: generated/cluster/SpectralClustering.ts:127 (opens in a new tab)

Signature

py(pythonBridge: PythonBridge): void;

Parameters

NameType
pythonBridgePythonBridge

Returns

void

Defined in: generated/cluster/SpectralClustering.ts:131 (opens in a new tab)

Methods

dispose()

Disposes of the underlying Python resources.

Once dispose() is called, the instance is no longer usable.

Signature

dispose(): Promise<void>;

Returns

Promise<void>

Defined in: generated/cluster/SpectralClustering.ts:200 (opens in a new tab)

fit()

Perform spectral clustering from features, or affinity matrix.

Signature

fit(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeTraining instances to cluster, similarities / affinities between instances if affinity='precomputed', or distances between instances if affinity='precomputed\_nearest\_neighbors. If a sparse matrix is provided in a format other than csr\_matrix, csc\_matrix, or coo\_matrix, it will be converted into a sparse csr\_matrix.
opts.y?anyNot used, present here for API consistency by convention.

Returns

Promise<any>

Defined in: generated/cluster/SpectralClustering.ts:217 (opens in a new tab)

fit_predict()

Perform spectral clustering on X and return cluster labels.

Signature

fit_predict(opts: object): Promise<ArrayLike>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeTraining instances to cluster, similarities / affinities between instances if affinity='precomputed', or distances between instances if affinity='precomputed\_nearest\_neighbors. If a sparse matrix is provided in a format other than csr\_matrix, csc\_matrix, or coo\_matrix, it will be converted into a sparse csr\_matrix.
opts.y?anyNot used, present here for API consistency by convention.

Returns

Promise<ArrayLike>

Defined in: generated/cluster/SpectralClustering.ts:257 (opens in a new tab)

init()

Initializes the underlying Python resources.

This instance is not usable until the Promise returned by init() resolves.

Signature

init(py: PythonBridge): Promise<void>;

Parameters

NameType
pyPythonBridge

Returns

Promise<void>

Defined in: generated/cluster/SpectralClustering.ts:140 (opens in a new tab)