sklearn画t-SNE图

import numpy as np
import matplotlib.pyplot as plt
import json
from sklearn import manifold

def draw_tsne(emb_filename):
    model = json.load(open(emb_filename,'r')) 
    X = np.array(model['data'])
    y = np.array(model['label'])



    '''t-SNE'''
    tsne = manifold.TSNE(n_components=2,method='exact')  #降成2维度
    print(X.shape)
    X = X[:1750,:,:]##读取其中的前1750个向量
    y = y[:1750]##读取其中的前1750个标签
    X = X.reshape(X.shape[0],X.shape[2])
    print(X.shape)
    X_tsne = tsne.fit_transform(X)
    print(X.shape)
    print(X_tsne.shape)
    print(y.shape)
    print("Org data dimension is {}.Embedded data dimension is {}".format(X.shape[-1], X_tsne.shape[-1]))

    '''嵌入空间可视化'''
    x_min, x_max = X_tsne.min(0), X_tsne.max(0)
    X_norm = (X_tsne - x_min) / (x_max - x_min)  # 归一化
    plt.figure(figsize=(8, 8))
    for i in range(X_norm.shape[0]):
        plt.text(X_norm[i, 0], X_norm[i, 1], '*', color=plt.cm.Set1(y[i]), 
                 fontdict={'weight': 'bold', 'size': 18})
    plt.xticks([])
    plt.yticks([])
    plt.show()
    plt.savefig('1.png')
emb_filename = ("/data/test_data.json")#读取json文件
draw_tsne(emb_filename)

你可能感兴趣的:(语音,sklearn,python)