TSNE 降维 可视化脚本

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


def plot_embedding(data, label):
    _min, _max = np.min(data, 0), np.max(data, 0)
    data = (data - _min) / (_max - _min)

    for i in range(data.shape[0]):
        plt.text(data[i, 0], data[i, 1], str(label[i]),
                 color=plt.cm.tab10(label[i]),
                 fontdict={'weight': 'bold', 'size': 9})
    plt.show()


feature = "xxx"  # numpy.ndarray, 如(1000,256)
y_test = ""  # numpy.ndarray, 如(1000,)

tsne = TSNE(n_components=2, init='pca', random_state=0)
tsne_feature = tsne.fit_transform(feature)

plot_embedding(tsne_feature, y_test)

你可能感兴趣的:(python,常用py脚本,python,机器学习)