Manhattan distance is not supported in sklearn.metrics.pairwise_kernels
that is the reason for the ValueError.
From Documentation:
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