Download our e-book of Introduction To Python
Shahid Ahmed
2 years ago
sentences = [
['Word', 'embeddings', 'work', 'by', 'using', 'an', 'algorithm'],
['this', 'is', 'the', 'InsideAIML', 'website'],
['you', 'can', 'read', 'technical','articles', 'for','free'],
['We', 'will', 'use', 'the', 'Gensim', 'library'],
['learn', 'python', 'programming', 'on', 'insideaiml']
]
from gensim.models import Word2Vec
sentences = [
['Word', 'embeddings', 'work', 'by', 'using', 'an', 'algorithm'],
['this', 'is', 'the', 'InsideAIML', 'website'],
['you', 'can', 'read', 'technical','articles', 'for','free'],
['We', 'will', 'use', 'the', 'Gensim', 'library'],
['learn', 'python', 'programming', 'on', 'insideaiml']
]
W2V_model = Word2Vec(sentences, min_count=1)
print(W2V_model)
words = list(W2V_model.wv.vocab)
print(words)
print(W2V_model['insideaiml'])
W2V_model.save('W2V_model.bin')
Word2Vec_model = Word2Vec.load('W2V_model.bin')
print(Word2Vec_model)
Word2Vec(vocab=29, size=100, alpha=0.025)
['Word', 'embeddings', 'work', 'by', 'using', 'an', 'algorithm', 'this', 'is', 'the', 'InsideAIML', 'website', 'you', 'can', 'read', 'technical', 'articles', 'for', 'free', 'We', 'will', 'use', 'Gensim', 'library', 'learn', 'python', 'programming', 'on', 'insideaiml']
[ 2.3622620e-03 4.3221487e-04 3.9335857e-03 -1.2020235e-03
-9.9151593e-04 2.8309512e-03 4.7964812e-03 -3.7568363e-03
-4.9456498e-03 3.6903718e-03 -4.8737871e-03 -3.9381068e-03
2.5999357e-03 1.3458870e-03 2.2719600e-03 -1.9624005e-03
3.5575717e-03 4.6965261e-03 -6.2980008e-04 -3.6406862e-03
-3.5829267e-03 1.6928543e-03 1.4477138e-03 1.1637001e-03
-4.7915865e-04 1.3976435e-03 -2.3567895e-03 2.9160331e-03
-4.4381022e-03 2.0105252e-03 -2.9324128e-03 1.6421793e-03
-5.0091086e-04 2.3349845e-03 -4.1118253e-04 -9.2874817e-04
4.4296873e-03 -3.4641903e-03 4.3619485e-03 4.7739753e-03
4.8495419e-03 3.6664470e-03 3.8093987e-03 3.6490641e-03
-3.3609912e-04 2.9555541e-03 -1.0483260e-03 -4.3996158e-03
2.6523159e-03 -3.4169867e-03 1.3806688e-03 -6.9535966e-04
-5.6781049e-04 3.5429434e-03 -1.8909144e-03 3.0394471e-03
4.1374662e-03 -4.5139138e-03 4.5683607e-03 2.7697829e-03
2.3550272e-03 1.3603187e-03 -4.5494111e-03 7.6852361e-04
-4.8047729e-04 -2.4365645e-03 4.2462661e-03 2.0318357e-03
-1.9684029e-03 1.5639960e-03 -4.5757894e-03 2.1069648e-03
-3.5330481e-03 -1.3349410e-03 1.9695498e-03 3.1291901e-03
4.7138124e-03 -2.2136174e-04 -2.9766995e-03 -4.5496337e-03
-3.2605783e-03 1.5357189e-03 -1.9210422e-03 -1.8419328e-03
3.9830280e-05 2.9295796e-04 -4.0149586e-03 -4.4272095e-03
5.2146171e-04 3.7140078e-03 -3.3862747e-03 -6.4570026e-04
-4.8357933e-03 3.9663548e-03 3.4471180e-03 3.9999108e-04
2.2896260e-03 4.4800160e-03 3.8771254e-03 -1.2966482e-03]
Word2Vec(vocab=29, size=100, alpha=0.025)
from gensim.models import Word2Vec
from sklearn.decomposition import PCA
from matplotlib import pyplot
sentences = [
['Word', 'embeddings', 'work', 'by', 'using', 'an', 'algorithm'],
['this', 'is', 'the', 'InsideAIML', 'website'],
['you', 'can', 'read', 'technical','articles', 'for','free'],
['We', 'will', 'use', 'the', 'Gensim', 'library'],
['learn', 'python', 'programming', 'on', 'insideaiml']
]
W2V_model = Word2Vec(sentences, min_count=1)
vec = W2V_model[W2V_model.wv.vocab]
pca = PCA(n_components=2)
result = pca.fit_transform(vec)
pyplot.scatter(result[:, 0], result[:, 1])
words = list(W2V_model.wv.vocab)
for i, word in enumerate(words):
pyplot.annotate(word, xy=(result[i, 0], result[i, 1]))
pyplot.show()