t-SNE

文章摘自故障诊断与python学习 的 tSNE-python代码实现及使用详解

image

代码实现

from time import time
import numpy as np
import maplotlib.pyplot as plt

from sklearn import datasets
from sklearn.manifold import TSNE

def get_data():
	digits = datasets.load_digits(n_class=6)
	data = digits.data
	label = digits.target
	n_samples, n_features = data.shape
	return data, label, n_samples, n_features
	

def plot_embedding(data, label, title):
	x_min, x_max = np.min(data, 0), np.max(data)
	data = (data - x_min) / (x_max - x_min)
	
	fig = plt.figure()
	ax = plt.subplot(111)
	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.xtricks([])
		plt.ytricks([])
		plt.title(title)
		return fig
		
		
def main():
	data, label, n_samples, n_features = get_data()
	tsne = TSNE(n_compoents=2, init=pca, random_state=0)
	t0 = time()
	result = tsne.fit_transform(data)
	fig = plot_embedding(result, label, 
	't-SNE embedding of the digits(time %.2fs)' % (time() - t0))
	plt.show()
posted @ 2023-05-26 00:09  woxin_lab  阅读(59)  评论(0编辑  收藏  举报