LSH/classification_utils.py
def cross_validate(data, k, hyperparams, labels):
    '''
    Take a bunch of data, run k-fold cross validation on it
    and then return the maximum accuracy score. 
    @param data 2D numpy array holding the full array of data
    @param k the number of folds
    @param hyperparams a list containing the different hyper-parameters to test
    @param labels an ndarray of data labels, one per row of the "data" ndarray
    @return a tuple (bestHyperParam, bestAcc) representing the optimal amount of neighbors as well as 
                        the maximum accuracy scored over all the folds
    '''
    bestAccuracy = 0.0
    optimalHyperParam = None
    for hyperparam in hyperparams: # choose optimal hyper-parameter
        accuracies = list() 
        for fold in range(k): # fold = [0, 1,...,k-1]
            trainDat = [data[i] for i in range(len(data)) if i%k != fold] 
            trainLabs = [labels[i] for i in range(len(data)) if i%k != fold]
            testDat = [data[i] for i in range(len(data)) if i%k == fold] # test every k-th example
            testLabs = [labels[i] for i in range(len(data)) if i%k == fold] 
            model = train(trainDat, trainLabs, hyperparam)
            accuracies.append(test(model, testDat, testLabs))
        accuracy = np.mean(accuracies)
        if accuracy > bestAccuracy:         # store the data
            bestAccuracy = accuracy
            optimalHyperParam = hyperparam
    
    return (optimalHyperParam, bestAccuracy )

    Take a bunch of data, run k-fold cross validation on it
    and then return the maximum accuracy score. 
    @param data 2D numpy array holding the full array of data
    @param k the number of folds
    @param hyperparams a list containing the different hyper-parameters to test
    @param labels an ndarray of data labels, one per row of the "data" ndarray
    @return a tuple (bestHyperParam, bestAcc) representing the optimal amount of neighbors as well as 
                        the maximum accuracy scored over all the folds
    
Take a bunch of data, run k-fold cross validation on it
and then return the maximum accuracy score.
Take a bunch of data run k - fold cross validation on it and then return the maximum accuracy score .
def cross_validate(data, k, hyperparams, labels):
    bestAccuracy = 0.0
    optimalHyperParam = None
    for hyperparam in hyperparams: 
        accuracies = list() 
        for fold in range(k): 
            trainDat = [data[i] for i in range(len(data)) if i%k != fold] 
            trainLabs = [labels[i] for i in range(len(data)) if i%k != fold]
            testDat = [data[i] for i in range(len(data)) if i%k == fold] 
            testLabs = [labels[i] for i in range(len(data)) if i%k == fold] 
            model = train(trainDat, trainLabs, hyperparam)
            accuracies.append(test(model, testDat, testLabs))
        accuracy = np.mean(accuracies)
        if accuracy > bestAccuracy:         
            bestAccuracy = accuracy
            optimalHyperParam = hyperparam
    return (optimalHyperParam, bestAccuracy )
def cross_validate ( data , k , hyperparams , labels ) : bestAccuracy = 0.0 optimalHyperParam = None for hyperparam in hyperparams : accuracies = list ( ) for fold in range ( k ) : trainDat = [ data [ i ] for i in range ( len ( data ) ) if i % k != fold ] trainLabs = [ labels [ i ] for i in range ( len ( data ) ) if i % k != fold ] testDat = [ data [ i ] for i in range ( len ( data ) ) if i % k == fold ] testLabs = [ labels [ i ] for i in range ( len ( data ) ) if i % k == fold ] model = train ( trainDat , trainLabs , hyperparam ) accuracies . append ( test ( model , testDat , testLabs ) ) accuracy = np . mean ( accuracies ) if accuracy > bestAccuracy : bestAccuracy = accuracy optimalHyperParam = hyperparam return ( optimalHyperParam , bestAccuracy )
Take a bunch of data, run k-fold cross validation on it
and then return the maximum accuracy score.
Take a bunch of data run k - fold cross validation on it and then return the maximum accuracy score .
'''
    Take a bunch of data, run k-fold cross validation on it
    and then return the maximum accuracy score. 
    @param data 2D numpy array holding the full array of data
    @param k the number of folds
    @param hyperparams a list containing the different hyper-parameters to test
    @param labels an ndarray of data labels, one per row of the "data" ndarray
    @return a tuple (bestHyperParam, bestAcc) representing the optimal amount of neighbors as well as 
                        the maximum accuracy scored over all the folds
    ''' # choose optimal hyper-parameter # fold = [0, 1,...,k-1] # test every k-th example # store the data
data
k
hyperparams
labels