Search code examples
pythonscikit-learncluster-analysis

SpectralClustering vs. spectral_clustering in scikit-learn


I noticed that there are two different functions for spectral clustering in sklearn.cluster library: SpectralClustering and spectral_clustering. Although they differ in some details, both do spectral clustering and most of their parameters overlap. I am confused about why there are two methods so similar in sklearn?

Some differences I noticed:

  • In SpectralClustering, parameter affinity takes both string and array; its default value is 'rbf'; in spectral_clustering it can only be a matrix

  • SpectralClustering() works like a constructor. It doesn't return anything but has two attributes affinity_matrix_(which you can access after calling .fit()) and labels_. spectral_clustering is a method that only returns the labels.

Using SpectralClustering:

cluster=SpectralClustering().fit(X)
cluster.labels_

Using spectral_clustering:

labels=spectral_clustering(affinity_matrix)

Despite these apparent differences, I'm wondering whether these two methods differ in fundamental aspects. Otherwise why are there two methods that accomplish basically the same thing?


Solution

  • Did you check the source code?

    I'd expect that SpectralClustering is an object oriented wrapper for the imperative method spectral_clustering.