import numpy as np import matplotlib.pyplot as plt from time import time from sklearn.manifold import TSNE def plot_embedding(data, label, title): ids = np.unique(label) label_color = label.copy() for i, label_id in enumerate(ids): label_color[label_color==label_id] = i x_min, x_max = np.min(data, 0), np.max(data, 0) data = (data - x_min) / (x_max - x_min) fig = plt.figure() ax = plt.subplot(111) for i in range(data.shape[0]): plt.text(data[i, 0], data[i, 1], str(label[i]), color=plt.cm.Set1(label_color[i] / 10.), fontdict={'weight': 'bold', 'size': 9}) plt.xticks([]) plt.yticks([]) plt.title(title) plt.show() return fig def sne(data, label): print('Computing t-SNE embedding') tsne = TSNE(n_components=2, init='pca', random_state=0) t0 = time() result = tsne.fit_transform(data) fig = plot_embedding(result, label, 't-SNE embedding of the digits (time %.2fs)' % (time() - t0))