LSH/ANN.py
def test(self, testData, testLabels, L):
        '''
        Testing on a nearest neighbor classifier involves finding L R-nearest neighbors 
        of every query point. Alternatively, all R-nearest neighbors could be found.
        @param testData a 2D ndarray representing the testing data
        @param testLabels a 1D ndarray representing the testing labels: one cell per testData row
        @param L the number of approximate nearest neighbors (optional)
        @raise LogicalError if there are not exactly as many labels as there are examples
        @return the accuracy reported on the testData
        '''
        
        if len(testData) != len(testLabels):
            raise LogicalError, "Method ANN.test(): There needs to be one label per testing example"
        if L is None:
            L = self.L          # Use stored hyper-parameter if not provided with one.
                                # Note that L could still be None, in which case we do not
                                # want to stop after finding L approximate nearest neighbors.
        appNeighbors = dict()
        correctPreds = 0
        for i in range(len(testData)): 
            appNeighbors[i] = list()
            for j in range(len(self.trainDat)):
                if l2norm(testData[i]-self.trainDat[j]) <= self.R:      
                    appNeighbors[i].append(j)
                    if L is not None: L = L - 1 # Found an approximate neighbor
                    if L == 0: break # This is exactly where the computational advantage of having at most L approx. nearest neighbors is introduced
            votedLabel = self.__vote__(appNeighbors[i])
            if votedLabel == testLabels[i]:
                correctPreds = correctPreds + 1
        
        if self.printAloneMsg == True:    
            print "For radius = " + str(self.R) + ", points found alone: " + str(self.numAlonePoints)
        self.numAlonePoints = 0                 # reset counter
        return correctPreds / len(testData)

        Testing on a nearest neighbor classifier involves finding L R-nearest neighbors 
        of every query point. Alternatively, all R-nearest neighbors could be found.
        @param testData a 2D ndarray representing the testing data
        @param testLabels a 1D ndarray representing the testing labels: one cell per testData row
        @param L the number of approximate nearest neighbors (optional)
        @raise LogicalError if there are not exactly as many labels as there are examples
        @return the accuracy reported on the testData
        
Testing on a nearest neighbor classifier involves finding L R-nearest neighbors
of every query point. Alternatively, all R-nearest neighbors could be found.
Testing on a nearest neighbor classifier involves finding L R - nearest neighbors of every query point . Alternatively all R - nearest neighbors could be found .
def test(self, testData, testLabels, L):
        if len(testData) != len(testLabels):
            raise LogicalError, "Method ANN.test(): There needs to be one label per testing example"
        if L is None:
            L = self.L          
        appNeighbors = dict()
        correctPreds = 0
        for i in range(len(testData)): 
            appNeighbors[i] = list()
            for j in range(len(self.trainDat)):
                if l2norm(testData[i]-self.trainDat[j]) <= self.R:      
                    appNeighbors[i].append(j)
                    if L is not None: L = L - 1 
                    if L == 0: break 
            votedLabel = self.__vote__(appNeighbors[i])
            if votedLabel == testLabels[i]:
                correctPreds = correctPreds + 1
        if self.printAloneMsg == True:    
            print "For radius = " + str(self.R) + ", points found alone: " + str(self.numAlonePoints)
        self.numAlonePoints = 0                 
        return correctPreds / len(testData)
def test ( self , testData , testLabels , L ) : if len ( testData ) != len ( testLabels ) : raise LogicalError , "Method ANN.test(): There needs to be one label per testing example" if L is None : L = self . L appNeighbors = dict ( ) correctPreds = 0 for i in range ( len ( testData ) ) : appNeighbors [ i ] = list ( ) for j in range ( len ( self . trainDat ) ) : if l2norm ( testData [ i ] - self . trainDat [ j ] ) <= self . R : appNeighbors [ i ] . append ( j ) if L is not None : L = L - 1 if L == 0 : break votedLabel = self . __vote__ ( appNeighbors [ i ] ) if votedLabel == testLabels [ i ] : correctPreds = correctPreds + 1 if self . printAloneMsg == True : print "For radius = " + str ( self . R ) + ", points found alone: " + str ( self . numAlonePoints ) self . numAlonePoints = 0 return correctPreds / len ( testData )
Testing on a nearest neighbor classifier involves finding L R-nearest neighbors
of every query point.
Testing on a nearest neighbor classifier involves finding L R - nearest neighbors of every query point .
'''
        Testing on a nearest neighbor classifier involves finding L R-nearest neighbors 
        of every query point. Alternatively, all R-nearest neighbors could be found.
        @param testData a 2D ndarray representing the testing data
        @param testLabels a 1D ndarray representing the testing labels: one cell per testData row
        @param L the number of approximate nearest neighbors (optional)
        @raise LogicalError if there are not exactly as many labels as there are examples
        @return the accuracy reported on the testData
        ''' # Use stored hyper-parameter if not provided with one. # Note that L could still be None, in which case we do not # want to stop after finding L approximate nearest neighbors. # Found an approximate neighbor # This is exactly where the computational advantage of having at most L approx. nearest neighbors is introduced # reset counter
self
testData
testLabels
L