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?
kmeans
works on numerical matrices only. @thelatemail pointed out, column 5 ofiris
isn't numeric.- you use use
cl_predict
clue
instead ofpredict.kmeans
Comments
Post a Comment