t-SNE对手写数字降维可视化

# t-SNE对手写数字的降维可视化
from time import time
import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets
from sklearn.manifold import TSNE

digits = datasets.load_digits(n_class=6)
# print(digits)
data = digits.data
label = digits.target
# print(data)
# print(data.shape)
n_samples,n_features = data.shape

tsne = TSNE(n_components=2,init='pca',random_state=0)
t0 = time()
result = tsne.fit_transform(data)
result

tsne = TSNE(n_components=2,init='pca',random_state=0)
t0 = time()
result = tsne.fit_transform(data)
result

x_min,x_max = np.min(result,0),np.max(result,0)
data = (result-x_min)/(x_max-x_min)

fig = plt.figure()
# 表示画布整个输出,不分割成小块区域,图形直接输出在整块画布上
ax = plt.subplot(111)
# print(data.shape[0])
for i in range(data.shape[0]):
    plt.text(data[i,0],data[i,1],str(label[i]),color=plt.cm.Set1(label[i]/10.),fontdict={'weight':'bold','size':9})
plt.xticks([])
plt.yticks([])
plt.title('t-SNE embedding of the digits')
plt.show()

这里写图片描述

猜你喜欢

转载自blog.csdn.net/alicelmx/article/details/81364826