LSH/LSH_ANN.py
def test(self, testDat, testLabs, neighbNum = None):
        
        '''
        Given testing examples and labels, classify every example and report on classification accuracy.
        In LSH_ANN, testing occurs by looping through all the hash tables, producing k inner products of the
        test point and checking to see whether there exist collisions. Collisions indicate R-near neighbors, 
        with probability dependent on the choice of L and k.
        @param testDat: a 2D numpy array which holds the testing data. Patterns in rows, variables in columns.
        @param testLabs: a 1D numpy array, in row correspondence with testDat, that contains the labels of the patterns.
        @param neighbNum (optional): The maximum number of approximate nearest neighbors to consider in the label voting of the example.
                if None (the default), then all neighbors found are considered.
        @return: the accuracy of the classification.
        @raise LogicalError: If the testing labels are not in row correspondence with the testing data.
        @note: The Jan 2008 Communications of the ACM paper of Andoni and Indyk imply that we can also consider votes from duplicates. That is,
        if we find the same R-near neighbor more than once, we should consider his vote as many times as he appears.
        '''
        
        if len(testLabs) != len(testDat):
            raise LogicalError, "Method LSH_ANN.test(): The testing labels are not in row correspondence with the testing examples."
        
        # We need to loop through all hash tables, find the k different quantized projections of the testing point and decide
        # on whether the exact same projection exists within a given hash table. If so, this collision indicates, with high probability,
        # the existence of an approximate nearest neighbor for the testing point. In this case, we store the training point's index
        # and eventually use them all for voting.
        
        correctPreds = 0
        self.randomVotes = 0
        for i in range(len(testDat)):
            neighbors = list()
            for htIndex in range(self.L):
                testPointProjections = np.array([testDat[i].dot(v) for v in self.randomVectorMap[htIndex]])  
                testPointProjections = np.floor((testPointProjections + self.randomBiasMap[htIndex]) / self.w) # TODO: Make into one line after debugging. 
                if tuple(testPointProjections) in self.trainDat[htIndex]:
                    assert len(self.trainDat[htIndex][tuple(testPointProjections)]) > 0
                    if neighbNum is not None: # add all neighbors up to a point
                        neighbors = neighbors + self.trainDat[htIndex][tuple(testPointProjections)][0:neighbNum]
                        neighbNum = neighbNum - len(self.trainDat[htIndex][tuple(testPointProjections)])
                        if neighbNum <= 0:  break       # Finished scanning neighbors for test point
                    else: # add all neighbors
                        neighbors = neighbors + self.trainDat[htIndex][tuple(testPointProjections)]        
            pointLabel = self.__vote__(neighbors)
            if pointLabel == testLabs[i]:
                correctPreds = correctPreds + 1
         
        print "Randomly voted " + str(self.randomVotes) + " times."
        return correctPreds / len(testDat)

        Given testing examples and labels, classify every example and report on classification accuracy.
        In LSH_ANN, testing occurs by looping through all the hash tables, producing k inner products of the
        test point and checking to see whether there exist collisions. Collisions indicate R-near neighbors, 
        with probability dependent on the choice of L and k.
        @param testDat: a 2D numpy array which holds the testing data. Patterns in rows, variables in columns.
        @param testLabs: a 1D numpy array, in row correspondence with testDat, that contains the labels of the patterns.
        @param neighbNum (optional): The maximum number of approximate nearest neighbors to consider in the label voting of the example.
                if None (the default), then all neighbors found are considered.
        @return: the accuracy of the classification.
        @raise LogicalError: If the testing labels are not in row correspondence with the testing data.
        @note: The Jan 2008 Communications of the ACM paper of Andoni and Indyk imply that we can also consider votes from duplicates. That is,
        if we find the same R-near neighbor more than once, we should consider his vote as many times as he appears.
        
Given testing examples and labels, classify every example and report on classification accuracy.
In LSH_ANN, testing occurs by looping through all the hash tables, producing k inner products of the
test point and checking to see whether there exist collisions. Collisions indicate R-near neighbors,
with probability dependent on the choice of L and k.
@param testDat: a 2D numpy array which holds the testing data. Patterns in rows, variables in columns.
@param testLabs: a 1D numpy array, in row correspondence with testDat, that contains the labels of the patterns.
@param neighbNum (optional): The maximum number of approximate nearest neighbors to consider in the label voting of the example.
if None (the default), then all neighbors found are considered.
@return: the accuracy of the classification.
@raise LogicalError: If the testing labels are not in row correspondence with the testing data.
@note: The Jan 2008 Communications of the ACM paper of Andoni and Indyk imply that we can also consider votes from duplicates. That is,
if we find the same R-near neighbor more than once, we should consider his vote as many times as he appears.
Given testing examples and labels classify every example and report on classification accuracy . In LSH_ANN testing occurs by looping through all the hash tables producing k inner products of the test point and checking to see whether there exist collisions . Collisions indicate R - near neighbors with probability dependent on the choice of L and k . @param testDat : a 2D numpy array which holds the testing data . Patterns in rows variables in columns . @param testLabs : a 1D numpy array in row correspondence with testDat that contains the labels of the patterns . @param neighbNum ( optional ) : The maximum number of approximate nearest neighbors to consider in the label voting of the example . if None ( the default ) then all neighbors found are considered . @return : the accuracy of the classification . @raise LogicalError : If the testing labels are not in row correspondence with the testing data . @note : The Jan 2008 Communications of the ACM paper of Andoni and Indyk imply that we can also consider votes from duplicates . That is if we find the same R - near neighbor more than once we should consider his vote as many times as he appears .
def test(self, testDat, testLabs, neighbNum = None):
        if len(testLabs) != len(testDat):
            raise LogicalError, "Method LSH_ANN.test(): The testing labels are not in row correspondence with the testing examples."
        correctPreds = 0
        self.randomVotes = 0
        for i in range(len(testDat)):
            neighbors = list()
            for htIndex in range(self.L):
                testPointProjections = np.array([testDat[i].dot(v) for v in self.randomVectorMap[htIndex]])  
                testPointProjections = np.floor((testPointProjections + self.randomBiasMap[htIndex]) / self.w) 
                if tuple(testPointProjections) in self.trainDat[htIndex]:
                    assert len(self.trainDat[htIndex][tuple(testPointProjections)]) > 0
                    if neighbNum is not None: 
                        neighbors = neighbors + self.trainDat[htIndex][tuple(testPointProjections)][0:neighbNum]
                        neighbNum = neighbNum - len(self.trainDat[htIndex][tuple(testPointProjections)])
                        if neighbNum <= 0:  break       
                    else: 
                        neighbors = neighbors + self.trainDat[htIndex][tuple(testPointProjections)]        
            pointLabel = self.__vote__(neighbors)
            if pointLabel == testLabs[i]:
                correctPreds = correctPreds + 1
        print "Randomly voted " + str(self.randomVotes) + " times."
        return correctPreds / len(testDat)
def test ( self , testDat , testLabs , neighbNum = None ) : if len ( testLabs ) != len ( testDat ) : raise LogicalError , "Method LSH_ANN.test(): The testing labels are not in row correspondence with the testing examples." correctPreds = 0 self . randomVotes = 0 for i in range ( len ( testDat ) ) : neighbors = list ( ) for htIndex in range ( self . L ) : testPointProjections = np . array ( [ testDat [ i ] . dot ( v ) for v in self . randomVectorMap [ htIndex ] ] ) testPointProjections = np . floor ( ( testPointProjections + self . randomBiasMap [ htIndex ] ) / self . w ) if tuple ( testPointProjections ) in self . trainDat [ htIndex ] : assert len ( self . trainDat [ htIndex ] [ tuple ( testPointProjections ) ] ) > 0 if neighbNum is not None : neighbors = neighbors + self . trainDat [ htIndex ] [ tuple ( testPointProjections ) ] [ 0 : neighbNum ] neighbNum = neighbNum - len ( self . trainDat [ htIndex ] [ tuple ( testPointProjections ) ] ) if neighbNum <= 0 : break else : neighbors = neighbors + self . trainDat [ htIndex ] [ tuple ( testPointProjections ) ] pointLabel = self . __vote__ ( neighbors ) if pointLabel == testLabs [ i ] : correctPreds = correctPreds + 1 print "Randomly voted " + str ( self . randomVotes ) + " times." return correctPreds / len ( testDat )
Given testing examples and labels, classify every example and report on classification accuracy.
Given testing examples and labels classify every example and report on classification accuracy .
'''
        Given testing examples and labels, classify every example and report on classification accuracy.
        In LSH_ANN, testing occurs by looping through all the hash tables, producing k inner products of the
        test point and checking to see whether there exist collisions. Collisions indicate R-near neighbors, 
        with probability dependent on the choice of L and k.
        @param testDat: a 2D numpy array which holds the testing data. Patterns in rows, variables in columns.
        @param testLabs: a 1D numpy array, in row correspondence with testDat, that contains the labels of the patterns.
        @param neighbNum (optional): The maximum number of approximate nearest neighbors to consider in the label voting of the example.
                if None (the default), then all neighbors found are considered.
        @return: the accuracy of the classification.
        @raise LogicalError: If the testing labels are not in row correspondence with the testing data.
        @note: The Jan 2008 Communications of the ACM paper of Andoni and Indyk imply that we can also consider votes from duplicates. That is,
        if we find the same R-near neighbor more than once, we should consider his vote as many times as he appears.
        ''' # We need to loop through all hash tables, find the k different quantized projections of the testing point and decide # on whether the exact same projection exists within a given hash table. If so, this collision indicates, with high probability, # the existence of an approximate nearest neighbor for the testing point. In this case, we store the training point's index # and eventually use them all for voting. # TODO: Make into one line after debugging.  # add all neighbors up to a point # Finished scanning neighbors for test point # add all neighbors
self
testDat
testLabs
neighbNum