コード例 #1
0
ファイル: MCEvaluator.py プロジェクト: charanpald/sandbox
 def f1AtK(positiveArray, orderedItems, k, verbose=False): 
     """
     Return the F1@k measure for each row of the predicted matrix UV.T 
     using real values in positiveArray. positiveArray is a tuple (indPtr, colInds)
     
     :param orderedItems: The ordered items for each user (users are rows, items are cols)  
     
     :param verbose: If true return recall and first k recommendation for each row, otherwise just precisions
     """
     if type(positiveArray) != tuple: 
         positiveArray = SparseUtils.getOmegaListPtr(positiveArray)        
     
     orderedItems = orderedItems[:, 0:k]
     indPtr, colInds = positiveArray
     
     precisions = MCEvaluatorCython.precisionAtk(indPtr, colInds, orderedItems)
     recalls = MCEvaluatorCython.recallAtk(indPtr, colInds, orderedItems)
     
     denominator = precisions+recalls
     denominator += denominator == 0      
     
     f1s = 2*precisions*recalls/denominator
     
     if verbose: 
         return f1s, orderedItems
     else: 
         return f1s.mean()
コード例 #2
0
ファイル: MCEvaluator.py プロジェクト: rezaarmand/sandbox
    def f1AtK(positiveArray, orderedItems, k, verbose=False):
        """
        Return the F1@k measure for each row of the predicted matrix UV.T 
        using real values in positiveArray. positiveArray is a tuple (indPtr, colInds)
        
        :param orderedItems: The ordered items for each user (users are rows, items are cols)  
        
        :param verbose: If true return recall and first k recommendation for each row, otherwise just precisions
        """
        if type(positiveArray) != tuple:
            positiveArray = SparseUtils.getOmegaListPtr(positiveArray)

        orderedItems = orderedItems[:, 0:k]
        indPtr, colInds = positiveArray

        precisions = MCEvaluatorCython.precisionAtk(indPtr, colInds,
                                                    orderedItems)
        recalls = MCEvaluatorCython.recallAtk(indPtr, colInds, orderedItems)

        denominator = precisions + recalls
        denominator += denominator == 0

        f1s = 2 * precisions * recalls / denominator

        if verbose:
            return f1s, orderedItems
        else:
            return f1s.mean()
コード例 #3
0
    def testStratifiedRecallAtk(self):
        m = 20
        n = 50
        r = 3
        alpha = 1

        X, U, V = SparseUtilsCython.generateSparseBinaryMatrixPL((m, n),
                                                                 r,
                                                                 density=0.2,
                                                                 alpha=alpha,
                                                                 csarray=True)

        itemCounts = numpy.array(X.sum(0) + 1, numpy.int32)

        (indPtr, colInds) = X.nonzeroRowsPtr()

        indPtr = numpy.array(indPtr, numpy.uint32)
        colInds = numpy.array(colInds, numpy.uint32)

        k = 5
        orderedItems = numpy.random.randint(0, n, m * k)
        orderedItems = numpy.reshape(orderedItems, (m, k))
        orderedItems = numpy.array(orderedItems, numpy.int32)
        beta = 0.5

        recalls, denominators = MCEvaluatorCython.stratifiedRecallAtk(
            indPtr, colInds, orderedItems, itemCounts, beta)

        recalls2 = numpy.zeros(m)

        #Now compute recalls from scratch
        for i in range(m):
            omegai = colInds[indPtr[i]:indPtr[i + 1]]

            numerator = 0
            for j in range(k):
                if orderedItems[i, j] in omegai:
                    numerator += 1 / itemCounts[orderedItems[i, j]]**beta

            denominator = 0

            for j in omegai:
                denominator += 1 / itemCounts[j]**beta

            recalls2[i] = numerator / denominator

        nptst.assert_array_equal(recalls, recalls2)

        #Now try to match with normal recall
        itemCounts = numpy.ones(n, numpy.int32)
        recalls, denominators = MCEvaluatorCython.stratifiedRecallAtk(
            indPtr, colInds, orderedItems, itemCounts, beta)
        recalls2 = MCEvaluatorCython.recallAtk(indPtr, colInds, orderedItems)

        nptst.assert_array_equal(recalls, recalls2)
コード例 #4
0
    def testStratifiedRecallAtk(self): 
        m = 20 
        n = 50 
        r = 3     
        alpha = 1
        
        X, U, V = SparseUtilsCython.generateSparseBinaryMatrixPL((m,n), r, density=0.2, alpha=alpha, csarray=True)
        
        itemCounts = numpy.array(X.sum(0)+1, numpy.int32) 
        
        (indPtr, colInds) = X.nonzeroRowsPtr()
        
        indPtr = numpy.array(indPtr, numpy.uint32)
        colInds = numpy.array(colInds, numpy.uint32)
        
        k = 5
        orderedItems = numpy.random.randint(0, n, m*k)
        orderedItems = numpy.reshape(orderedItems, (m, k))
        orderedItems = numpy.array(orderedItems, numpy.int32)        
        beta = 0.5
        
        recalls, denominators = MCEvaluatorCython.stratifiedRecallAtk(indPtr, colInds, orderedItems, itemCounts, beta)
        
        
        recalls2 = numpy.zeros(m)        
            
        #Now compute recalls from scratch 
        for i in range(m):
            omegai = colInds[indPtr[i]:indPtr[i+1]]            
            
            numerator = 0 
            for j in range(k):
                if orderedItems[i, j] in omegai: 
                    numerator += 1/itemCounts[orderedItems[i, j]]**beta
            
            denominator = 0

            for j in omegai: 
                denominator += 1/itemCounts[j]**beta
                
            recalls2[i] = numerator/denominator
            
        nptst.assert_array_equal(recalls, recalls2)
                                
                
        #Now try to match with normal recall 
        itemCounts = numpy.ones(n, numpy.int32)
        recalls, denominators = MCEvaluatorCython.stratifiedRecallAtk(indPtr, colInds, orderedItems, itemCounts, beta)
        recalls2 = MCEvaluatorCython.recallAtk(indPtr, colInds, orderedItems)
        
        nptst.assert_array_equal(recalls, recalls2)
コード例 #5
0
ファイル: MCEvaluator.py プロジェクト: charanpald/sandbox
 def recallAtK(positiveArray, orderedItems, k, verbose=False): 
     """
     Compute the average recall@k score for each row of the predicted matrix UV.T 
     using real values in positiveArray. positiveArray is a tuple (indPtr, colInds)
     
     :param orderedItems: The ordered items for each user (users are rows, items are cols)  
     
     :param verbose: If true return recall and first k recommendation for each row, otherwise just precisions
     """
     if type(positiveArray) != tuple: 
         positiveArray = SparseUtils.getOmegaListPtr(positiveArray)        
     
     orderedItems = orderedItems[:, 0:k]
     indPtr, colInds = positiveArray
     recalls = MCEvaluatorCython.recallAtk(indPtr, colInds, orderedItems)
     
     if verbose: 
         return recalls, orderedItems
     else: 
         return recalls.mean()
コード例 #6
0
ファイル: MCEvaluator.py プロジェクト: rezaarmand/sandbox
    def recallAtK(positiveArray, orderedItems, k, verbose=False):
        """
        Compute the average recall@k score for each row of the predicted matrix UV.T 
        using real values in positiveArray. positiveArray is a tuple (indPtr, colInds)
        
        :param orderedItems: The ordered items for each user (users are rows, items are cols)  
        
        :param verbose: If true return recall and first k recommendation for each row, otherwise just precisions
        """
        if type(positiveArray) != tuple:
            positiveArray = SparseUtils.getOmegaListPtr(positiveArray)

        orderedItems = orderedItems[:, 0:k]
        indPtr, colInds = positiveArray
        recalls = MCEvaluatorCython.recallAtk(indPtr, colInds, orderedItems)

        if verbose:
            return recalls, orderedItems
        else:
            return recalls.mean()