日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/20 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MachineLearning 13. 机器学习之降维方法UMAP及可视化 (umap) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

點(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 malignant

3. 單細(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() p2

4. 單細(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:

  • Leland McInnes, John Healy, et al. UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction. Machine Learning (stat.ML).2018
  • 總結(jié)

    以上是生活随笔為你收集整理的MachineLearning 13. 机器学习之降维方法UMAP及可视化 (umap)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 中文字幕在线观看国产 | 欧美午夜性 | 美女网站黄频 | 亚洲vs天堂 | 日本精品免费一区二区三区 | 亚洲一二三不卡 | 一路向西在线看 | 国产91白丝在一线播放 | 国产精品白丝喷水在线观看 | 午夜插插 | 日本中文字幕在线观看 | 久久久一级黄色片 | 久久99精品国产.久久久久 | 伊人自拍 | 国产喷白浆一区二区三区 | 色综合久久久无码中文字幕波多 | 日韩欧美中文在线观看 | 91精品久久久久久久 | 久久高清 | 欧洲久久久| 俄罗斯av在线| 第一福利av | 日本一区二区免费视频 | 久久亚洲国产精品 | 五月天堂网 | 在线观看黄色国产 | 亚洲一区二区在线免费观看 | 91av影院| 波多野结衣中文一区 | 免费看国产精品 | 日日夜夜天天综合 | av毛片网站 | 日韩精品一区二区亚洲av性色 | 99国产精品一区二区 | 91国内视频 | 男人的天堂成人 | 久久无码人妻精品一区二区三区 | 天天操中文字幕 | 日韩中文字幕一区二区三区四区 | 国产美女精品视频 | 欧美日韩中文字幕一区 | 永久免费看成人av的动态图 | 欧美激情电影一区二区 | 邻家有女4完整版电影观看 欧美偷拍另类 | 奶水旺盛的女人伦理 | 国产激情自拍 | 激情五月色播五月 | www.日韩av.com| 免费黄网在线看 | 中文字幕有码av | 在线a| 国产绳艺sm调教室论坛 | 黄色网页在线 | 亚洲国产无码精品 | 国产99色 | 亚洲精品天堂网 | 人人干人人看 | 久久99精品波多结衣一区 | 男女拍拍拍 | 天堂欧美| 男女三级视频 | 亚洲天堂视频在线播放 | 捆绑japanhdxxxxvideos | 欧美图片一区 | 亚洲天堂视频网站 | 成人在线观看亚洲 | 欧美a在线播放 | av黄色免费 | 国产免费一区二区视频 | 日韩欧美一区二 | 国产中文在线 | 国产成人无码精品久久久久 | 秋霞午夜 | 精品亚洲中文字幕 | 内射干少妇亚洲69xxx | 国偷自拍| 欧美一区二区三区免 | 二区在线视频 | 51免费看成人啪啪片 | 黄网站免费观看 | 亚洲黄色小说视频 | 国产亚洲精品成人av在线 | 日韩美女视频在线 | 婷婷综合在线视频 | 国产这里有精品 | 黄色大全在线观看 | 久久精品人人 | 亚洲dvd | 中文字幕网伦射乱中文 | 视色视频| 国产一区二区小说 | 大肉大捧一进一出视频 | 欧美午夜精品一区二区三区电影 | 超碰人人网 | 女的高潮流时喷水图片大全 | 国产又色又爽又黄又免费 | 国产a免费观看 | 欧美在线xxxx| 秋霞一区|