R/k_means.R

library(e1071)

k_means=function(x,k){
  
  set.seed(42)
  x=as.matrix(x)
  res=kmeans(x,k)
  tss=res$tot.withinss
  
  for (i in 1:30){
    set.seed(42)
    res1=kmeans(x,k)
    tss1=res1$tot.withinss
    if (tss<tss1){res=res}else{res=res1}
    tss=res$tot.withinss
    
  }
  #return(res$centers)
  structure(list(centers=res$centers,cluster=res$cluster))
  
  
}

Try the RobustEM package in your browser

Any scripts or data that you put into this service are public.

RobustEM documentation built on April 14, 2017, 10:05 a.m.