forked from szcf-weiya/ESL-CN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e149ef7
commit 34963ab
Showing
9 changed files
with
130 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ site | |
**/.Archive | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
.ipynb_checkpoints/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
rm(list = lm(all=TRUE)) | ||
## | ||
## solution to Ex. 14.5 | ||
## | ||
|
||
## ############### | ||
## generate data | ||
## ############### | ||
|
||
## center at (0, 0, 1) | ||
theta1 = runif(30, -pi/8, pi/8) | ||
phi1 = runif(30, 0, 2*pi) | ||
x1 = sin(theta1)*cos(phi1) + rnorm(30, 0, 0.6) | ||
y1 = sin(theta1)*sin(phi1) + rnorm(30, 0, 0.6) | ||
z1 = cos(theta1) + rnorm(30, 0, 0.6) | ||
|
||
## center at (1, 0, 0) | ||
theta2 = runif(30, pi/2-pi/4, pi/2+pi/4) | ||
phi2 = runif(30, -pi/4, pi/4) | ||
x2 = sin(theta2)*cos(phi2) + rnorm(30, 0, 0.6) | ||
y2 = sin(theta2)*sin(phi2) + rnorm(30, 0, 0.6) | ||
z2 = cos(theta2) + rnorm(30, 0, 0.6) | ||
|
||
## center at (0, 1, 0) | ||
theta3 = runif(30, pi/2-pi/4, pi/2 + pi/4) | ||
phi3 = runif(30, pi/2-pi/4, pi/2+pi/4) | ||
x3 = sin(theta3)*cos(phi3) + rnorm(30, 0, 0.6) | ||
y3 = sin(theta3)*sin(phi3) + rnorm(30, 0, 0.6) | ||
z3 = cos(theta3) + rnorm(30, 0, 0.6) | ||
|
||
## ############### | ||
## initialize | ||
## ############### | ||
q1 = q2 = 5 # K = 25 | ||
x = c(x1, x2, x3) | ||
y = c(y1, y2, y3) | ||
z = c(z1, z2, z3) | ||
data = data.frame(x, y, z) | ||
idx = sample(90, 25) | ||
coor = data[idx, ] | ||
coor.grid = expand.grid(1:5, 1:5) | ||
|
||
## find the closest prototype to x in Euclidean distance in R^p | ||
classify <- function(x, prototype) | ||
{ | ||
d = apply(prototype, 1, function(y) sum((x-y)^2)) | ||
return(as.numeric(which.min(d))) | ||
} | ||
## can be optimized | ||
fulldist <- function(x) | ||
{ | ||
n = nrow(x) | ||
res = matrix(nrow = n, ncol = n) | ||
for(i in 1:n) | ||
{ | ||
for (j in 1:n) | ||
{ | ||
res[i, j] = sqrt(sum((x[i, ]- x[j, ])^2)) | ||
} | ||
} | ||
return(res) | ||
} | ||
## calculate the distance of vector to a matrix | ||
distance <- function(x, m) | ||
{ | ||
res = apply(m, 1, function(y) sqrt(sum((x-y)^2))) | ||
return(as.numeric(res)) | ||
} | ||
plot.som <- function(data, coor, iter) | ||
{ | ||
#plot(expand.grid(1:5, 1:5), cex = 10, xlim=c(0.5, 5.5), ylim=c(0.5, 5.5), pty="s") | ||
res = apply(data, 1, function(x) classify(x, coor)) | ||
res = res - 0.001 ## avoid divisible | ||
res.x = floor(res/5) | ||
res.y = res - res.x*5 | ||
#res.y[res.y == 0] == 5 | ||
res.x = res.x + 1 + runif(90, -0.3, 0.3) # avoid overlap | ||
res.y = res.y + runif(90, -0.3, 0.3) | ||
#points(res.x[1:30], res.y[1:30], col = "red", pch = 16, cex = 0.8) | ||
plot(res.x[1:30], res.y[1:30], col = "red", pch = 16, xlim = c(0.5, 5.5), ylim = c(0.5, 5.5), pty="s", xlab = NA, ylab = NA, main = paste0("iteration ", iter)) | ||
points(res.x[31:60], res.y[31:60], col = "green", pch = 16) | ||
points(res.x[61:90], res.y[61:90], col = "blue", pch = 16) | ||
xy = expand.grid(1:5, 1:5) | ||
symbols(xy[,1], xy[,2], circles = rep(0.45, nrow(xy)), add = T, inches = F) | ||
} | ||
## initial configuration | ||
png("iter_0.png") | ||
plot.som(data, coor, 0) | ||
dev.off() | ||
|
||
R = 2 | ||
niter = 40 | ||
for (iter in 1:niter) | ||
{ | ||
alpha = -1/niter*iter + 1 | ||
r = -1/niter*iter + 2 | ||
for (i in 1:90) | ||
{ | ||
xi = data[i, ] | ||
mj.idx = classify(xi, coor) | ||
mj = coor.grid[mj.idx, ] | ||
mk.idx = which(distance(mj, coor.grid) <= r) | ||
mk = coor.grid[mk.idx, ] | ||
# mj11 = t(sapply(1:R, function(x) c(mj[1]+x, mj[2]))) | ||
# mj12 = t(sapply(1:R, function(x) c(mj[1]-x, mj[2]))) | ||
# mj21 = t(sapply(1:R, function(x) c(mj[1], mj[2]+x))) | ||
# mj22 = t(sapply(1:R, function(x) c(mj[1], mj[2]-x))) | ||
# ## include itself | ||
# mj.neighbor = rbind(mj, mj11, mj12, mj21, mj22) | ||
# ## remove outside elements | ||
# mj.neighbor = mj.neighbor[mj.neighbor[,1] <= 5 & mj.neighbor[,1] >= 1 & mj.neighbor[,2] <= 5 & mj.neighbor[,2] >= 1,] | ||
# mj.neighbor = mj.neighbor[distance(mj, mj.neighbor) <= R, ] | ||
xi.m = matrix(rep(1, length(mk.idx),nrow = length(mk.idx))) %*% as.matrix(xi) | ||
coor[mk.idx, ]= coor[mk.idx, ] + alpha*(xi.m - coor[mk.idx, ]) | ||
} | ||
if (iter - 10*floor(iter/10) == 0) | ||
{ | ||
png(paste0("iter_", iter, ".png")) | ||
plot.som(data, coor, iter) | ||
dev.off() | ||
} | ||
} | ||
|
||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes