2014-09-30 6 views
5

मेरे पास एक (सममित) आसन्नता मैट्रिक्स है, जो समाचार पत्रों में नामों के सह-अवसर (उदाहरण: ग्रेग, मैरी, सैम, टॉम) के आधार पर बनाया गया है (उदाहरण: ए, बी, सी, घ)। निचे देखो।लिफ्ट मूल्य गणना

कैसे गैर शून्य मैट्रिक्स तत्वों (http://en.wikipedia.org/wiki/Lift_(data_mining)) के लिए लिफ्ट मूल्य गणना करने के लिए?

मुझे एक कुशल कार्यान्वयन में दिलचस्पी होगी, जिसका उपयोग बहुत बड़े मैट्रिक्स (उदाहरण के लिए एक लाख गैर-शून्य तत्व) के लिए भी किया जा सकता है।

मैं किसी भी मदद की सराहना करता हूं।

# Load package 
library(Matrix) 

# Data 
A <- new("dgTMatrix" 
    , i = c(2L, 2L, 2L, 0L, 3L, 3L, 3L, 1L, 1L) 
    , j = c(0L, 1L, 2L, 0L, 1L, 2L, 3L, 1L, 3L) 
    , Dim = c(4L, 4L) 
    , Dimnames = list(c("Greg", "Mary", "Sam", "Tom"), c("a", "b", "c", "d")) 
    , x = c(1, 1, 1, 1, 1, 1, 1, 1, 1) 
    , factors = list() 
) 

# > A 
# 4 x 4 sparse Matrix of class "dgTMatrix" 
#  a b c d 
# Greg 1 . . . 
# Mary . 1 . 1 
# Sam 1 1 1 . 
# Tom . 1 1 1 

# One mode projection of the data 
# (i.e. final adjacency matrix, which is the basis for the lift value calculation) 
A.final <- tcrossprod(A) 

# > A.final 
# 4 x 4 sparse Matrix of class "dsCMatrix" 
#  Greg Mary Sam Tom 
# Greg 1 . 1 . 
# Mary . 2 1 2 
# Sam  1 1 3 2 
# Tom  . 2 2 3 

उत्तर

2

यहां कुछ ऐसा है जो आपकी मदद कर सकता है लेकिन निश्चित रूप से सबसे प्रभावी कार्यान्वयन नहीं है।

ComputeLift <- function(data, projection){ 
# Initialize a matrix to store the results. 
lift <- matrix(NA, nrow=nrow(projection), ncol=ncol(projection)) 
# Select all pairs in the projection matrix 
for(i in 1:nrow(projection)){ 
    for(j in 1:ncol(projection)){ 
     # The probability to observe both names in the same article is the 
     # number of articles where the names appear together divided by the 
     # total number of articles 
     pAB <- projection[i,j]/ncol(data) 
     # The probability for a name to appear in an article is the number of 
     # articles where the name appears divided by the total number of articles 
     pA <- sum(A[i,])/ncol(data) 
     pB <- sum(A[j,])/ncol(data) 
     # The lift is computed as the probability to observe both names in an 
     # article divided by the product of the probabilities to observe each name. 
     lift[i,j] <- pAB/(pA*pB) 
    } 
} 
lift 
} 

ComputeLift(data=A, projection=A.final) 
संबंधित मुद्दे