Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a co-occurrence matrix from dummy-coded observations

Tags:

r

Is there a simple approach to converting a data frame with dummies (binary coded) on whether an aspect is present, to a co-occurrence matrix containing the counts of two aspects co-occuring?

E.g. going from this

X <- data.frame(rbind(c(1,0,1,0), c(0,1,1,0), c(0,1,1,1), c(0,0,1,0)))
X
  X1 X2 X3 X4
1  1  0  1  0
2  0  1  1  0
3  0  1  1  1
4  0  0  1  0

to this

   X1 X2 X3 X4
X1  0  0  1  0
X2  0  0  2  1
X3  1  2  0  1
X4  0  1  1  0
like image 275
mhermans Avatar asked May 16 '12 16:05

mhermans


People also ask

How is a co-occurrence matrix made?

7.5A. To produce the co-occurrence matrices for a given value of d, we merely need to calculate the numbers of cases for which pixels a distance d apart have intensity values i and j. Here, we content ourselves with the two cases d=(1, 0) and . We thus obtain the matrices shown in Fig.

What is co-occurrence matrix in NLP?

Unlike the Occurrence matrix which is a rectangular matrix, the co-occurrence matrix is a square matrix where it depicts the co-occurrence of two terms in a context. Thus, the co-occurrence matrix is also sometimes called the term-term matrix.

What is co-occurrence matrix in machine learning?

A co-occurrence matrix or co-occurrence distribution (also referred to as : gray-level co-occurrence matrices GLCMs) is a matrix that is defined over an image to be the distribution of co-occurring pixel values (grayscale values, or colors) at a given offset.


1 Answers

This will do the trick:

X <- as.matrix(X)
out <- crossprod(X)  # Same as: t(X) %*% X
diag(out) <- 0       # (b/c you don't count co-occurrences of an aspect with itself)
out
#      [,1] [,2] [,3] [,4]
# [1,]    0    0    1    0
# [2,]    0    0    2    1
# [3,]    1    2    0    1
# [4,]    0    1    1    0

To get the results into a data.frame exactly like the one you showed, you can then do something like:

nms <- paste("X", 1:4, sep="")
dimnames(out) <- list(nms, nms)
out <- as.data.frame(out)
like image 154
Josh O'Brien Avatar answered Oct 09 '22 12:10

Josh O'Brien