Skip to content

Instantly share code, notes, and snippets.

@tvladeck
Last active December 19, 2015 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvladeck/03252c3ca0c968dfe6fa to your computer and use it in GitHub Desktop.
Save tvladeck/03252c3ca0c968dfe6fa to your computer and use it in GitHub Desktop.
spectral clustering for random forest proximity / similarity matrix
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