def display_data(word_vectors, words, target_words=None):
target_matrix = word_vectors.copy()
if target_words:
target_words = [line.strip().lower() for line in open(target_words)][:2000]
rows = [words.index(word) for word in target_words if word in words]
target_matrix = target_matrix[rows,:]
else:
rows = np.random.choice(len(word_vectors), size=1000, replace=False)
target_matrix = target_matrix[rows,:]
reduced_matrix = tsne(target_matrix, 2);
Plot.figure(figsize=(200, 200), dpi=100)
max_x = np.amax(reduced_matrix, axis=0)[0]
max_y = np.amax(reduced_matrix, axis=0)[1]
Plot.xlim((-max_x,max_x))
Plot.ylim((-max_y,max_y))
Plot.scatter(reduced_matrix[:, 0], reduced_matrix[:, 1], 20);
for row_id in range(0, len(rows)):
target_word = words[rows[row_id]]
x = reduced_matrix[row_id, 0]
y = reduced_matrix[row_id, 1]
Plot.annotate(target_word, (x,y))
Plot.savefig("word_vectors.png");
评论列表
文章目录