Cosine similarity measures the cosine of the angle between two vectors. It shows how similar their directions are, ignoring their lengths. This is useful in word embeddings because words with similar meanings tend to point in similar directions.
import numpy as np king = np.array([0.8, 0.6]) man = np.array([0.7, 0.5]) woman = np.array([0.6, 0.7]) result = king - man + woman print(result)
Subtracting 'man' from 'king' and then adding 'woman' results in [0.8-0.7+0.6, 0.6-0.5+0.7] = [0.7, 0.8].
Word2Vec Skip-gram learns dense vectors where semantic relationships are encoded as linear offsets, enabling analogy tasks. Bag-of-Words and TF-IDF do not capture such relationships, and one-hot encoding is sparse and does not encode meaning.
Spearman rank correlation measures how well the order of similarity scores from the model matches the order of human judgments, which is ideal for similarity tasks. Mean squared error measures exact value differences, which is less meaningful here.
The code searches over all words including 'king', 'man', and 'woman'. Since the result vector is close to these, it may return one of them instead of the intended analogy word. Excluding input words from the search fixes this.