Last active
December 19, 2015 16:26
-
-
Save tvladeck/03252c3ca0c968dfe6fa to your computer and use it in GitHub Desktop.
spectral clustering for random forest proximity / similarity matrix
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
spectral <- function(proximity, k = 2) | |
{ | |
D <- diag(apply(proximity, 1, sum)) | |
U <- D - proximity | |
# L <- diag(nrow(my.data)) - solve(D) %*% A # simple Laplacian | |
# round(L[1:12,1:12],1) | |
# matrix power operator: computes M^power (M must be diagonalizable) | |
"%^%" <- function(M, power) | |
with(eigen(M), vectors %*% (values^power * solve(vectors))) | |
# L <- (D %^% (-1/2)) %*% A %*% (D %^% (-1/2)) # normalized Laplacian | |
evL <- eigen(U, symmetric=TRUE) | |
Z <- evL$vectors[,(ncol(evL$vectors)-k+1):ncol(evL$vectors)] | |
km <- kmeans(Z, centers=k, nstart=5) | |
return(km) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment