def testReciprocalRankAtk(self):
        m = 20
        n = 50
        r = 3
        X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m, n),
                                                                r,
                                                                0.5,
                                                                verbose=True,
                                                                csarray=True)

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

        (indPtr, colInds) = X.nonzeroRowsPtr()
        indPtr = numpy.array(indPtr, numpy.uint32)
        colInds = numpy.array(colInds, numpy.uint32)
        rrs = MCEvaluatorCython.reciprocalRankAtk(indPtr, colInds,
                                                  orderedItems)

        rrs2 = numpy.zeros(m)
        for i in range(m):
            omegai = colInds[indPtr[i]:indPtr[i + 1]]
            for j in range(k):
                if orderedItems[i, j] in omegai:
                    rrs2[i] = 1 / float(1 + j)
                    break

        nptst.assert_array_equal(rrs, rrs2)

        #Test case where no items are in ranking
        orderedItems = numpy.ones((m, k), numpy.int32) * (n + 1)
        rrs = MCEvaluatorCython.reciprocalRankAtk(indPtr, colInds,
                                                  orderedItems)
        nptst.assert_array_equal(rrs, numpy.zeros(m))

        #Now, make all items rank 2
        for i in range(m):
            omegai = colInds[indPtr[i]:indPtr[i + 1]]
            orderedItems[i, 1] = omegai[0]

        rrs = MCEvaluatorCython.reciprocalRankAtk(indPtr, colInds,
                                                  orderedItems)
        nptst.assert_array_equal(rrs, numpy.ones(m) * 0.5)
 def testReciprocalRankAtk(self): 
     m = 20 
     n = 50 
     r = 3 
     X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m,n), r, 0.5, verbose=True, csarray=True)
     
     k = 5
     orderedItems = numpy.random.randint(0, n, m*k)
     orderedItems = numpy.reshape(orderedItems, (m, k))
     orderedItems = numpy.array(orderedItems, numpy.int32)
     
     (indPtr, colInds) = X.nonzeroRowsPtr()
     indPtr = numpy.array(indPtr, numpy.uint32)
     colInds = numpy.array(colInds, numpy.uint32)
     rrs = MCEvaluatorCython.reciprocalRankAtk(indPtr, colInds, orderedItems)
     
     rrs2 = numpy.zeros(m)
     for i in range(m): 
         omegai = colInds[indPtr[i]:indPtr[i+1]]
         for j in range(k): 
             if orderedItems[i, j] in omegai: 
                 rrs2[i] = 1/float(1+j)
                 break 
     
     nptst.assert_array_equal(rrs, rrs2)
     
     #Test case where no items are in ranking 
     orderedItems = numpy.ones((m, k), numpy.int32) * (n+1)
     rrs = MCEvaluatorCython.reciprocalRankAtk(indPtr, colInds, orderedItems)
     nptst.assert_array_equal(rrs, numpy.zeros(m))
     
     #Now, make all items rank 2
     for i in range(m): 
         omegai = colInds[indPtr[i]:indPtr[i+1]]
         orderedItems[i, 1] = omegai[0]
     
     rrs = MCEvaluatorCython.reciprocalRankAtk(indPtr, colInds, orderedItems)
     nptst.assert_array_equal(rrs, numpy.ones(m)*0.5)     
Example #3
0
 def mrrAtK(positiveArray, orderedItems, k, verbose=False): 
     """
     Compute the mean reciprocal rank@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 mrr and first k recommendation for each row, otherwise just mrr
     """
     if type(positiveArray) != tuple: 
         positiveArray = SparseUtils.getOmegaListPtr(positiveArray)        
     
     orderedItems = orderedItems[:, 0:k]
     indPtr, colInds = positiveArray
     mrr = MCEvaluatorCython.reciprocalRankAtk(indPtr, colInds, orderedItems)
     
     if verbose: 
         return mrr, orderedItems
     else: 
         return mrr.mean()
Example #4
0
    def mrrAtK(positiveArray, orderedItems, k, verbose=False):
        """
        Compute the mean reciprocal rank@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 mrr and first k recommendation for each row, otherwise just mrr
        """
        if type(positiveArray) != tuple:
            positiveArray = SparseUtils.getOmegaListPtr(positiveArray)

        orderedItems = orderedItems[:, 0:k]
        indPtr, colInds = positiveArray
        mrr = MCEvaluatorCython.reciprocalRankAtk(indPtr, colInds,
                                                  orderedItems)

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