SpectralClustering vs. spectral_clustering in scikit-learn
Asked Answered
K

1

0

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?

Kylander answered 13/4, 2019 at 21:40 Comment(0)
G
0

Did you check the source code?

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

Glaive answered 17/4, 2019 at 6:3 Comment(1)
Thank you for reminding me of looking at the source code. You are absolutely right. SpectralClustering basically computes the affinity matrix by itself and then calls spectral_clustering(Affinity_Matrix).Kylander

© 2022 - 2024 — McMap. All rights reserved.