def draw2D(self): model = TSNE(n_components=2) result = model.fit_transform(self.__wordVector) model = KMeans(5) lable = model.fit_predict(result) cm_subsection = linspace(0, 1, 5) colors = [cm.rainbow(x) for x in cm_subsection] random.shuffle(colors) fig = plt.figure(figsize=(20, 12)) for i, word in enumerate(self.__showWords): plt.scatter(result[i, 0], result[i, 1], color=colors[lable[i]]) plt.annotate(word, xy=(result[i, 0], result[i, 1])) fig.show() self.__saveImag(plt, '2D')
import numpy as np from sklearn.manifold import TSNE data = np.load('tsne_data.npy') print("Dimensions of the data: ", data.shape) kmeans_1 = TSNE(n_clusters=2, perplexity=30) transformed_data = kmeans_1.fit_predict(data)