t-SNE(t-distribution Stochastic Neighbor Embedding)是目前最为流行的高维数据的降维算法。

t-SNE 成立的前提基于这样的一个假设:我们现实世界观察到的数据集,都在本质上有一种低维的特性(low intrinsic dimensionality),尽管它们嵌入在高维空间中,甚至可以说,高维数据经过降维后,在低维状态下,更能显现其本质特性,这其实也是流形学习(Manifold Learning)的基本思想。

原始论文请见,论文链接(pdf)

1. sklearn 仿真

  • import 必要的库;

    import numpy as np
    from numpy import linalg
    from numpy.linalg import norm
    from scipy.spatial.distance import squareform, pdist # We import sklearn. import sklearn
    from sklearn.manifold import TSNE
    from sklearn.datasets import load_digits
    from sklearn.preprocessing import scale # We'll hack a bit with the t-SNE code in sklearn 0.15.2. from sklearn.metrics.pairwise import pairwise_distances
    from sklearn.manifold.t_sne import (_joint_probabilities,
    _kl_divergence)
    from sklearn.utils.extmath import _ravel # Random state. RS = 20150101 # We'll use matplotlib for graphics. import matplotlib.pyplot as plt
    import matplotlib.patheffects as PathEffects
    import matplotlib
    %matplotlib inline # We import seaborn to make nice plots. import seaborn as sns
    sns.set_style('darkgrid')
    sns.set_palette('muted')
    sns.set_context("notebook", font_scale=1.5,
    rc={"lines.linewidth": 2.5}) # We'll generate an animation with matplotlib and moviepy. from moviepy.video.io.bindings import mplfig_to_npimage
    import moviepy.editor as mpy
  • 加载数据集

    digits = load_digits()
    # digits.data.shape ⇒ (1797L, 64L)
  • 调用 sklearn 工具箱中的 t-SNE 类

    X = np.vstack([digits.data[digits.target==i]
    for i in range(10)])
    y = np.hstack([digits.target[digits.target==i]
    for i in range(10)])
    digits_proj = TSNE(random_state=RS).fit_transform(X)
    # digits_proj:(1797L, 2L),ndarray 类型
  • 可视化

    def scatter(x, colors):
    # We choose a color palette with seaborn.
    palette = np.array(sns.color_palette("hls", 10)) # We create a scatter plot.
    f = plt.figure(figsize=(8, 8))
    ax = plt.subplot(aspect='equal')
    sc = ax.scatter(x[:,0], x[:,1], lw=0, s=40,
    c=palette[colors.astype(np.int)])
    plt.xlim(-25, 25)
    plt.ylim(-25, 25)
    ax.axis('off')
    ax.axis('tight') # We add the labels for each digit.
    txts = []
    for i in range(10):
    # Position of each label.
    xtext, ytext = np.median(x[colors == i, :], axis=0)
    txt = ax.text(xtext, ytext, str(i), fontsize=24)
    txt.set_path_effects([
    PathEffects.Stroke(linewidth=5, foreground="w"),
    PathEffects.Normal()])
    txts.append(txt) return f, ax, sc, txts
    scatter(digits_proj, y)
    plt.savefig('images/digits_tsne-generated.png', dpi=120)

An illustrated introduction to the t-SNE algorithm

