t-sne降维处理

from time import time
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
from sklearn import (manifold, datasets, decomposition, ensemble, lda,random_projection)
import pickle
import os

def plot_embedding_2d(X, title=None):
    #坐标缩放到[0,1]区间
    x_min, x_max = np.min(X,axis=0), np.max(X,axis=0)
    X = (X - x_min) / (x_max - x_min)

    #降维后的坐标为(X[i, 0], X[i, 1]),在该位置画出对应的digits
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    for i in range(X.shape[0]):
        ax.text(X[i, 0], X[i, 1],str(i),
                 color=plt.cm.Set1(i / 10.),
                 fontdict={'weight': 'bold', 'size': 2})

    if title is not None:
        plt.title(title)

data = pickle.load(open(os.path.join('./tsne_1.pkl'), 'rb'))
print("Computing t-SNE embedding")
tsne = manifold.TSNE(n_components=2, init='pca', random_state=0)
t0 = time()
X_tsne = tsne.fit_transform(data)
print(X_tsne.shape)
plot_embedding_2d(X_tsne[:,0:2],"Rendering Attribution Space")
# plot_embedding_3d(X_tsne,"t-SNE 3D (time %.2fs)" %(time() - t0))

plt.show()

你可能感兴趣的:(深度学习通用代码)