Advertisement
gakonst

Untitled

Jan 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. sdata = read.csv('sdata.txt')
  2.  
  3. library(cluster)
  4.  
  5. X = c(-4, 0, 4)
  6. Y = c(10, 0, 10)
  7. rnames = c("x1", "x2", "x3")
  8. centers = data.frame(X, Y, row.names = rnames)
  9.  
  10. model = kmeans(sdata, centers = centers)
  11. # model$centers
  12. # model$cluster
  13.  
  14. # Calculate cohesion and separation
  15. cohesion = model$tot.withinss
  16. separation = model$betweenss
  17.  
  18. # Q1
  19. cohesion
  20. # Q2
  21. separation
  22.  
  23. # Plot the data with the clusters
  24. plot(sdata, col = model$cluster)
  25. points(model$centers, col = 4, pch = "+", cex = 2)
  26.  
  27. # Calculate and plot silhouette
  28. model_silhouette = silhouette(model$cluster, dist(sdata))
  29. plot(model_silhouette)
  30. mean_silhouette = mean(model_silhouette[, 3])
  31.  
  32. # Q3
  33. mean_silhouette
  34.  
  35. X= c(-2, 2, 0)
  36. Y = c(0, 0, 10)
  37. rnames = c("x1", "x2", "x3")
  38. centers = data.frame(X, Y, row.names = rnames)
  39.  
  40. model = kmeans(sdata, centers = centers)
  41. model$centers
  42. model$cluster
  43.  
  44. # Plot the data with the clusters
  45. plot(sdata, col = model$cluster)
  46. points(model$centers, col = 4, pch = "+", cex = 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement