Skip to content

Instantly share code, notes, and snippets.

@therusetiawan
Created June 16, 2023 03:53
Show Gist options
  • Select an option

  • Save therusetiawan/78de62f5f2481b6c23b37e70d19fb643 to your computer and use it in GitHub Desktop.

Select an option

Save therusetiawan/78de62f5f2481b6c23b37e70d19fb643 to your computer and use it in GitHub Desktop.
Visualize fasttext vector using tsne
import fasttext
import numpy as np
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
model = fasttext.load_model('models/cc.en.300.bin')
# static keywords
keywords = ['deeds', 'acquisitions', 'skills', 'acquirements', 'attainments', 'achievements']
keyword_vectors = []
for keyword in keywords:
vec = model.get_word_vector(keyword)
keyword_vectors.append(vec)
keyword_vectors = np.array(keyword_vectors)
tsne = TSNE(n_components = 2, random_state = 0, n_iter = 10000, perplexity = 2)
np.set_printoptions(suppress = True)
T = tsne.fit_transform(keyword_vectors)
labels = keywords
plt.figure(figsize = (14, 8))
plt.scatter(T[:, 0], T[:, 1], c = 'blue', edgecolors = 'r')
for label, x, y in zip(labels, T[:, 0], T[:, 1]):
plt.annotate(label, xy = (x + 1, y + 1), xytext = (0, 0), textcoords = 'offset points')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment