如下所示:

import pickle as pkl
import numpy as np
from matplotlib import pyplot as plt
from tsne import bh_sne
import sys with open("data", 'rb') as f:
if sys.version_info > (3, 0):
data = pkl.load(f, encoding='latin1')
else:
data = pkl.load(f) data =data.astype('float64') with open("label", 'rb') as f:
if sys.version_info > (3, 0):
y_data = pkl.load(f, encoding='latin1')
else:
y_data = pkl.load(f)
classNum = 6
y_data = np.where(y_data==1)[1]*(9.0/classNum) vis_data = bh_sne(data) # plot the result
vis_x = vis_data[:, 0]
vis_y = vis_data[:, 1] fig = plt.figure()
plt.scatter(vis_x, vis_y, c=y_data, s=1, cmap=plt.cm.get_cmap("jet", 10))
plt.colorbar(ticks=range(10))
plt.clim(-0.5, 9.5)
plt.show()
fig.savefig('test.png')

结果:

以MNIST为例,先做PCA降到50维,再做t-sne:

from time import time
from tsne import bh_sne
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
from matplotlib import offsetbox
from sklearn import (manifold, datasets, decomposition, ensemble,
discriminant_analysis, random_projection)
from sklearn import decomposition mnist = input_data.read_data_sets('./input_data', one_hot=False)
sub_sample = 5000
y = mnist.train.labels[0:sub_sample]
X = mnist.train.images[0:sub_sample] n_samples, n_features = X.shape
n_neighbors = 30 #----------------------------------------------------------------------
# Scale and visualize the embedding vectors
def plot_embedding(X_emb, title=None):
x_min, x_max = np.min(X_emb, 0), np.max(X_emb, 0)
X_emb = (X_emb - x_min) / (x_max - x_min) plt.figure()
ax = plt.subplot(111)
for i in range(X_emb.shape[0]):
plt.text(X_emb[i, 0], X_emb[i, 1], str(y[i]),
color=plt.cm.Set1(y[i] / 10.),
fontdict={'weight': 'bold', 'size': 9}) if hasattr(offsetbox, 'AnnotationBbox'):
# only print thumbnails with matplotlib > 1.0
shown_images = np.array([[1., 1.]]) # just something big
for i in range(sub_sample):
dist = np.sum((X_emb[i] - shown_images) ** 2, 1)
if np.min(dist) < 8e-3:
# don't show points that are too close
continue
shown_images = np.r_[shown_images, [X_emb[i]]]
imagebox = offsetbox.AnnotationBbox(
offsetbox.OffsetImage(X[i].reshape(28,28)[::2,::2], cmap=plt.cm.gray_r),
X_emb[i])
ax.add_artist(imagebox)
plt.xticks([]), plt.yticks([])
if title is not None:
plt.title(title) #----------------------------------------------------------------------
# Plot images of the digits
n_img_per_row = 20
img = np.zeros((30 * n_img_per_row, 30 * n_img_per_row))
for i in range(n_img_per_row):
ix = 30 * i + 1
for j in range(n_img_per_row):
iy = 30 * j + 1
img[ix:ix + 28, iy:iy + 28] = X[i * n_img_per_row + j].reshape((28, 28)) plt.imshow(img, cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
plt.title('A selection from the 64-dimensional digits dataset') # t-SNE embedding of the digits dataset
print("Computing t-SNE embedding")
t0 = time()
X_pca = decomposition.TruncatedSVD(n_components=50).fit_transform(X)
# data =X.astype('float64')
X_tsne = bh_sne(X_pca) plot_embedding(X_tsne,
"t-SNE embedding of the digits (time %.2fs)" %
(time() - t0)) plt.show()

结果如下:

更多降维的可视化参考:http://scikit-learn.org/stable/auto_examples/manifold/plot_lle_digits.html#sphx-glr-auto-examples-manifold-plot-lle-digits-py

t-SNE可视化(MNIST例子)的更多相关文章

  1. 一个简单的TensorFlow可视化MNIST数据集识别程序

    下面是TensorFlow可视化MNIST数据集识别程序,可视化内容是,TensorFlow计算图,表(loss, 直方图, 标准差(stddev)) # -*- coding: utf-8 -*- ...

  2. Tensorflow可视化MNIST手写数字训练

    简述] 我们在学习编程语言时,往往第一个程序就是打印“Hello World”,那么对于人工智能学习系统平台来说,他的“Hello World”小程序就是MNIST手写数字训练了.MNIST是一个手写 ...

  3. 可视化MNIST之降维探索Visualizing MNIST: An Exploration of Dimensionality Reduction

    At some fundamental level, no one understands machine learning. It isn’t a matter of things being to ...

  4. 莫烦TensorFlow_09 MNIST例子

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...

  5. (4运行例子)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署

    ​1.联通ColaB 2.运行最基础mnist例子,并且打印图表结果  # https://pypi.python.org/pypi/pydot#!apt-get -qq install -y gra ...

  6. 使用t-SNE做降维可视化

    最近在做一个深度学习分类项目,想看看训练集数据的分布情况,但由于数据本身维度接近100,不能直观的可视化展示,所以就对降维可视化做了一些粗略的了解以便能在低维空间中近似展示高维数据的分布情况,以下内容 ...

  7. 用vs2013(cpu-only)调试caffe的mnist

    在调试Mnist例子之前,首先需要用vs2013编译好caffe.详情请参见: [caffe-Windows]caffe+VS2013+Windows无GPU快速配置教程 按照上述教程编译好caffe ...

  8. [转] kaldi中FST的可视化-以yesno为例

    http://blog.csdn.net/u013677156/article/details/77893661 1.kaldi解码过程 kaldi识别解码一段语音的过程是:首先提取特征,然后过声学模 ...

  9. Caffe 使用记录(一)mnist手写数字识别

    1. 运行它 1. 安装caffe请参考 http://www.cnblogs.com/xuanyuyt/p/5726926.html  此例子在官网 http://caffe.berkeleyvis ...

