Issue
I am trying to use manhattan distance for SpectralClustering()
in Sklearn. I am trying to set the affinity
parameter to be manhattan
, but getting the following error.
ValueError: Unknown kernel 'manhattan'
What is the proper kernel name should I use for it? Anyone can help? Basically, I want to use SpectralClustering
to realize kmeans
using manhattan
distance metric.
Here the line of code for setting SpectralClustering()
:
clustering = SpectralClustering(n_clusters=10, affinity='manhattan', assign_labels="kmeans")
clustering.fit(X)
Solution
Manhattan distance is not supported in sklearn.metrics.pairwise_kernels
that is the reason for the ValueError.
Valid values for metric are::
[‘rbf’, ‘sigmoid’, ‘polynomial’, ‘poly’, ‘linear’, ‘cosine’]
linear
and manhattan
distance metric are different, you could understand from the example mentioned here:
>>> import numpy as np
>>> from sklearn.metrics import pairwise_distances
>>> from sklearn.metrics.pairwise import pairwise_kernels
>>> X = np.array([[2, 3], [3, 5], [5, 8]])
>>> Y = np.array([[1, 0], [2, 1]])
>>> pairwise_distances(X, Y, metric='manhattan')
array([[ 4., 2.],
[ 7., 5.],
[12., 10.]])
>>> pairwise_kernels(X, Y, metric='linear')
array([[ 2., 7.],
[ 3., 11.],
[ 5., 18.]])
Manhattan distance function is available under sklearn.metrics.pairwise_distance
Now, the simpler way to use manhattan distance measure with spectral cluster would be,
>>> from sklearn.cluster import SpectralClustering
>>> from sklearn.metrics import pairwise_distances
>>> import numpy as np
>>> X = np.array([[1, 1], [2, 1], [1, 0],
... [4, 7], [3, 5], [3, 6]])
>>> X_precomputed = pairwise_distances(X, metric='manhattan')
>>> clustering = SpectralClustering(n_clusters=2, affinity='precomputed', assign_labels="discretize",random_state=0)
>>> clustering.fit(X_precomputed)
>>> clustering.labels_
>>> clustering
Answered By - Venkatachalam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.