Guest User

Untitled

a guest
Feb 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. library(ggplot2)
  2.  
  3. input_data <- as.matrix(read.table("SpectData.txt"))
  4. colnames(input_data) <- c("x1", "x2")
  5.  
  6. #1- Define Weights matrix
  7. W <- matrix(0,nrow = nrow(input_data),ncol = nrow(input_data))
  8.  
  9. #2- Define Degree Matrix
  10. D <- matrix(0,nrow = nrow(input_data),ncol = nrow(input_data))
  11.  
  12. calculateWeight <- function(x1,x2,sigma) {
  13. result <- exp(- (norm(x2-x1,type = "2"))^2/ (2*sigma^2))
  14. result
  15. }
  16.  
  17. calcWieghtMatrix <- function(sigma) {
  18. for(i in 1: nrow(W)){
  19. for(j in 1: nrow(W)){
  20. if(i == j){
  21. next
  22. }
  23. if( W[i,j] != 0){
  24. next
  25. }
  26.  
  27. W[i,j] <<- calculateWeight(input_data[i,],input_data[j,],sigma)
  28. W[j,i] <<- W[i,j]
  29. }
  30. }
  31. }
  32.  
  33. calcDegreeMatrix <- function() {
  34. for( i in 1:nrow(input_data)){
  35. D[i,i] <<- sum(W[i,])
  36. }
  37. }
  38.  
  39. executeSpectralClustring <- function (sigma,numberOfClusters){
  40. calcWieghtMatrix(sigma)
  41. calcDegreeMatrix()
  42. L <<- D - W
  43. eigenDecompostion <- eigen(L,symmetric = FALSE)
  44. index <- ncol(eigenDecompostion$vectors)-1
  45. eigenVector <- eigenDecompostion$vectors[,index]
  46. cl <- kmeans(eigenVector,numberOfClusters)
  47. ggplot(input_data,col = cl$cluster)
  48. }
  49.  
  50. executeSpectralClustring(0.01,2)
  51.  
  52. m <- cbind(1:10, sin(1:10), cos(1:10))
  53. ggplot.matrix <- function (data = NULL, mapping = aes(), ..., environment = parent.frame())
  54. {
  55. d <- reshape2::melt(as.data.frame(data), 1)
  56. ggplot(d, mapping, environment = environment)
  57. }
  58.  
  59. ggplot(m, aes(V1,value, colour=variable)) + geom_line()
  60.  
  61. dataFrame<-data.frame(classMatrix)
  62.  
  63. ggplot(input_data1,aes(x=input_data1$x1, y=input_data1$x2, colour = cl$cluster))+ geom_point(shape=19)
Add Comment
Please, Sign In to add comment