Skip to content
Advertisement

Constructing a co-occurrence matrix in python pandas

I know how to do this in R. But, is there any function in pandas that transforms a dataframe to an nxn co-occurrence matrix containing the counts of two aspects co-occurring.

For example a matrix df:

JavaScript

would yield:

JavaScript

Since the matrix is mirrored on the diagonal I guess there would be a way to optimize code.

Advertisement

Answer

It’s a simple linear algebra, you multiply matrix with its transpose (your example contains strings, don’t forget to convert them to integer):

JavaScript

if, as in R answer, you want to reset diagonal, you can use numpy’s fill_diagonal:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement