endamaco
endamaco

Reputation: 163

R - Predicting custom clustering

I've implemented my own clustering algorithms, what I need to know is how to make my clustering be usable by the default method "predict" for predicting the clustering belonging of instances of the testing set. I have the training set, I make the clusters based on them and I get a new object representing the centers of the clusters and for each instances of the training set his cluster; now I want to assign each instance of the testing set to his own cluster using "predict"

Upvotes: 2

Views: 491

Answers (1)

redmode
redmode

Reputation: 4951

Basic idea is:

# clustering function
myclust <- function(x){
  ret <- list(x=x)
  class(ret) <- "mycluster" # your class name
  ret
}

# predict function for your class
predict.mycluster <- function(obj){
  result <- obj$x
  return(result)
}

# clustering
y <- myclust(1:4)
class(y)
# [1] "mycluster"
predict(y)
# [1] 1 2 3 4

Upvotes: 3

Related Questions