点击关注,桓峰基因

桓峰基因公众号推出机器学习应用于临床预测的方法,跟着教程轻松学习,每个文本教程配有视频教程大家都可以自由免费学习,目前已有的机器学习教程整理出来如下:

MachineLearning 1. 主成分分析(PCA)

MachineLearning 2. 因子分析(Factor Analysis)

MachineLearning 3. 聚类分析(Cluster Analysis)

MachineLearning 4. 癌症诊断方法之 K-邻近算法(KNN)

MachineLearning 5. 癌症诊断和分子分型方法之支持向量机(SVM)

MachineLearning 6. 癌症诊断机器学习之分类树(Classification Trees)

MachineLearning 7. 癌症诊断机器学习之回归树(Regression Trees)

MachineLearning 8. 癌症诊断机器学习之随机森林(Random Forest)

MachineLearning 9. 癌症诊断机器学习之梯度提升算法(Gradient Boosting)

MachineLearning 10. 癌症诊断_机器学习_之神经网络(Neural network)

MachineLearning 11. 机器学习之随机森林生存分析(randomForestSRC)

MachineLearning 12. 机器学习之降维方法t-SNE及可视化(Rtsne)

这期介绍一下NB的最佳降维方法之一 t-SNE,并实现在多个数据集上的应用,尤其是单细胞测序数据。


前言

SNE(t-distributed stochastic neighbor embedding)是用于降维的一种机器学习算法,是由 Laurens van der Maaten 和 Geoffrey Hinton在08年提出来。此外,t-SNE 是一种非线性降维算法,非常适用于高维数据降维到2维或者3维,进行可视化。t-SNE是由SNE(Stochastic Neighbor Embedding, SNE; Hinton and Roweis, 2002)发展而来。t-SNE本质是一种嵌入模型,能够将高维空间中的数据映射到低维空间中,并保留数据集的局部特性。t-SNE降维方法,降维的效果相对于PCA更好,还可以进行可视化分析等,是目前很好用的降维算法之一。

软件安装

设置清华镜像,加快包的安装如下:

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
if(!require(Rtsne))install.packages("Rtsne")

数据读取

这里我们准备了三个数据集,一个是例子中给出来的,一个是乳腺癌患者活检数据集,另一个是单细胞转录组的数据集。

1. 埃德加·安德森的虹膜数据

Description This famous (Fisher’s or Anderson’s) iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica. Usage iris Format iris is a data frame with 150 cases (rows) and 5 variables (columns) named Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, and Species.

library(Rtsne)
library(ggplot2)
data("iris")
iris_unique <- unique(iris)  # Remove duplicates
iris_matrix <- as.matrix(iris_unique[, 1:4])
head(iris_matrix)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1          5.1         3.5          1.4         0.2
## 2          4.9         3.0          1.4         0.2
## 3          4.7         3.2          1.3         0.2
## 4          4.6         3.1          1.5         0.2
## 5          5.0         3.6          1.4         0.2
## 6          5.4         3.9          1.7         0.4

2. 乳腺癌患者活检数据

我们已经多次使用过这个数据集了,class是二分类的结果变量恶性还是良性的。

Description This breast cancer database was obtained from the University of Wisconsin Hospitals, Madison from Dr. William H. Wolberg. He assessed biopsies of breast tumours for 699 patients up to 15 July 1992; each of nine attributes has been scored on a scale of 1 to 10, and the outcome is also known. There are 699 rows and 11 columns. Usage biopsy Format This data frame contains the following columns: ID sample code number (not unique). V1 clump thickness. V2 uniformity of cell size. V3 uniformity of cell shape. V4 marginal adhesion. V5 single epithelial cell size. V6 bare nuclei (16 values are missing). V7 bland chromatin. V8 normal nucleoli. V9 mitoses. class “benign” or “malignant”.

library(MASS)
data("biopsy")
head(biopsy)
##        ID V1 V2 V3 V4 V5 V6 V7 V8 V9     class
## 1 1000025  5  1  1  1  2  1  3  1  1    benign
## 2 1002945  5  4  4  5  7 10  3  2  1    benign
## 3 1015425  3  1  1  1  2  2  3  1  1    benign
## 4 1016277  6  8  8  1  3  4  3  7  1    benign
## 5 1017023  4  1  1  3  2  1  3  1  1    benign
## 6 1017122  8 10 10  8  7 10  9  7  1 malignant
data <- unique(na.omit(biopsy[, -1]))
head(data)
##   V1 V2 V3 V4 V5 V6 V7 V8 V9     class
## 1  5  1  1  1  2  1  3  1  1    benign
## 2  5  4  4  5  7 10  3  2  1    benign
## 3  3  1  1  1  2  2  3  1  1    benign
## 4  6  8  8  1  3  4  3  7  1    benign
## 5  4  1  1  3  2  1  3  1  1    benign
## 6  8 10 10  8  7 10  9  7  1 malignant

3. 单细胞转录组数据集

单细胞数据我们可以从scatter这个软件包获取,但是目前失效了,我到github上下载了一下,同样可以使用:

library(scater)
library(scRNAseq)
load("sc_example_counts.RData")
load("sc_example_cell_info.RData")
head(sc_example_cell_info)
##              Cell Mutation_Status Cell_Cycle Treatment
## Cell_001 Cell_001        positive          S    treat1
## Cell_002 Cell_002        positive         G0    treat1
## Cell_003 Cell_003        negative         G1    treat1
## Cell_004 Cell_004        negative          S    treat1
## Cell_005 Cell_005        negative         G1    treat2
## Cell_006 Cell_006        negative         G0    treat1
sc_example_counts = unique(sc_example_counts)
sc_example_counts[1:5, 1:5]
##           Cell_001 Cell_002 Cell_003 Cell_004 Cell_005
## Gene_0001        0      123        2        0        0
## Gene_0002      575       65        3     1561     2311
## Gene_0003        0        0        0        0     1213
## Gene_0004        0        1        0        0        0
## Gene_0005        0        0       11        0        0

参数说明

Barnes-Hut implementation of t-Distributed Stochastic Neighbor Embedding Description Wrapper for the C++ implementation of Barnes-Hut t-Distributed Stochastic Neighbor Embedding. t-SNE is a method for constructing a low dimensional embedding of high-dimensional data, distances or similarities. Exact t-SNE can be computed by setting theta=0.0. Usage Rtsne(X, …)

  1. Rtsne输入对象必须为矩阵;

  2. dims参数设置降维后的维度,默认为2;

  3. theta参数取值越大,结果的准确率越低,默认为0.5,theta此处设置为0.0 ,可以计算出t-SNE的准确值,但是运行时间会比较长;

  4. pca表示是否对输入的数据进行pca分析,此处设置TRUE。

例子实操

1. iris数据集的降维

读入矩阵,构造模型,给出结果:

# Set a seed if you want reproducible results
set.seed(123)
tsne_out <- Rtsne(iris_matrix, pca = FALSE, perplexity = 30, theta = 0)  # Run TSNE
res <- as.data.frame(tsne_out$Y)
res$Class = iris_unique$Species
length(unique(res$Class))
## [1] 3
# 绘制降维后图形
ggplot(res, aes(x = V1, y = V2, color = Class)) + geom_point(size = 1.25) + labs(title = "t-SNE",x = "TSNE1", y = "TSNE2") + theme(plot.title = element_text(hjust = 0.5)) + theme_bw()

2. biopsy数据集的降维

读入矩阵,构造模型,给出结果:

set.seed(123)
tsne <- Rtsne(as.matrix(data[, 1:9]), pca = TRUE, perplexity = 10, theta = 0)  # Run TSNE
res <- as.data.frame(tsne$Y)
res$Class = data$class
length(unique(res$Class))
## [1] 2
head(res)
##           V1        V2     Class
## 1  -7.126002  32.10630    benign
## 2 -20.002728 -20.17814    benign
## 3 -12.207140  41.77637    benign
## 4   8.327698 -24.82569    benign
## 5  11.560514  34.23023    benign
## 6  14.006864 -56.03128 malignant
# 绘制降维后图形
ggplot(res, aes(x = V1, y = V2, color = Class)) + geom_point(size = 1.25) + labs(title = "t-SNE",x = "TSNE1", y = "TSNE2") + theme(plot.title = element_text(hjust = 0.5)) + theme_bw()

4. 单细胞转录组数据集降维

set.seed(123)
tsne <- Rtsne(as.matrix(t(sc_example_counts)), pca = FALSE, perplexity = 10, theta = 0)  # Run TSNE
res <- as.data.frame(tsne$Y)
res$Class = sc_example_cell_info[sc_example_cell_info$Cell %in% colnames(sc_example_counts),]$Cell_Cycle
length(unique(res$Class))
## [1] 4
head(res)
##          V1         V2 Class
## 1 -52.47414  29.539762     S
## 2  24.23850 -12.023524    G0
## 3  28.90259  48.070417    G1
## 4  72.40250   9.639261     S
## 5 -41.68105  53.376565    G1
## 6  38.48649  12.414251    G0
ggplot(res, aes(x = V1, y = V2, color = Class)) + geom_point(size = 1.25) + labs(title = "t-SNE",x = "TSNE1", y = "TSNE2") + theme(plot.title = element_text(hjust = 0.5)) + theme_bw()

我这里都给大家分析三个数据集的,方便大家选择适合自己的数据方法,另外需要代码的将这期教程转发朋友圈,并配文“学生信,找桓峰基因,铸造成功的你!”即可获得!

桓峰基因,铸造成功的您!

有想进生信交流群的老师可以扫最后一个二维码加微信,备注“单位+姓名+目的”,有些想发广告的就免打扰吧,还得费力气把你踢出去!

References:

  1. Maaten, L. Van Der, 2014. Accelerating t-SNE using Tree-Based Algorithms. Journal of Machine Learning Research, 15, p.3221-3245.

  2. van der Maaten, L.J.P. & Hinton, G.E., 2008. Visualizing High-Dimensional Data Using t-SNE. Journal of Machine Learning Research, 9, pp.2579-2605.

MachineLearning 12. 机器学习之降维方法t-SNE及可视化 (Rtsne)相关推荐

  1. MachineLearning 13. 机器学习之降维方法UMAP及可视化 (umap)

    点击关注,桓峰基因 桓峰基因公众号推出机器学习应用于临床预测的方法,跟着教程轻松学习,每个文本教程配有视频教程大家都可以自由免费学习,目前已有的机器学习教程整理出来如下: MachineLearnin ...

  2. 机器学习基础---降维方法---T分布随机近邻嵌入(TSNE)推导

    T-SNE(T-Stochastic Neighbor Embedding) 核心思想: 对无监督聚类问题: PCA目的是在样本空间内找到子空间,以变换矩阵W对样本矩阵XXX实现原空间到子空间的映射, ...

  3. python降维方法_机器学习数据降维方法总结(附python代码)

    介绍 在机器学习实战时,如果面对一个数据集具有上千个特征,那么对于模型训练将是一个巨大的挑战.面对如此多的数据变量,如果我们认真的去分析每一个变量将耗费我们几周甚至几个月的时间,那么你估计也要被开除了 ...

  4. pca 累积方差贡献率公式_机器学习数据降维方法:PCA主成分分析

    PCA在机器学习中很常用,是一种无参数的数据降维方法.PCA步骤: 将原始数据按列组成n行m列矩阵X 将X的每一行(代表一个属性字段)进行零均值化,即减去这一行的均值 求出协方差矩阵 求出协方差矩阵的 ...

  5. 机器学习之降维方法(LDA、PCA)小结

    1 线性判别分析(LDA) 1.1 LDA简介 LDA是一种监督学习的降维技术,也就是说它的数据集的每个样本是有类别输出的.一句话来概括LDA的核心思想,[投影后类内方差最小,类间方差最大].我们将数 ...

  6. 机器学习基础---降维方法---局部保持投影(LPP)推导

    LPP 方法概述 核心思想 有映射Ym∗n=f(Xd∗n)\underset{m*n}{Y}=f(\underset {d*n}X)m∗nY​=f(d∗nX​),能够实现将d维的样本变换到m维空间之中 ...

  7. 机器学习四大降维方法

    引言: 机器学习领域中所谓的降维就是指采用某种映射方法,将原高维空间中的数据点映射到低维度的空间中.降维的本质是学习一个映射函数 f : x->y,其中x是原始数据点的表达,目前最多使用向量表达 ...

  8. 机器学习--特征降维方法总结

    本篇博文主要总结一下机器学习里面特征降维的方法,以及各种方法之间的联系和区别. 机器学习中我个人认为有两种途径可以来对特征进行降维,一种是特征抽取,其代表性的方法是PCA,SVD降维等,另外一个途径就 ...

  9. 详解机器学习高维数据降维方法

    当特征选择完成后,可以直接训练模型了,但是可能由于特征矩阵过大,导致计算量大,训练时间长的问题,因此降低维度也是必不可少的. 常见的降维方法除了以上提到的基于 L1 惩罚项的模型以外,另外还有主成分分 ...

最新文章

  1. OpenCASCADE:网格之BRepMesh 架构
  2. 有源光缆AOC知识百科汇总
  3. 你所不知道的setTimeout
  4. USB设备多个配置描述符的获取过程
  5. 第10篇:Flowable-BPMN操作流程部署、启动
  6. Linux软件安装方法小结
  7. 安卓学习笔记08:常用布局 - 线性布局
  8. python离散积分_科学网—python数据处理笔记(三)通道积分图 - 钱磊的博文
  9. python编程入门书籍-编程小白的第一本 Python 入门书
  10. php7 mysql部署_windows上apache+php+mysql环境部署(php7安装失败,勿模仿!!)
  11. 卡巴斯基离线病毒库升级办法
  12. 城市应急指挥系统详情分析及建设方案概述
  13. 【NOI2018】你的名字(后缀自动机,线段树合并)
  14. power bi 如何构建不同年份的月度对比_以Google Analytics为例,盘点BI产品四大要素...
  15. 使用阿里云物联网平台IoT Studio控制海康摄像头旋转
  16. 通信原理第6章节-数字基带传输系统
  17. linux 磁盘分区的原理,linux磁盘—分区原理
  18. 9.8.1 1.打印head标签的内容。2.打印body标签的内容。3.打印id为Hi的标签对象
  19. 计算机键盘上顿号在哪,顿号在键盘上怎么打?顿号怎么打出来?
  20. 由浅入深:Python 中如何实现自动导入缺失的库?

热门文章

  1. 谷粒商城-人人开源前端npm install下载依赖的时候出现问题
  2. 百万级用户接入PLC远程监控,10%的工控人都在使用的实用工具
  3. 金融数据分析之PDF年报中词频率统计
  4. [技术讨论]遇到电路反馈怎么办,看这里(二)
  5. 倍福TwinCAT(贝福Beckhoff)基础教程6.1 TwinCAT如何与高级语言通讯
  6. java 根据时区算出这个时区的时间
  7. android:id=quot;@+id/rootquot;,root – 商米开发者文档
  8. Pr如何复制粘贴在同一轨道?
  9. ArcGIS Web API 接入E都市瓦片
  10. 一次生产环境大量CLOSE_WAIT导致服务无法访问的定位过程