LSH/LSH_ANN.py
def train(self, trainDat, trainLabs):
        
        '''        
         Trains the LSH classifier. This involves randomly generating L*K D-dimensional vectors whose components 
         are drawn i.i.d from a Gaussian and initializing the hash tables. Every hash table will be associated with
         a different set of K such vectors, yielding K different inner products (random proections on the real number line).
         This is arguably the most complex method of this class.
         
         @param trainDat: a 2D numpy array holding our training data. Patterns in rows, features in columns.
         @param trainLabs: a 1D numpy array, in row correspondence with trainDat, holding one label per pattern.
         @raise LogicalError if the training labels are not in row correspondence with training data.
        '''
        
        if len(trainDat) != len(trainLabs):
            raise LogicalError, "Method LSH_ANN.train(): The training labels were not in row correspondence with the training data."
         
        self.trainLabs = trainLabs
        self.trainDat = list()     
        self.randomVectorMap = dict()
        self.randomBiasMap = dict()
        
        # For every hash table, generate k random D-dimensional vectors, whose components are drawn i.i.d from a Gaussian distribution.
        # We associate each of these vectors with the relevant hash table by storing the association in the "randomVectorMap" dictionary.
        # TODO: In order to make the experiments re-producible, the random number seed needs to be initialized to a static number.
        
        for hashInd in range(self.L):
            b = np.random.uniform(0, self.w)
            randomVecs = np.array([np.random.normal(0, 1, len(trainDat[0])) for _ in range(self.k)]) # k random D-dimensional vectors
            self.randomVectorMap[hashInd] = randomVecs
            self.randomBiasMap[hashInd] = b
            
            # Multiplying the k X D "randomVecs" matrix by our training data transposed (to make it D x N)
            # yields a k X N matrix which describes every point by k random projections onto R1.
            # We then transpose the result to make it N x k.
            projMat = np.array((randomVecs.dot(trainDat.T))).T
            
            # We now quantify every projection into buckets according to  the formula (u.v + b)/w
            projMat = np.array([np.floor((row + b)/self.w) for row in projMat])
            
            # Now, every example can be stored in the "hashInd"'th hash table s.t 
            # its k-dimensional projection is the key and its index is the mapped value
            
            currentDict = dict()
            for exIndex in range(len(projMat)):
                if tuple(projMat[exIndex]) not in currentDict:
                    currentDict[tuple(projMat[exIndex])] = list()
                currentDict[tuple(projMat[exIndex])].append(exIndex)
            self.trainDat.append(currentDict)
        
         Trains the LSH classifier. This involves randomly generating L*K D-dimensional vectors whose components 
         are drawn i.i.d from a Gaussian and initializing the hash tables. Every hash table will be associated with
         a different set of K such vectors, yielding K different inner products (random proections on the real number line).
         This is arguably the most complex method of this class.
         
         @param trainDat: a 2D numpy array holding our training data. Patterns in rows, features in columns.
         @param trainLabs: a 1D numpy array, in row correspondence with trainDat, holding one label per pattern.
         @raise LogicalError if the training labels are not in row correspondence with training data.
        
Trains the LSH classifier. This involves randomly generating L*K D-dimensional vectors whose components
are drawn i.i.d from a Gaussian and initializing the hash tables. Every hash table will be associated with
a different set of K such vectors, yielding K different inner products (random proections on the real number line).
This is arguably the most complex method of this class.

@param trainDat: a 2D numpy array holding our training data. Patterns in rows, features in columns.
@param trainLabs: a 1D numpy array, in row correspondence with trainDat, holding one label per pattern.
@raise LogicalError if the training labels are not in row correspondence with training data.
Trains the LSH classifier . This involves randomly generating L * K D - dimensional vectors whose components are drawn i . i . d from a Gaussian and initializing the hash tables . Every hash table will be associated with a different set of K such vectors yielding K different inner products ( random proections on the real number line ) . This is arguably the most complex method of this class . @param trainDat : a 2D numpy array holding our training data . Patterns in rows features in columns . @param trainLabs : a 1D numpy array in row correspondence with trainDat holding one label per pattern . @raise LogicalError if the training labels are not in row correspondence with training data .
def train(self, trainDat, trainLabs):
        if len(trainDat) != len(trainLabs):
            raise LogicalError, "Method LSH_ANN.train(): The training labels were not in row correspondence with the training data."
        self.trainLabs = trainLabs
        self.trainDat = list()     
        self.randomVectorMap = dict()
        self.randomBiasMap = dict()
        for hashInd in range(self.L):
            b = np.random.uniform(0, self.w)
            randomVecs = np.array([np.random.normal(0, 1, len(trainDat[0])) for _ in range(self.k)]) 
            self.randomVectorMap[hashInd] = randomVecs
            self.randomBiasMap[hashInd] = b
            projMat = np.array((randomVecs.dot(trainDat.T))).T
            projMat = np.array([np.floor((row + b)/self.w) for row in projMat])
            currentDict = dict()
            for exIndex in range(len(projMat)):
                if tuple(projMat[exIndex]) not in currentDict:
                    currentDict[tuple(projMat[exIndex])] = list()
                currentDict[tuple(projMat[exIndex])].append(exIndex)
            self.trainDat.append(currentDict)
def train ( self , trainDat , trainLabs ) : if len ( trainDat ) != len ( trainLabs ) : raise LogicalError , "Method LSH_ANN.train(): The training labels were not in row correspondence with the training data." self . trainLabs = trainLabs self . trainDat = list ( ) self . randomVectorMap = dict ( ) self . randomBiasMap = dict ( ) for hashInd in range ( self . L ) : b = np . random . uniform ( 0 , self . w ) randomVecs = np . array ( [ np . random . normal ( 0 , 1 , len ( trainDat [ 0 ] ) ) for _ in range ( self . k ) ] ) self . randomVectorMap [ hashInd ] = randomVecs self . randomBiasMap [ hashInd ] = b projMat = np . array ( ( randomVecs . dot ( trainDat . T ) ) ) . T projMat = np . array ( [ np . floor ( ( row + b ) / self . w ) for row in projMat ] ) currentDict = dict ( ) for exIndex in range ( len ( projMat ) ) : if tuple ( projMat [ exIndex ] ) not in currentDict : currentDict [ tuple ( projMat [ exIndex ] ) ] = list ( ) currentDict [ tuple ( projMat [ exIndex ] ) ] . append ( exIndex ) self . trainDat . append ( currentDict )
Trains the LSH classifier.
Trains the LSH classifier .
'''        
         Trains the LSH classifier. This involves randomly generating L*K D-dimensional vectors whose components 
         are drawn i.i.d from a Gaussian and initializing the hash tables. Every hash table will be associated with
         a different set of K such vectors, yielding K different inner products (random proections on the real number line).
         This is arguably the most complex method of this class.
         
         @param trainDat: a 2D numpy array holding our training data. Patterns in rows, features in columns.
         @param trainLabs: a 1D numpy array, in row correspondence with trainDat, holding one label per pattern.
         @raise LogicalError if the training labels are not in row correspondence with training data.
        ''' # For every hash table, generate k random D-dimensional vectors, whose components are drawn i.i.d from a Gaussian distribution. # We associate each of these vectors with the relevant hash table by storing the association in the "randomVectorMap" dictionary. # TODO: In order to make the experiments re-producible, the random number seed needs to be initialized to a static number. # k random D-dimensional vectors # Multiplying the k X D "randomVecs" matrix by our training data transposed (to make it D x N) # yields a k X N matrix which describes every point by k random projections onto R1. # We then transpose the result to make it N x k. # We now quantify every projection into buckets according to  the formula (u.v + b)/w # Now, every example can be stored in the "hashInd"'th hash table s.t  # its k-dimensional projection is the key and its index is the mapped value
self
trainDat
trainLabs