# Pair confusion matrix for more than two cluster algorithms

How would one code the following issue:

The columns represent the k groupings from the k cluster algorithms. The rows are the N(N-1)/2 pairs of data points. If an algorithm groups a pair together (i,j) = 1 and 0 otherwise. This results in a binary matrix of size N(N-1)/2 x k. Agreement occurs if algorithms both group a pair of data points together.

How would I calculate, i.e. program, this number for all k combinations of algorithms?

a in this case represents the pairs of data points grouped together by algorithms 1, 2, 3, ..., k or any other combination of algorithms, for example, 2, 3 and k or 1, 2, 3 and 4. This will allow us to make an UpSet plot:

https://en.wikipedia.org/wiki/UpSet_Plot#:~:text=UpSet%20plots%20are%20a%20data,sets%20(or%20vice%20versa)

and

https://kieranhealy.org/blog/archives/2020/04/16/upset-plots/

• Comembership confusion matrix - which count is a pair of objects - is a relation between two partitions, a 2x2 frequency table. And I could not understand your setting since I don't see a relation between partitions (columns of you data). Will you explain, in your question, in greater detail what you want to do? Dec 6, 2022 at 19:41
• @ttnphns a in this case represents the pairs of data points grouped together by algorithms 1, 2, 3, ..., k or any other combination of algorithms, for example, 2, 3 and k or 1, 2, 3 and 4. This will allow us to make an upSet plot. Dec 6, 2022 at 19:48
• @ttnphns Hence, we compare more than two partitions. The binary matrix is preliminary to the frequency table from which we infer a. We would continue doing this until we looped over all algorithm combinations: for k = 3 we have four values for a. Dec 6, 2022 at 20:02
• So, you extract some combination of columns from your binary matrix and you want to know how many rows in this extracted submatrix is full of 1s. Right? Dec 6, 2022 at 20:02
• @ttnphns Correct Dec 6, 2022 at 20:04

cluster=c(2,1,2,0,0,3)                 # kmedioids
cluster=cbind(cluster,c(0,0,0,0,0,1))  # spectral
n=nrow(cluster)
N=n*(n-1)/2                            # aantal paren
A=matrix(NA,N,2)
for (k in 1:ncol(cluster)){
rowcounter=1
for (i in 1:(n-1)){
for (j in (i+1):n){
A[rowcounter,k]=cluster[i,k]==cluster[j,k]
rowcounter=rowcounter+1
}
}
}


But unfeasible for Python! Re-write appreciated

• Some more commenting in the code, or comments outside the code would make this answer considerably more useful. Dec 7, 2022 at 18:14