This is an example for LLE(Locally Linear Embedding) with R program.
We will use functions from “lle” package to achieve our practice.
library(lle)
# using the default data containing 3-columns(3-dimensions) from "lle" package
data(lle_scurve_data)
X <- lle_scurve_data
head(X, n=3)
## [,1] [,2] [,3]
## [1,] 0.9553277 4.951205 -0.1744640
## [2,] -0.6600097 3.266908 -0.7732213
## [3,] -0.9832013 1.257341 -0.2963627
# construct neightbor matrix
neighbours <- find_nn_k(X, k=15)
neighbours[1:6, 1:6]
## 1 2 3 4 5 6
## 1 FALSE FALSE FALSE FALSE FALSE FALSE
## 2 FALSE FALSE FALSE FALSE FALSE FALSE
## 3 FALSE FALSE FALSE FALSE FALSE FALSE
## 4 FALSE FALSE FALSE FALSE FALSE FALSE
## 5 FALSE FALSE FALSE FALSE FALSE TRUE
## 6 FALSE FALSE FALSE FALSE TRUE FALSE
# calculate weights between x and its neighbors
weights <- find_weights(neighbours, X, m=2, reg=2)
weights$wgts[1:6, 1:6]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0 0 0 0 0.00000000 0.00000000
## [2,] 0 0 0 0 0.00000000 0.00000000
## [3,] 0 0 0 0 0.00000000 0.00000000
## [4,] 0 0 0 0 0.00000000 0.00000000
## [5,] 0 0 0 0 0.00000000 -0.03799705
## [6,] 0 0 0 0 0.04279949 0.00000000
library(scatterplot3d)
# the 3-D graph of original data
scatterplot3d(x=X[,1], y=X[,2], z=X[,3], color=X[,2])
# peform LLE
# K=5
k5 <- lle(X, m=2, k=5, reg=2, ss=FALSE, id=TRUE, v=0.9 )
## finding neighbours
## calculating weights
## intrinsic dim: mean=1.92125, mode=2
## computing coordinates
plot(k5$Y, main="K=5 data", xlab=expression(y[1]), ylab=expression(y[2]))
# K=15
k15 <- lle(X, m=2, k=15, reg=2, ss=FALSE, id=TRUE, v=0.9 )
## finding neighbours
## calculating weights
## intrinsic dim: mean=2, mode=2
## computing coordinates
plot(k15$Y, main="K=15 data", xlab=expression(y[1]), ylab=expression(y[2]))
# K=40
k40 <- lle(X, m=2, k=40, reg=2, ss=FALSE, id=TRUE, v=0.9 )
## finding neighbours
## calculating weights
## intrinsic dim: mean=2.00625, mode=2
## computing coordinates
plot(k40$Y, main="K=40 data", xlab=expression(y[1]), ylab=expression(y[2]))