#' Initialize responisibility matrix
#'
#' This function allows you to do variational Bayesian inference for Gaussian mixture. It is based on the MatLab code by Mo Chen (sth4nth@gmail.com).
#' @param X NxD data matrix
#' @param K Number of mixture components
#' @param labels_init Initial cluster labels (if empty, use either k-means or randomly assigned labels)
#' @param kmeans_init Boolean flag, which, if TRUE initializes the responsibilities with the output of the k-means algorithm
#' @param verbose Boolean flag which, if TRUE, prints the type of initialization
#' @return Resp NxK matrix of responsibilities
#' @export
#'
init = function(X, K, labels_init, kmeans_init = F, verbose = T){
if(is.vector(X)){
N = length(X)
}else{
N = dim(X)[1]
}
if(!missing(labels_init)){
if(verbose) message(sprintf("Initialization of responsibilities: user-defined labels. \n"))
label = labels_init # assign some user-defined labels
}else if(kmeans_init){
if(verbose) message(sprintf("Initialization of responsibilities: k-means. \n"))
km = stats::kmeans(X, K, iter.max = 100)
label = km$cluster # initial labels correspond to the output of the k-means algorithm
}else{
if(verbose) message(sprintf("Initialization of responsibilities: random assignment to mixture components.\n"))
label = sample(1:K, N, replace=T) # assign random labels
}
Resp = as.matrix(Matrix::sparseMatrix(i = c(1:N), j = label, x = 1)) # create responsibility matrix
Resp
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.