Skip to content

Instantly share code, notes, and snippets.

@Syncrossus
Last active September 5, 2019 08:42
Show Gist options
  • Save Syncrossus/0f07f27cbf08ca897c3e5b151b07c1b7 to your computer and use it in GitHub Desktop.
Save Syncrossus/0f07f27cbf08ca897c3e5b151b07c1b7 to your computer and use it in GitHub Desktop.
Cluster relabeling function (useful for confusion tables with clusters labeled with k-means for example). runs in complexity O(n^2*m^2) with n the length of labels1 and m the number of clusters.
# arguments :
# labels1 : list of labels corresponding to the wrong clusters
# labels2 : list of labels corresponding to the right clusters
# return :
# labels1 relabeled
relabel <- function(labels1, labels2){
newlabels <- labels1
levels1 <- levels(as.factor(labels1))
levels2 <- levels(as.factor(labels2))
for(i in levels1){
max_i_eq_j <- c(0,0)
for(j in levels2){
n_i_eq_j <- length(which(which(labels1==i) %in% which(labels2==j)))
if(n_i_eq_j>as.numeric(max_i_eq_j[1])){
max_i_eq_j[1]<-n_i_eq_j
max_i_eq_j[2]<-j
}
}
newlabels[which(labels1==i)] <- max_i_eq_j[2]
}
return(newlabels)
}
@Syncrossus
Copy link
Author

This code is released under the WTFPL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment