Co-occurrence matrix

The following code was written with the idea of classifying fragments from the relationship between subsequent entries.  It creates a 16×16 matrix for each row of the data, turns that into a row, normalises it and adds a class label.  I used this to make test and training sets on the co-occurrence matrix.  If the data needs to be orientation-invariant (e.g. if it’s a grey level co-occurrence matrix of an image and you want to just take into account when things are “next” to each other and not necessarily “to the right”) then you could add in some extra lines to populate the co-occurrence matrix.  Also, once you have each matrix, you could also create a vector of means or other texture measures (as was done in our work with the GLCM).

R-implementation

# create cooccurence matrix
COM <- array(0,c(nrow(D),16,16))
# populate cooccurrence for each row of the data
for(i in 1:nrow(D)) for(j in 1:(ncol(D)-1))
COM[i,D[i,j]+1,D[i,j+1]+1]<-COM[i,D[i,j]+1,D[i,j+1]+1]+1
# create list for classification
newdata <- array(0,0)
for(i in 1:nrow(D))
newdata <- rbind(newdata,as.vector(COM[i,,]))
# normalise rows
for(i in 1:nrow(newdata)) {tot<-sum(newdata[i,(1:ncol(D1))])
for(j in 1:ncol(newdata)) newdata[i,j]<-newdata[i,j]/tot}
newdata <- cbind(newdata,1)
# write to new file
write.table(newdata,"coms1.txt",row.names=FALSE,col.names=FALSE)

3 responses to “Co-occurrence matrix”

1. Ashutosh Agrahari

Good evening
sir,
I research scholar and doing content analysis. I am trying to create a term to term co-occurrence matrix from Document term matrix using your program but I am not getting any correct result.

• Hi there Ashutosh, i think the co-occurrence algorithms here would only work for numeric data at this stage. Also the idea of co-occurrence here is the coincidence between two documents but rather the differences between adjacent cells of a table.

2. Tharsis

You can compute co-occurrence with ease doing basic matrix operations.
Suppose that a row of data_matrix represents a set and each column represents the occurrence of an element in that set.
To compute the co-occurrence of elements in all sets, one simply does:

co_occurrence <- t(data_matrix) %*% data_matrix