Impossible to set the gamma parameter for the spectral_clustering function in sklearn 0.18.1

682 Views Asked by At

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.

0

There are 0 best solutions below