gakonst

Untitled

Jan 23rd, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. rm(list=ls())
  2.  
  3. library(cluster)
  4. library(mixtools)
  5.  
  6. kmdata = read.csv("kmdata.txt")
  7. target = kmdata[, 3]
  8. kmdata = kmdata[, 1:2]
  9.  
  10. # Q1
  11. plot(kmdata, col=target)
  12.  
  13. # Q2
  14. model = kmeans(kmdata, 3)
  15. plot(kmdata, col=model$cluster)
  16.  
  17. # Q3
  18. model = mvnormalmixEM(kmdata, k = 3, epsilon = 0.0001)
  19. plot(model, which = 2)
  20. # Alternatively use this
  21. # clusters = max.col(model$posterior)
  22. # centers = matrix(unlist(model$mu), byrow = TRUE, ncol = 2)
  23. # plot(gsdata, col = clusters)
  24. # points(centers, col = 4, pch = "+", cex = 2)
  25. # for (i in 1:3) ellipse(mu = model$mu[[i]], sigma = model$sigma[[i]])
Advertisement
Add Comment
Please, Sign In to add comment