def plot_tsne(data, analyse = True, n_components = 2, precomputed = False):
"""Perform t-SNE and plot overview of the results"""
if precomputed:
metric = 'precomputed';
else:
metric = None;
if analyse:
tsne = sl.TSNE(n_components=n_components, init = 'pca', random_state = 0, metric = metric)
Y = tsne.fit_transform(data)
else:
Y = data;
if n_components == 1:
plt.plot(Y);
elif n_components == 2:
plt.scatter(Y[:,0], Y[:,1], c = range(len(Y[:,0])), cmap = plt.cm.Spectral);
else:
ax = plt.gcf().add_subplot(1, 1, 1, projection = '3d');
ax.scatter(Y[:, 0], Y[:, 1], Y[:,2], c = range(len(Y[:,0])), cmap=plt.cm.Spectral)
plt.title("t-SNE")
plt.tight_layout();
评论列表
文章目录