Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(ggplot2)
- input_data <- as.matrix(read.table("SpectData.txt"))
- colnames(input_data) <- c("x1", "x2")
- #1- Define Weights matrix
- W <- matrix(0,nrow = nrow(input_data),ncol = nrow(input_data))
- #2- Define Degree Matrix
- D <- matrix(0,nrow = nrow(input_data),ncol = nrow(input_data))
- calculateWeight <- function(x1,x2,sigma) {
- result <- exp(- (norm(x2-x1,type = "2"))^2/ (2*sigma^2))
- result
- }
- calcWieghtMatrix <- function(sigma) {
- for(i in 1: nrow(W)){
- for(j in 1: nrow(W)){
- if(i == j){
- next
- }
- if( W[i,j] != 0){
- next
- }
- W[i,j] <<- calculateWeight(input_data[i,],input_data[j,],sigma)
- W[j,i] <<- W[i,j]
- }
- }
- }
- calcDegreeMatrix <- function() {
- for( i in 1:nrow(input_data)){
- D[i,i] <<- sum(W[i,])
- }
- }
- executeSpectralClustring <- function (sigma,numberOfClusters){
- calcWieghtMatrix(sigma)
- calcDegreeMatrix()
- L <<- D - W
- eigenDecompostion <- eigen(L,symmetric = FALSE)
- index <- ncol(eigenDecompostion$vectors)-1
- eigenVector <- eigenDecompostion$vectors[,index]
- cl <- kmeans(eigenVector,numberOfClusters)
- ggplot(input_data,col = cl$cluster)
- }
- executeSpectralClustring(0.01,2)
- m <- cbind(1:10, sin(1:10), cos(1:10))
- ggplot.matrix <- function (data = NULL, mapping = aes(), ..., environment = parent.frame())
- {
- d <- reshape2::melt(as.data.frame(data), 1)
- ggplot(d, mapping, environment = environment)
- }
- ggplot(m, aes(V1,value, colour=variable)) + geom_line()
- dataFrame<-data.frame(classMatrix)
- 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