Cluster word2vec vectors using Affinity Propagation in python (sklearn)

Viewed 1771

I want to cluster my word2vec clusters using Affinity Propagation and get the cluster center words.

My current code is as follows.

model = word2vec.Word2Vec.load("word2vec")
word_vectors = model.wv.syn0
affprop = AffinityPropagation(affinity="precomputed", damping=0.5)
af= affprop.fit(word_vectors)

However, this raise the following error: ValueError: S must be a square array (shape=(77, 300))

As I understand 300 means word2vec hidden layer dimension and 77 is my vocabulary size.

I am just wondering how to use Affinity Propagation for word2vec vectors that are not square arrays.

Please help me!

1 Answers

It's because you stated that the affinity between samples is already computed, which is always a square matrix. So you can either use Euclidean distance which is implemented, or if you want to use a different metric you have to precompute it, see the example code below:

# some dummy data
word_vectors = np.random.random((77, 300))

# using eucliden distance
affprop = AffinityPropagation(affinity='euclidean', damping=0.5)
af = affprop.fit(word_vectors)

# using cosine
from sklearn.metrics.pairwise import cosine_distances
word_cosine = cosine_distances(word_vectors)
affprop = AffinityPropagation(affinity='precomputed', damping=0.5)
af = affprop.fit(word_cosine)
Related