天天看點

t-SNE(降維)可視化 (sklearn digits手寫資料集)

      t-SNE是一種降維方法,是最好的降維方法之一

      t-SNE是一種集降維與可視化于一體的技術,它是基于SNE可視化的改進,解決了SNE在可視化後樣本分布擁擠、邊界不明顯的特點,是目前最好的降維可視化手段。 

from time import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import offsetbox
from sklearn import manifold, datasets
# ----------------------------------------------------------------------
# Scale and visualize the embedding vectors
def plot_embedding(X, title=None):
    x_min, x_max = np.min(X, 0), np.max(X, 0)
    X = (X - x_min) / (x_max - x_min)
 
    plt.figure()
    ax = plt.subplot(111)
    for i in range(X.shape[0]):  #周遊所有1083個圖
        plt.text(X[i, 0], X[i, 1], str(y[i]),
                 color=plt.cm.Set1(y[i] / 10.),  #cm代表color map,即顔色映射地圖,Set1, Set2, Set3是它的三個顔色集合,可傳回顔色
                 fontdict={'weight': 'bold', 'size': 9})

    plt.xticks([]), plt.yticks([])
    if title is not None:
        plt.title(title)

 
# ----------------------------------------------------------------------
digits = datasets.load_digits(n_class=6)
X = digits.data  #X是(1083,64)

y = digits.target #y是 (1083)
#即共1083張圖, X的每張圖用一個64維的矩陣表示
# t-SNE embedding of the digits dataset
print("Computing t-SNE embedding")
tsne = manifold.TSNE(n_components=2, init='pca', random_state=0)
#把64維降到2維

X_tsne = tsne.fit_transform(X)
#X_tsne是(1083,2)
 
plot_embedding(X_tsne,
               "t-SNE embedding"
               )
plt.show()
           
from time import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import offsetbox
from sklearn import manifold, datasets

digits = datasets.load_digits(n_class=6)
X = digits.data  #X是(1083,64)
y = digits.target #y是 (1083)
#即共1083張圖, X的每張圖用一個64維的矩陣表示
n_samples, n_features = X.shape
n_neighbors = 30


# ----------------------------------------------------------------------
# Scale and visualize the embedding vectors
def plot_embedding(X, title=None):
    x_min, x_max = np.min(X, 0), np.max(X, 0)
    X = (X - x_min) / (x_max - x_min)

    plt.figure()
    ax = plt.subplot(111)
    for i in range(X.shape[0]):  #周遊所有1083個圖
        plt.text(X[i, 0], X[i, 1], str(y[i]),
                 color=plt.cm.Set1(y[i] / 10.),  #cm代表color map,即顔色映射地圖,Set1, Set2, Set3是它的三個顔色集合,可傳回顔色
                 fontdict={'weight': 'bold', 'size': 9})

    if hasattr(offsetbox, 'AnnotationBbox'):
        # only print thumbnails with matplotlib > 1.0
        shown_images = np.array([[1., 1.]])  # just something big
        for i in range(X.shape[0]):
            dist = np.sum((X[i] - shown_images) ** 2, 1)
            if np.min(dist) < 4e-3:
                # don't show points that are too close
                continue
            shown_images = np.r_[shown_images, [X[i]]]
            imagebox = offsetbox.AnnotationBbox(
                offsetbox.OffsetImage(digits.images[i], cmap=plt.cm.gray_r),
                X[i]
            )
            ax.add_artist(imagebox)

    plt.xticks([]), plt.yticks([])
    if title is not None:
        plt.title(title)


# ----------------------------------------------------------------------
# Plot images of the digits
#隻取了前20*20=400個
n_img_per_row = 20
img = np.zeros((10 * n_img_per_row, 10 * n_img_per_row))
for i in range(n_img_per_row):
    ix = 10 * i + 1
    for j in range(n_img_per_row):
        iy = 10 * j + 1
        img[ix:ix + 8, iy:iy + 8] = X[i * n_img_per_row + j].reshape((8, 8))

plt.imshow(img, cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
plt.title('A selection from the 64-dimensional digits dataset')
plt.show()

# ----------------------------------------------------------------------
# t-SNE embedding of the digits dataset
print("Computing t-SNE embedding")

tsne = manifold.TSNE(n_components=2, init='pca', random_state=0)
#把64維降到2維

t0 = time()
X_tsne = tsne.fit_transform(X)
#X_tsne是(1083,2)

plot_embedding(X_tsne,
               "t-SNE embedding of the digits (time %.2fs)" %(time() - t0)
               )

plt.show()
           
t-SNE(降維)可視化 (sklearn digits手寫資料集)
t-SNE(降維)可視化 (sklearn digits手寫資料集)
如果沒有hasattr(offsetbox, 'AnnotationBbox') 這部分那麼結果會是這樣
t-SNE(降維)可視化 (sklearn digits手寫資料集)

繼續閱讀