Skip to content

Instantly share code, notes, and snippets.

@adsieg
Created May 6, 2019 13:01
Show Gist options
  • Save adsieg/95e8437e00a21a85f22ba7134812ec6a to your computer and use it in GitHub Desktop.
Save adsieg/95e8437e00a21a85f22ba7134812ec6a to your computer and use it in GitHub Desktop.
Cosine Similarity
def cosine_distance_wordembedding_method(s1, s2):
import scipy
vector_1 = np.mean([model[word] for word in preprocess(s1)],axis=0)
vector_2 = np.mean([model[word] for word in preprocess(s2)],axis=0)
cosine = scipy.spatial.distance.cosine(vector_1, vector_2)
print('Word Embedding method with a cosine distance asses that our two sentences are similar to',round((1-cosine)*100,2),'%')
@alimehran200
Copy link

hi.
which modules i should import to run this code

@kgarg8
Copy link

kgarg8 commented Oct 28, 2021

why is similarity calculated as (1 - cosine) and not cosine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment