MachineLearning 13. 机器学习之降维方法UMAP及可视化 (umap)
點(diǎn)擊關(guān)注,桓峰基因
桓峰基因公眾號(hào)推出機(jī)器學(xué)習(xí)應(yīng)用于臨床預(yù)測(cè)的方法,跟著教程輕松學(xué)習(xí),每個(gè)文本教程配有視頻教程大家都可以自由免費(fèi)學(xué)習(xí),目前已有的機(jī)器學(xué)習(xí)教程整理出來(lái)如下:
MachineLearning 1. 主成分分析(PCA)
MachineLearning 2. 因子分析(Factor Analysis)
MachineLearning 3. 聚類分析(Cluster Analysis)
MachineLearning 4. 癌癥診斷方法之 K-鄰近算法(KNN)
MachineLearning 5. 癌癥診斷和分子分型方法之支持向量機(jī)(SVM)
MachineLearning 6. 癌癥診斷機(jī)器學(xué)習(xí)之分類樹(shù)(Classification Trees)
MachineLearning 7. 癌癥診斷機(jī)器學(xué)習(xí)之回歸樹(shù)(Regression Trees)
MachineLearning 8. 癌癥診斷機(jī)器學(xué)習(xí)之隨機(jī)森林(Random Forest)
MachineLearning 9. 癌癥診斷機(jī)器學(xué)習(xí)之梯度提升算法(Gradient Boosting)
MachineLearning 10. 癌癥診斷_機(jī)器學(xué)習(xí)_之神經(jīng)網(wǎng)絡(luò)(Neural network)
MachineLearning 11. 機(jī)器學(xué)習(xí)之隨機(jī)森林生存分析(randomForestSRC)
MachineLearning 12. 機(jī)器學(xué)習(xí)之降維方法t-SNE及可視化(Rtsne)
MachineLearning 13. 機(jī)器學(xué)習(xí)之降維方法UMAP及可視化 (umap)
這期介紹一下NB的最佳降維方法之一 UMAP,并實(shí)現(xiàn)在多個(gè)數(shù)據(jù)集上的應(yīng)用,尤其是單細(xì)胞測(cè)序數(shù)據(jù)。
前言
UMAP(Uniform Manifold Approximation and Projection)是一種非線性降維的算法,相對(duì)于t-SNE,UMAP算法更加快速 該方法的原理是利用流形學(xué)和投影技術(shù),達(dá)到降維目的 首先計(jì)算高維空間中的點(diǎn)之間的距離,將它們投影到低維空間,并計(jì)算該低維空間中的點(diǎn)之間的距離。然后,它使用隨機(jī)梯度下降來(lái)最小化這些距離之間的差異。
軟件安裝
設(shè)置清華鏡像,加快包的安裝如下:
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) if(!require(umap))install.packages("umap") if(!require(uwot))install.packages("uwot")數(shù)據(jù)讀取
這里我們準(zhǔn)備了三個(gè)數(shù)據(jù)集,一個(gè)是例子中給出來(lái)的,一個(gè)是乳腺癌患者活檢數(shù)據(jù)集,另一個(gè)是單細(xì)胞轉(zhuǎn)錄組的數(shù)據(jù)集。
1. 埃德加·安德森的iris數(shù)據(jù)
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(umap) library(uwot) data("iris") iris.data <- iris[, grep("Sepal|Petal", colnames(iris))] iris.labels <- iris[, "Species"]2. 乳腺癌患者活檢數(shù)據(jù)
我們已經(jīng)多次使用過(guò)這個(gè)數(shù)據(jù)集了,class是二分類的結(jié)果變量惡性還是良性的。
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 malignant3. 單細(xì)胞轉(zhuǎn)錄組數(shù)據(jù)集
單細(xì)胞數(shù)據(jù)我們可以從scatter這個(gè)軟件包獲取,但是目前失效了,我到github上下載了一下,同樣可以使用:
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參數(shù)說(shuō)明
umap函數(shù)主要的就是method參數(shù),有兩個(gè):na?ve純R語(yǔ)言編寫;umap-learn需要調(diào)用python包。
Computes a manifold approximation and projection Description Computes a manifold approximation and projection Usage umap( d, config = umap.defaults, method = c(“naive”, “umap-learn”), preserve.seed = TRUE, … )
我們看下umap.defaults,這就是模型的配置函數(shù)了。它有一個(gè)默認(rèn)的配置列表:
umap.defaults ## umap configuration parameters ## n_neighbors: 15 ## n_components: 2 ## metric: euclidean ## n_epochs: 200 ## input: data ## init: spectral ## min_dist: 0.1 ## set_op_mix_ratio: 1 ## local_connectivity: 1 ## bandwidth: 1 ## alpha: 1 ## gamma: 1 ## negative_sample_rate: 5 ## a: NA ## b: NA ## spread: 1 ## random_state: NA ## transform_state: NA ## knn: NA ## knn_repeats: 1 ## verbose: FALSE## umap_learn_args: NA其中參數(shù)的意義:
n_neighbors:確定相鄰點(diǎn)的數(shù)量,通常其設(shè)置在2-100之間。
n_components:降維的維數(shù)大小,默認(rèn)是2,其范圍最好也在2-100之間。
Metric:距離的計(jì)算方法,有很多可以選擇,具體的需要我們?cè)趹?yīng)用的時(shí)候自行篩選。如:euclidean,manhattan,chebyshev,minkowski,canberra,braycurtis,mahalanobis,wminkowski,seuclidean,cosine,correlation,haversine,hamming,jaccard,dice,russelrao,kulsinski,rogerstanimoto,sokalmichener,sokalsneath,yule。這個(gè)地方需要注意的是如果需要傳參的算法,可以利用metric_kwds設(shè)置(此值python有)。
n_epochs:模型訓(xùn)練迭代次數(shù)。數(shù)據(jù)量大時(shí)200,小時(shí)500。
input:數(shù)據(jù)的類型,如果是data就會(huì)按照數(shù)據(jù)進(jìn)行計(jì)算;如果dist就會(huì)認(rèn)為是距離矩陣進(jìn)行訓(xùn)練。
init:初始化用的。其中有這么三種方式:spectral,random,自定義。
min_dist:控制允許嵌入的緊密程度,值越小點(diǎn)越聚集,默認(rèn)一般是0.1。
set_op_mix_ratio:設(shè)置降維過(guò)程中,各特征的結(jié)合方式,值0-1。0代表取交集,1代表取合集;中間就是比例。
local_connectivity:局部連接的點(diǎn)之間值,默認(rèn)1,其值越大局部連接越多,導(dǎo)致的結(jié)果就是超越固有的流形維數(shù)出現(xiàn)改變。
bandwith:用于構(gòu)造子集參數(shù),具體怎么設(shè),就默認(rèn)吧。
alpha:相當(dāng)于在python中的leanging_rate(學(xué)習(xí)率)參數(shù)。
gamma:布局最優(yōu)的學(xué)習(xí)率
negative_sample_rate:每一個(gè)陽(yáng)性樣本導(dǎo)致的陰性率。其值越大導(dǎo)致高的優(yōu)化也就是過(guò)擬合,預(yù)測(cè)準(zhǔn)確度下降。默認(rèn)是5.
a,b主要是關(guān)聯(lián)min_dist 和 spread。可以不用設(shè)置。
spread:有效的嵌入式降維范圍。與min_dist聯(lián)合使用。
random_state:此值主要是確保模型的可重復(fù)性。如果不設(shè)置基于np.random,每次將會(huì)不同。
transform_seed:此值用于數(shù)值轉(zhuǎn)換操作。一般默認(rèn)42。
verbose:控制工作日志,防止存儲(chǔ)過(guò)多。
umap_learn_args:這個(gè)參數(shù)就牛了,他可以調(diào)用python基于umap-learn訓(xùn)練好的參數(shù)。
custom.settings = umap.defaults custom.settings$n_neighbors = 5 custom.settings例子實(shí)操
1. iris數(shù)據(jù)集的降維
讀入矩陣,構(gòu)造模型,給出結(jié)果:
library(umap) # Set a seed if you want reproducible results iris.umap <- umap(iris.data) iris.umap # head(iris.umap$layout, 3) res <- as.data.frame(iris.umap$layout) res$Species = iris[, "Species"] head(res) library(ggplot2) p1 <- ggplot(res, aes(x = V1, y = V2, color = Species)) + geom_point(size = 1.25) +labs(title = "UMAP of iris", x = "UMAP1", y = "UMAP2") + theme(plot.title = element_text(hjust = 0.5)) +theme_bw() p1預(yù)測(cè)
最后就是預(yù)測(cè),其實(shí)并沒(méi)有什么預(yù)測(cè)功能,主要就是用來(lái)降維,那既然提供了,那我們就簡(jiǎn)單提下是怎么實(shí)現(xiàn)的。其實(shí)就是基于前面計(jì)算的參數(shù),將新的數(shù)據(jù)與原始數(shù)據(jù)合并,然后計(jì)算出新的降維結(jié)果,看是否可以和原模型一樣。
par(mfrow = c(1, 2)) iris.wnoise <- iris.data + matrix(rnorm(150 * 40, 0, 0.1), ncol = 4) colnames(iris.wnoise) <- colnames(iris.data) head(iris.wnoise, 3) iris.wnoise.umap <- predict(iris.umap, iris.wnoise) head(iris.wnoise.umap, 3) plot.iris(iris.umap, iris.labels) plot.iris(iris.wnoise.umap, iris.labels, add = F, pch = 4, legend.suffix = " (with noise)")2. biopsy數(shù)據(jù)集的降維
讀入矩陣,構(gòu)造模型,給出結(jié)果:
biopsy.umap <- umap(as.matrix(data[, 1:9])) res <- as.data.frame(biopsy.umap$layout) res$Class = data$class p2 <- ggplot(res, aes(x = V1, y = V2, color = Class)) + geom_point(size = 1.25) +labs(title = "UMAP of biopsy", x = "UMAP1", y = "UMAP2") + theme(plot.title = element_text(hjust = 0.5)) +theme_bw() p24. 單細(xì)胞轉(zhuǎn)錄組數(shù)據(jù)集降維
sc.umap <- umap(as.matrix(t(sc_example_counts))) res <- as.data.frame(sc.umap$layout) res$Class = sc_example_cell_info[sc_example_cell_info$Cell %in% colnames(sc_example_counts),]$Cell_Cycle length(unique(res$Class)) head(res) p3 <- ggplot(res, aes(x = V1, y = V2, color = Class)) + geom_point(size = 1.25) +labs(title = "UMAP of single cell", x = "UMAP1", y = "UMAP2") + theme(plot.title = element_text(hjust = 0.5)) +theme_bw() p3合并三個(gè)數(shù)據(jù)集降維結(jié)果
library(patchwork) p1 | p2 | p3我這里都給大家分析三個(gè)數(shù)據(jù)集的,方便大家選擇適合自己的數(shù)據(jù)方法,另外需要代碼的將這期教程轉(zhuǎn)發(fā)朋友圈,并配文“學(xué)生信,找桓峰基因,鑄造成功的你!”即可獲得!
桓峰基因,鑄造成功的您!
有想進(jìn)生信交流群的老師可以掃最后一個(gè)二維碼加微信,備注“單位+姓名+目的”,有些想發(fā)廣告的就免打擾吧,還得費(fèi)力氣把你踢出去!
References:
總結(jié)
以上是生活随笔為你收集整理的MachineLearning 13. 机器学习之降维方法UMAP及可视化 (umap)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 修复好一个alpha865qqz的勒索病
- 下一篇: you_get下载视频报错 don‘t