k means - kmeans prediction using R -


km <- kmeans(iris,3)  predict.kmeans <-   function(km, data)  {k <- nrow(km$centers)  n <- nrow(data)  d <- as.matrix(dist(rbind(km$centers, data)))[-(1:k),1:k]  out <- apply(d, 1, which.min)  return(out)}  predict.kmeans(km,iris[1,]) # error:apply(d, 1, which.min) : dim(x) must have positive length 

i have problems simple code here,what's wrong it?

  1. kmeans works on numerical matrices only. @thelatemail pointed out, column 5 of iris isn't numeric.
  2. you use use cl_predict clue instead of predict.kmeans

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -