如下所示:

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. 使用fork并发处理多个client的请求和对等通信p2p

    一.在前面讲过的回射客户/服务器程序中,服务器只能处理一个客户端的请求,如何同时服务多个客户端呢?在未讲到select/poll/epoll等高级IO之前,比较老土的办法是使用fork来实现.网络服务 ...

  2. Linux内核scatterlist API介绍

    1. 前言 我们在那些需要和用户空间交互大量数据的子系统(例如MMC[1].Video.Audio等)中,经常看到scatterlist的影子.对我们这些“非英语母语”的人来说,初见这个词汇,脑袋瞬间 ...

  3. Linux内核(8) - 设备模型(下)

    设备模型拍得再玄幻,它也只是个模型,必须得落实在具体的子系统,否则就只能抱着个最佳技术奖空遗恨.既然前面已经以USB子系统的实现分析示例了分析内核源码应该如何入手,那么这里就仍然以USB子系统为例,看 ...

  4. ActiveReports 报表控件官方中文新手教程 (1)-安装、激活以及产品资源

     本系列文章主要是面向初次接触 ActiveReports 产品的用户,能够帮助您在三天之内轻松的掌握ActiveReports控件的基本用法,包含安装.激活.创建报表.绑定数据源以及公布等内容. ...

  5. poj1564 Sum it up

    题目链接: http://poj.org/problem?id=1564 题目: Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  6. 腾讯 OCR 情况

    OCR技术之检测篇 https://cloud.tencent.com/developer/article/1101342 OCR技术之数据篇 https://cloud.tencent.com/de ...

  7. introduction to python for statistics,analysis笔记3

    一.产生数组和矩阵 1.linspace(start,end,number),产生在start和end数之间number个数 >>> x = linspace(, , ) >& ...

  8. Ruby gem 更换国内源

    gem sources --add http://gems.ruby-china.org/ --remove https://rubygems.org/

  9. MongoDB创建索引(不锁库方法)

    db.collection.createIndex( { a: 1 }, { background: true } )https://docs.mongodb.org/manual/tutorial/ ...

  10. 如何学习Linux

    为啥我们要学习Linux 技术的价值不在于这个技术有多么高超,而在于技术本身给我们带来什么价值,所以很多时候我们学习一个技术,不能盲目学,是为了使用这个技术,知道这个技术的使用场景,知道这个技术带来的 ...