如下所示:

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下多层函数调用堆栈运行状态

    我们用下面的C代码来研究函数调用的过程.  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16   int bar(int c, int d) {     ...

  2. Linux Shell之表达式

    严格来说,shell中没有表达式的概念.Shell本身事实上仅仅是一堆命令的集合.当然也不是胡乱的堆在一起.而是有一定的组织.仅仅是这个组织不那么严谨.所以本文不是要真的总结所谓的表达式,而是把she ...

  3. OpenGl学习 SelectObject函数

    SelectObject 函数功能:该函数选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象.   函数原型:HGDIOBJ SelectObject(HDC hdc, HGDIOB ...

  4. UVa 12715 Watching the Kangaroo(二分)

    题意:n条线段(n <= 100000) (L<=R <= 1e9) ,m组询问(m <= 100000) 每次询问一个点的覆盖范围的最大值.一个点x对于一条包括其的线段,覆盖 ...

  5. angular学习笔记(八)-控制视图显示隐藏

    本篇介绍angular控制视图的显示和隐藏: 通过给元素添加ng-show属性或者ng-hide属性来控制视图的显示或隐藏: ng-show: 绑定的数据值为true时,显示元素,值为false时,隐 ...

  6. less基本知识总结

    > 一款比较流行的预处理CSS,支持变量.混合.函数.嵌套.循环等特点> [官网](http://lesscss.org/)> [中文网](http://lesscss.cn/)&g ...

  7. 注解Annotation的IoC:从@Autowired到@Component

    注解Annotation的IoC:从@Autowired到@Component 2017-01-23 目录 1 什么是注解2 不使用注解示例  2.1 com.springioc.animal.Mon ...

  8. LeetCode: Roman to Integer 解题报告

    Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...

  9. Windows下 Pycharm连接Github 教程

    Pycharm连接Github 绑定账号 1.File->Settings->Version Control->Github Settings.png 会出现github,然后在旁边 ...

  10. iOS 为什么使用xcode8上传app包到appStore无法构建版本

    使用xcode8或者application loader上传代码包到AppStore提示上传成功,但是我们在iTunes Connect中的构建版本或者活动中看不到已上传的代码包.这个问题原因是:ap ...