理解 t-SNE (Python)的更多相关文章

  1. python中闭包和装饰器的理解(关于python中闭包和装饰器解释最好的文章)

    转载:http://python.jobbole.com/81683/ 呵呵!作为一名教python的老师,我发现学生们基本上一开始很难搞定python的装饰器,也许因为装饰器确实很难懂.搞定装饰器需 ...

  2. 深入理解并使用python的模块与包

    模块 编写好的一个python文件可以有两种用途:1)脚本,一个文件就是整个程序,用来被执行2)模块,文件中存放着一堆功能,用来被导入使用 模块的分类 1)开发者编写的 .py文件2 ) 由C或C++ ...

  3. python 中 深拷贝和浅拷贝的理解

    在总结 python 对象和引用的时候,想到其实 对于python的深拷贝和浅拷贝也可以很好对其的进行理解. 在python中,对象的赋值的其实就是对象的引用.也就是说,当创建一个对象,然后赋给另外一 ...

  4. 如何理解 Python 的赋值逻辑

    摘要: 如果你学过 C 语言,那么当你初见 Python 时可能会觉得 Python 的赋值方式略有诡异:好像差不多,但又好像哪里有点不太对劲. 本文比较并解释了这种赋值逻辑上的差异.回答了为什么需要 ...

  5. [转] 深刻理解Python中的元类(metaclass)

    非常详细的一篇深入讲解Python中metaclass的文章,感谢伯乐在线-bigship翻译及作者,转载收藏. 本文由 伯乐在线 - bigship 翻译.未经许可,禁止转载!英文出处:stacko ...

  6. 非常易于理解‘类'与'对象’ 间 属性 引用关系,暨《Python 中的引用和类属性的初步理解》读后感

    关键字:名称,名称空间,引用,指针,指针类型的指针(即指向指针的指针) 我读完后的理解总结: 1. 我们知道,python中的变量的赋值操作,变量其实就是一个名称name,赋值就是将name引用到一个 ...

  7. 深刻理解Python中的元类(metaclass)【转】

    译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得 ...

  8. 当我学完Python时我学了些什么

    本文是本人学完Python后的一遍回顾,加深理解而已,Python大神请过~ 学习Python的这几天来,觉得Python还是比较简单,容易上手的,就基本语法而言,但是有些高级特性掌握起来还是有些难度 ...

  9. Python学习笔记(三)——类型与变量

    一.输入与输出 print("string"); print("string1","string2","string3" ...

随机推荐

  1. 关于IO重定向

    首先,Unix进程使用文件描述符0,1,2作为标准输入.输出和错误的通道. 其次,当进程请求一个新的文件描述符的时候,系统内核将最低可用的文件描述符赋给它. 第三,文件描述符集合通过exec调用传递, ...

  2. MapReduce 图解流程

    Anatomy of a MapReduce Job In MapReduce, a YARN application is called a Job. The implementation of t ...

  3. Gora官方文档之二:Gora对Map-Reduce的支持 分类: C_OHTERS 2015-01-31 11:27 232人阅读 评论(0) 收藏

    参考官方文档:http://gora.apache.org/current/tutorial.html 项目代码见:https://code.csdn.net/jediael_lu/mygoradem ...

  4. ARM 授权费用太贵 科技巨头欲转向开源架构 RISC-V

    不久前,特斯拉加入 RISC-V 基金会,并考虑在新款芯片中使用免费的 RISC-V 设计.至此,已有 IBM.NXP.西部数据.英伟达.高通.三星.谷歌.华为等 100 多家科技公司加入 RISC- ...

  5. nginx简介(轻量级开源高并发web服务器:大陆使用者百度、京东、新浪、网易、腾讯、淘宝等)(并发量5w)(一般网站apache够用了,而且稳定)

    nginx简介(轻量级开源高并发web服务器:大陆使用者百度.京东.新浪.网易.腾讯.淘宝等)(并发量5w)(一般网站apache够用了,而且稳定) 一.总结 1.在连接高并发的情况下,Nginx是A ...

  6. P2P网络借贷系统简要解读

    P2P网络借贷系统的核心功能其实很简单,就是若干投资者(借款人)集体把钱贷给贷款人,到期后,贷款人把利息和本金按时还给借款人.平台的主要作用是"撮合",收取一定的服务费和管理费. ...

  7. Facebook开源软件列表

    从 Facebook 的 GitHub 账户中可以看到,Facebook 已经开源的开源项目有近 300 个,领域涉及移动.前端.Web.后端.大数据.数据库.工具和硬件等.Facebook 开源项目 ...

  8. 《编程导论(Java)·3.2.4 循环语句》

    本文全然复制<编程导论(Java)·3.2.4 循环语句>的内容.除[]中的说明文字.请阅读和比較其它编程教材. 我知道.假设我是一个刚開始学习的人,<编程导论(Java)>非 ...

  9. Android 输入框弹出样式

    在androidMainfest.xml文件里 在Activity中设置 [A]stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置 [B]stateU ...

  10. 从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(1)

    这一章节我们来具体讨论一下配合@autowired一起使用的限定器@Qualifier. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_n ...