MachineLearning 13. 机器学习之降维方法UMAP及可视化 (umap)
點擊關注,桓峰基因
桓峰基因公眾號推出機器學習應用于臨床預測的方法,跟著教程輕松學習,每個文本教程配有視頻教程大家都可以自由免費學習,目前已有的機器學習教程整理出來如下:
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)
MachineLearning 13. 機器學習之降維方法UMAP及可視化 (umap)
這期介紹一下NB的最佳降維方法之一 UMAP,并實現在多個數據集上的應用,尤其是單細胞測序數據。
前言
UMAP(Uniform Manifold Approximation and Projection)是一種非線性降維的算法,相對于t-SNE,UMAP算法更加快速 該方法的原理是利用流形學和投影技術,達到降維目的 首先計算高維空間中的點之間的距離,將它們投影到低維空間,并計算該低維空間中的點之間的距離。然后,它使用隨機梯度下降來最小化這些距離之間的差異。
軟件安裝
設置清華鏡像,加快包的安裝如下:
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) if(!require(umap))install.packages("umap") if(!require(uwot))install.packages("uwot")數據讀取
這里我們準備了三個數據集,一個是例子中給出來的,一個是乳腺癌患者活檢數據集,另一個是單細胞轉錄組的數據集。
1. 埃德加·安德森的iris數據
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. 乳腺癌患者活檢數據
我們已經多次使用過這個數據集了,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 malignant3. 單細胞轉錄組數據集
單細胞數據我們可以從scatter這個軟件包獲取,但是目前失效了,我到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參數說明
umap函數主要的就是method參數,有兩個:na?ve純R語言編寫;umap-learn需要調用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,這就是模型的配置函數了。它有一個默認的配置列表:
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其中參數的意義:
n_neighbors:確定相鄰點的數量,通常其設置在2-100之間。
n_components:降維的維數大小,默認是2,其范圍最好也在2-100之間。
Metric:距離的計算方法,有很多可以選擇,具體的需要我們在應用的時候自行篩選。如:euclidean,manhattan,chebyshev,minkowski,canberra,braycurtis,mahalanobis,wminkowski,seuclidean,cosine,correlation,haversine,hamming,jaccard,dice,russelrao,kulsinski,rogerstanimoto,sokalmichener,sokalsneath,yule。這個地方需要注意的是如果需要傳參的算法,可以利用metric_kwds設置(此值python有)。
n_epochs:模型訓練迭代次數。數據量大時200,小時500。
input:數據的類型,如果是data就會按照數據進行計算;如果dist就會認為是距離矩陣進行訓練。
init:初始化用的。其中有這么三種方式:spectral,random,自定義。
min_dist:控制允許嵌入的緊密程度,值越小點越聚集,默認一般是0.1。
set_op_mix_ratio:設置降維過程中,各特征的結合方式,值0-1。0代表取交集,1代表取合集;中間就是比例。
local_connectivity:局部連接的點之間值,默認1,其值越大局部連接越多,導致的結果就是超越固有的流形維數出現改變。
bandwith:用于構造子集參數,具體怎么設,就默認吧。
alpha:相當于在python中的leanging_rate(學習率)參數。
gamma:布局最優的學習率
negative_sample_rate:每一個陽性樣本導致的陰性率。其值越大導致高的優化也就是過擬合,預測準確度下降。默認是5.
a,b主要是關聯min_dist 和 spread。可以不用設置。
spread:有效的嵌入式降維范圍。與min_dist聯合使用。
random_state:此值主要是確保模型的可重復性。如果不設置基于np.random,每次將會不同。
transform_seed:此值用于數值轉換操作。一般默認42。
verbose:控制工作日志,防止存儲過多。
umap_learn_args:這個參數就牛了,他可以調用python基于umap-learn訓練好的參數。
custom.settings = umap.defaults custom.settings$n_neighbors = 5 custom.settings例子實操
1. iris數據集的降維
讀入矩陣,構造模型,給出結果:
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預測
最后就是預測,其實并沒有什么預測功能,主要就是用來降維,那既然提供了,那我們就簡單提下是怎么實現的。其實就是基于前面計算的參數,將新的數據與原始數據合并,然后計算出新的降維結果,看是否可以和原模型一樣。
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數據集的降維
讀入矩陣,構造模型,給出結果:
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. 單細胞轉錄組數據集降維
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合并三個數據集降維結果
library(patchwork) p1 | p2 | p3我這里都給大家分析三個數據集的,方便大家選擇適合自己的數據方法,另外需要代碼的將這期教程轉發朋友圈,并配文“學生信,找桓峰基因,鑄造成功的你!”即可獲得!
桓峰基因,鑄造成功的您!
有想進生信交流群的老師可以掃最后一個二維碼加微信,備注“單位+姓名+目的”,有些想發廣告的就免打擾吧,還得費力氣把你踢出去!
References:
總結
以上是生活随笔為你收集整理的MachineLearning 13. 机器学习之降维方法UMAP及可视化 (umap)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 修复好一个alpha865qqz的勒索病
- 下一篇: you_get下载视频报错 don‘t