0

I am using Affinity Propogation to cluster my similarity matrixsims. My code is as follows. According to an answer of my previous question I am using SpectralEmbedding to plot my data points of the similarity matrix sims.

import sklearn.cluster
from sklearn.manifold import SpectralEmbedding
import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle

sims =  np.array([[0, 17, 10, 32, 32], [18, 0, 6, 20, 15], [10, 8, 0, 20, 21], [30, 16, 20, 0, 17], [30, 15, 21, 17, 0]])

affprop = sklearn.cluster.AffinityPropagation(affinity="precomputed", damping=0.5)
affprop.fit(sims)

cluster_centers_indices = affprop.cluster_centers_indices_
print(cluster_centers_indices)
labels = affprop.labels_
n_clusters_ = len(cluster_centers_indices)
print(n_clusters_)

se = SpectralEmbedding(n_components=2, affinity='precomputed')
X = se.fit_transform(sims)

plt.close('all')
plt.figure(1)
plt.clf()

colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
for k, col in zip(range(n_clusters_), colors):
    class_members = labels == k
    cluster_center = X[cluster_centers_indices[k]]
    plt.plot(X[class_members, 0], X[class_members, 1], col + '.')
    plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
             markeredgecolor='k', markersize=14)
    for x in X[class_members]:
        plt.plot([cluster_center[0], x[0]], [cluster_center[1], x[1]], col)

plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()       

However, I do not understand what exactly happens with SpectralEmbedding. Please let me know what it does? And is it correct to use SpectralEmbedding to plot similarity values?

  • What's wrong with the [tutorial](http://scikit-learn.org/stable/modules/manifold.html#spectral-embedding)? What do you expect here. Mostly all embeddings are quite complex (compare with t-SNE) and SO is really not about going that deep into the theory. – sascha Sep 18 '17 at 01:30
  • @sascha Thank you for the comment. I actually tried to understand the tutorial. However, I could not as I am new to machine learning. I actually want to verify, if it is correct to use SpectralEmbedding according to my situation? –  Sep 18 '17 at 01:42
  • 1
    @Volka Your approach seems okay to me. In brief, the spectral clustering uses the Laplacian matrix of the data. It decompose the matrix and using the eigenvectors it maps the data to another representation (reduced dimensions). So its a way to cluster data based on the decomposition of the Laplacian matrix. – seralouk Sep 18 '17 at 10:36
  • @sera Thank you very much for your very useful comment. I actually want to plot my affinity propagation cluster outputs correctly. Is it achieved through the use of `SpectralEmbedding`? :) –  Sep 18 '17 at 11:29

0 Answers0