0

I am doing some clustering using the scikit-learn (aka sklearn) and everything worked fine until I tried to play with the gamma parameter of the spectral_clustering function.

I am using sklearn (0.18.1) with python (3.5.2) in a virtual environment manage with anaconda (1.5.1).

In the official documentation of the function, there is mention of a parameter gamma :

class sklearn.cluster.SpectralClustering(n_clusters=8, eigen_solver=None, random_state=None, n_init=10, gamma=1.0, affinity='rbf', n_neighbors=10, eigen_tol=0.0, assign_labels='kmeans', degree=3, coef0=1, kernel_params=None, n_jobs=1)

However, on my machine when I try to pass the parameter gamma to the function, I get the following error:

TypeError: spectral_clustering() got an unexpected keyword argument 'gamma'

And then, when I display the help page of the function help(spectral_clustering), I got completely different information than on the official documentation :

spectral_clustering(affinity, n_clusters=8, n_components=None, eigen_solver=None, random_state=None, n_init=10, eigen_tol=0.0, assign_labels='kmeans')

It is like some parameters are mising. I thought maybe it is coming from the conda package but installing it with pip doesn't solve the problem.

I even tried to install it outside of a virtual environment and still a similar problem.

However, I asked two of my colleagues and one had the same problem as me while the other one could see the same information in the help(spectral_clustering) function as in the official documentation.

How can I get the same functionalities as in the official documentation?

I am using the spectral_clustering function in one of my pipeline as the following:

if clt_mtd == 'spectral':
    # Spectral clustering
    labels = spectral_clustering(sm, n_clusters=num_clt, random_state=seed, gamma=0.1)

This function is working perfectly fine as long I don't try to modify the gamma parameter.


Solved: My mistake is coming from one of the official example that I used as a reference.

Solution: As discussed in the commentaries below, SpectralClustering and spectral_clustering are not the same. The last one is used by the function fit in the class SpectralClustering and doesn't take gamma as a parameter as this one is used before in the function fit.

Therefore, to be able to modify the gamma parameter one should instantiate a SpectralClustering object first and use the function fit provided by this class.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    There is the class ```SpectralClustering``` and there is the function ```spectral_clustering```. The latter is used internally and corresponds to your help call. Show the code of what you are doing exactly. (Actually it already looks like you are calling the function with the gamma-param which is wrong. You have to call the class and later fit, like always in sklearn!) – sascha Nov 17 '16 at 15:23
  • @sascha : I added an Edit with the way I am using the function. So if I understand correctly what you are saying, I can't really modify gamma from the function call because it is not a parameter of the function `spectral_clustering` but a parameter of the class `SpectralClustering`. Is it correct ? – Lucas Vandroux Nov 17 '16 at 15:37
  • Just follow the docs and use the class-based SpectralClustering, where the gamma is part of the docs. – sascha Nov 17 '16 at 15:43
  • @sascha, thanks a lot, you are absolutely right. I checked in the source code and the function `spectral_clustering` is used by the function `fit` of the class `SpectralClustering` but without the `gamma` parameter as it is used previously in the function `fit`. – Lucas Vandroux Nov 17 '16 at 15:51

0 Answers0