t-sne:不同perplexity值對形狀的影響?

兩個同心圓和S曲線數據集對不同perplexity值的t-SNE的說明。

我們觀察到,隨著perplexity值的增加,形狀越來越清晰。

聚類的大小、距離和形狀可能因初始化、perplexity值而異,并不總是能夠傳達意義。

如下所示,對于較高的perplexity,t-SNE發現了兩個同心圓的有意義的拓撲結構,但圓圈的大小和距離與原來的略有不同。與這兩個圓數據集相反,即使在較大的perplexity值下,形狀在視覺上也與S曲線數據集上的S曲線拓撲不同。

更多細節, “如何有效地使用t-SNE” https://distill.pub/2016/misread-tsne/提供了一個很好的關于各種參數的影響的討論,以及互動的情節。

circles, perplexity=5 in 1.6 sec
circles, perplexity=30 in 2.2 sec
circles, perplexity=50 in 2.3 sec
circles, perplexity=100 in 2.5 sec
S-curve, perplexity=5 in 1.7 sec
S-curve, perplexity=30 in 2.3 sec
S-curve, perplexity=50 in 1.9 sec
S-curve, perplexity=100 in 2.7 sec
uniform grid, perplexity=5 in 1.5 sec
uniform grid, perplexity=30 in 2.1 sec
uniform grid, perplexity=50 in 1.9 sec
uniform grid, perplexity=100 in 2.7 sec
# Author: Narine Kokhlikyan <narine@slice.com>
# License: BSD

print(__doc__)

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.ticker import NullFormatter
from sklearn import manifold, datasets
from time import time

n_samples = 300
n_components = 2
(fig, subplots) = plt.subplots(35, figsize=(158))
perplexities = [53050100]

X, y = datasets.make_circles(n_samples=n_samples, factor=.5, noise=.05)

red = y == 0
green = y == 1

ax = subplots[0][0]
ax.scatter(X[red, 0], X[red, 1], c="r")
ax.scatter(X[green, 0], X[green, 1], c="g")
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
plt.axis('tight')

for i, perplexity in enumerate(perplexities):
    ax = subplots[0][i + 1]

    t0 = time()
    tsne = manifold.TSNE(n_components=n_components, init='random',
                         random_state=0, perplexity=perplexity)
    Y = tsne.fit_transform(X)
    t1 = time()
    print("circles, perplexity=%d in %.2g sec" % (perplexity, t1 - t0))
    ax.set_title("Perplexity=%d" % perplexity)
    ax.scatter(Y[red, 0], Y[red, 1], c="r")
    ax.scatter(Y[green, 0], Y[green, 1], c="g")
    ax.xaxis.set_major_formatter(NullFormatter())
    ax.yaxis.set_major_formatter(NullFormatter())
    ax.axis('tight')

# Another example using s-curve
X, color = datasets.make_s_curve(n_samples, random_state=0)

ax = subplots[1][0]
ax.scatter(X[:, 0], X[:, 2], c=color)
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())

for i, perplexity in enumerate(perplexities):
    ax = subplots[1][i + 1]

    t0 = time()
    tsne = manifold.TSNE(n_components=n_components, init='random',
                         random_state=0, perplexity=perplexity)
    Y = tsne.fit_transform(X)
    t1 = time()
    print("S-curve, perplexity=%d in %.2g sec" % (perplexity, t1 - t0))

    ax.set_title("Perplexity=%d" % perplexity)
    ax.scatter(Y[:, 0], Y[:, 1], c=color)
    ax.xaxis.set_major_formatter(NullFormatter())
    ax.yaxis.set_major_formatter(NullFormatter())
    ax.axis('tight')


# Another example using a 2D uniform grid
x = np.linspace(01, int(np.sqrt(n_samples)))
xx, yy = np.meshgrid(x, x)
X = np.hstack([
    xx.ravel().reshape(-11),
    yy.ravel().reshape(-11),
])
color = xx.ravel()
ax = subplots[2][0]
ax.scatter(X[:, 0], X[:, 1], c=color)
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())

for i, perplexity in enumerate(perplexities):
    ax = subplots[2][i + 1]

    t0 = time()
    tsne = manifold.TSNE(n_components=n_components, init='random',
                         random_state=0, perplexity=perplexity)
    Y = tsne.fit_transform(X)
    t1 = time()
    print("uniform grid, perplexity=%d in %.2g sec" % (perplexity, t1 - t0))

    ax.set_title("Perplexity=%d" % perplexity)
    ax.scatter(Y[:, 0], Y[:, 1], c=color)
    ax.xaxis.set_major_formatter(NullFormatter())
    ax.yaxis.set_major_formatter(NullFormatter())
    ax.axis('tight')


plt.show()

腳本的總運行時間:(0分26.425秒)

Download Python source code: plot_t_sne_perplexity.py

Download Jupyter notebook: plot_t_sne_perplexity.ipynb