美文网首页R炒面
97-非监督学习之LLE非线性降维

97-非监督学习之LLE非线性降维

作者: wonphen | 来源:发表于2020-11-05 11:37 被阅读0次
> library(pacman)
> p_load(dplyr, lle, ggplot2, plot3D, plot3Drgl)

LLE: Locally Linear Embedding,局部线性嵌入,属于流形学习(Manifold Learning)的一种,其假设数据在较小的局部是线性的,也就是说,某一个数据可以由它邻域中的几个样本来线性表示。LLE 非常适合处理卷起或扭曲状的数据。

1、LLE步骤总结

1.计算行间距,设定超参数k。
2.对一行选出其最近的k行,表示为其线性组合,该线性组合系数为权重。
3.对每行重复操作,使得数据在2或3维空间中(近乎)保持该线性组合关系。

2、准备数据

> data(lle_scurve_data, package = "lle")
> scurve <- as.data.frame(lle_scurve_data)
> str(scurve)
## 'data.frame':    800 obs. of  3 variables:
##  $ V1: num  0.955 -0.66 -0.983 0.954 0.958 ...
##  $ V2: num  4.951 3.267 1.257 1.683 0.186 ...
##  $ V3: num  -0.174 -0.773 -0.296 -0.18 -0.161 ...
> # 设置列名
> names(scurve) <- c("x", "y", "z")
> # 检查缺失值
> DataExplorer::profile_missing(scurve)
##   feature num_missing pct_missing
## 1       x           0           0
## 2       y           0           0
## 3       z           0           0
> scatter3D(x = scurve$x, y = scurve$y, z = scurve$z, pch = 19,
+           bty = "b2", colkey = F, theta = 35, phi = 10,
+           col = ramp.col(c("red", "cyan")))
数据三维图
> # 让3D图像可以用鼠标转动
> plot3Drgl::plotrgl()

3、LLE超参数

除了维数,k (近邻数量)是唯一需要确定的超参数。
K可以通过函数计算出来:calc_k()
m 表示维数,通常2 或 3;
kmin,kmax 决定 k 取值域;
parallel,是否多核运行,默认为否;
cpus 指定使用 cpu 核数;

4、降维

> llek <- calc_k(scurve, m = 2, kmin = 1, kmax = 20, parallel = T,
+                cpus = parallel::detectCores())
> # 找出使rho最小的K值
> k <- filter(llek, rho == min(llek$rho))
> k
##    k       rho
## 1 17 0.1468803

使用最优的K值,降维:

> lle.scurve <- lle(scurve, m = 2, k = k$k)
## finding neighbours
## calculating weights
## computing coordinates
> str(lle.scurve)
## List of 4
##  $ Y     : num [1:800, 1:2] 1.756 0.428 -0.944 -0.626 -1.718 ...
##  $ X     :'data.frame':  800 obs. of  3 variables:
##   ..$ x: num [1:800] 0.955 -0.66 -0.983 0.954 0.958 ...
##   ..$ y: num [1:800] 4.951 3.267 1.257 1.683 0.186 ...
##   ..$ z: num [1:800] -0.174 -0.773 -0.296 -0.18 -0.161 ...
##  $ choise: NULL
##  $ id    : NULL

使用降维后的数据画图:

> tibble(LLE1 = lle.scurve$Y[, 1],
+        LLE2 = lle.scurve$Y[, 2],
+        z = scurve$z) %>% 
+   ggplot(aes(LLE1, LLE2, col = z)) +
+   geom_point(size = 1) +
+   # 按z值大小填充渐变色
+   scale_color_gradient(low = "cyan", high = "red") +
+   theme_bw() +
+   theme(legend.position = "top")
二维图

5、练习

1.给 flea 的LLE 二维散点图上加上代表种类的椭圆。

> data(flea, package = "GGally")
> flea.scale <- flea %>% 
+   as_tibble() %>%
+   # 去掉species变量
+   dplyr::select(-species) %>% 
+   # 标准化
+   scale()
> 
> flea.k <- calc_k(flea.scale, m = 2, kmin = 1, kmax = 20, plotres = F,
+                  parallel = T, cpus = parallel::detectCores())
## Library lle loaded.
## best k: 12 16 15
> flea.lle <- lle(flea.scale, m = 2, k = filter(flea.k, rho == min(flea.k$rho))$k)
## finding neighbours
## calculating weights
## computing coordinates
> p1 <- tibble(LLE1 = flea.lle$Y[, 1],
+              LLE2 = flea.lle$Y[, 2],
+              species = flea$species) %>% 
+       ggplot(aes(LLE1, LLE2, col = species)) +
+       geom_point(size = 2) +
+       stat_ellipse(level = 0.93) +
+       theme_bw() +
+       theme(legend.position = "top")
> p1
标准化后的数据

2.设定三维来做LLE,并用 scatter3D() 函数来作图。

> scatter3D(x = flea.lle$Y[, 1], y = flea.lle$Y[, 2], z = flea$head, pch = 19,
+           bty = "b2", colkey = F, theta = 35, phi = 10,
+           col = ramp.col(c("red", "cyan")))
三维图

3.不使用scale函数直接做二维LLE,并绘制散点图。

> flea2 <- flea %>% 
+   as_tibble() %>%
+   dplyr::select(-species)
> 
> flea.k2 <- calc_k(flea2, m = 2, kmin = 1, kmax = 20, plotres = F,
+                   parallel = T, cpus = parallel::detectCores())
## Library lle loaded.
## best k: 19 18 20
> flea.lle2 <- lle(flea2, m = 2, k = filter(flea.k, rho == min(flea.k$rho))$k)
## finding neighbours
## calculating weights
## computing coordinates
> p2 <- tibble(LLE1 = flea.lle2$Y[, 1],
+              LLE2 = flea.lle2$Y[, 2],
+              species = flea$species) %>% 
+       ggplot(aes(LLE1, LLE2, col = species)) +
+       geom_point(size = 2) +
+       stat_ellipse(level = 0.93) +
+       theme_bw() +
+       theme(legend.position = "none")

与标准化的图拼在一起对比:

> p_load(patchwork)
> 
> p1 + p2
标准化与非标准化数据对比

6、LLE总结

LLE是广泛使用的图形图像降维方法,它实现简单,但是对数据的流形分布特征有严格的要求,比如不能是闭合流形,不能是稀疏的数据集,不能是分布不均匀的数据集等等,这限制了它的应用。

LLE算法的主要优点有:
1)可以学习任意维的局部线性的低维流形。
2)算法归结为稀疏矩阵特征分解,计算复杂度相对较小,实现容易。

LLE算法的主要缺点有:
1)算法所学习的流形只能是不闭合的,且样本集是稠密均匀的。
2)算法对最近邻样本数的选择敏感,不同的最近邻数对最后的降维结果有很大影响。

7、SOM与LLE

SOM 与 LLE 的优点:
1.非线性还原算法。
2.新数据可以映射到SOM上。
3.训练成本相当不高。
4.LLE算法可重复。

SOM 与 LLE 的缺点:
1.不能处理分类变量。
2.不能直接用原始变量解释。
3.对不同尺度的数据很敏感。
4.新数据不能映射到LLE上。
5.不一定保留数据的全局结构。
6.SOM算法每次都会产生不同的结果。
7.SOM在大型数据集上效果更好。

相关文章

网友评论

    本文标题:97-非监督学习之LLE非线性降维

    本文链接:https://www.haomeiwen.com/subject/lpnnvktx.html