last day (15 days later) » 

09:07
1
Q: Pair confusion matrix for more than two cluster algorithms

Sean_TBI_ResearchHow 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 ...

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?
@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.
@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.
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?
@ttnphns Correct
@ttnphns See also last hyperlink.
I see. OK, that will be an entirely programming (not sratistical) question/task much tied with the language and functions you are going to use. The question is how to sift all the many combinations most quickly, an that might depend programming approaches to process arrays. You task is very similar to what TURF analysis in marketing is doing, so try to find some ready solutions on that side. If k is greater than 15 or 17, all combinations may take many minutes/hours.
09:07
@ttnphns I understand. Python is the language. What is the general formula for all combinations for k columns? I should search the web for TURF analysis?
I am not examining the link. But you could send me a copy of the article if you agree.
@ttnphns It's a blog post, it shows the idea behind a Venn diagram for more than three circles.
I think there is no formula. No "magic" formula to sort through all the combinations exist. Technical solutions may differ depending on your language, available matrix/restructuring functions, and your creativity.
Yes I think you should read about TURF analysis and how they implement it.
@ttnphns But first I would need this binary matrix, TURF analysis happens on that binary matrix. How would I create the binary matrix first?
Easy. Let V be the vector of your objects' cluster membership (labels) obtained via a clustering algorithm. Propagate the vector into the square matrix M. Then get the binary matrix B equal to M=M'. That is, transpose M and compare it with M. Vectorize (i.e, unwrap) one of two triangles of B into the column C. So this is one of the columns in your binary matrix.
09:09
Could you elaborate a bit more? Currently my output are vectors with cluster labels: [clustername][labels];[clustername][labels].. note that one algorithm can have more or less groupings than the other. First step is to find all the N(N-1)/2 pairs and then calculate this binary matrix for each column. Do I append the Patient ID's? Or are the labels enough. To summarize, how do I get these pairs?
 
4 hours later…
13:31
Your n objects are the same for all cluster solutions. Consider one cluster solution at a time. Let the vector of the (arbitrary, cluster) labels for the n=8 objects be [2 4 2 1 1 3 4 2]. (You have 4 clusters in this solution.)
Build the square 8x8 matrix from this vector, by stacking it. Then compare that matrix with it transposed. You obtain the square symmetric binary matrix with 1="equal", 0="unequal". Unwrap one (any) of the two triangles (the below of diagonal or the above of it) into a column (its length is n(n-1)/2). That will be the column of your binart matrix. Then go next cluster solution, repeat likewise.
 
3 hours later…
16:39
arr = np.array([2, 4, 2, 1, 1, 3, 4, 2])
bin_nums = ((arr.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)
bin_nums
Compare you mean substract or multiply?
17:03
arr = np.array([2, 4, 2, 1, 1, 3, 4, 2])
bin_nums = ((arr.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)
a = bin_nums
a_T = bin_nums.T

b = a - a_T

c = np.triu(b, k=0)

def unstack(a, axis=0):
return np.moveaxis(a, axis, 0)
# return list(np.moveaxis(a, axis, 0))

unstack(c, axis=1)
But not working yet..
 
3 hours later…
19:59
arr = np.array([2, 4, 2, 1, 1, 3, 4, 2])
bin_nums = ((arr.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)
a = bin_nums
a_T = bin_nums.T

a == a_T

b = b.astype(int)

c = b[np.triu_indices(8)]

c
But has length 36, instead of 28
bin_nums

last day (15 days later) »