随机推荐

  1. Linux中断 - 驱动申请中断API

    一.前言 本文主要的议题是作为一个普通的驱动工程师,在撰写自己负责的驱动的时候,如何向Linux Kernel中的中断子系统注册中断处理函数?为了理解注册中断的接口,必须了解一些中断线程化(threa ...

  2. mpu6050 DMP库的移植

    https://www.amobbs.com/thread-5528472-1-1.html 官方的运动库,必须通过这个才能启用MPU6050的DMP引擎(数据手册里完全不提这个东西,必须在官网注册登 ...

  3. spring mvc get方式乱码

    使用spring get方式,向后台传递参数的时候,出现乱码,网上搜索了好久,最后发现原因是server.xml中默认的配置有问题,修改如下就正确了,图片: 文本信息:     <Connect ...

  4. cv1.0入门

    作者 群号 C语言交流中心 环境 VS2010UltimTrial1.iso                    http://pan.baidu.com/s/1dEL85kl VS2010Ulti ...

  5. jQuery 隐藏与显示 input 默认值

    分享下jQuery如何隐藏和显示 input 默认值的例子. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

  6. linux 调试利器gdb, strace, pstack, pstree, lsof

    1) 如何使用strace+pstack利器分析程序性能? http://www.cnblogs.com/bangerlee/archive/2012/04/30/2476190.html 此文有详细 ...

  7. Oracle 12C 在 Oracle Linux 6.5 64Bit 安装手冊

    Oracle 12C  在 Oracle Linux 6.5 64Bit 安装手冊.step by step 下载地址: http://download.csdn.net/detail/rlhua/7 ...

  8. http请求头中的Content-Type属性在angular 和 node中的用法

    post请求的请求体有以下两种格式: 1. 字符串: 'name=code_bunny&age=12' 这种格式的请求体,需要配置请求头 'Content-Type':'application ...

  9. js 时间格式与时间戳的相互转换和计算几天后的日期是哪一天

    //把日期转换成时间戳 function get_unix_time(time1){    var newstr = time1.replace(/-/g,'/');     var date =  ...

  10. 成果展示-RSA算法工具