Ejemplo n.º 1
0
 def yhat(x, w):
     """
     Get predicted labels under the current hypothesis
     :param w: Hypothesis to be evaluated
     :param x: Feature matrix
     :return: List of predicted labels
     """
     return list(map(lambda x_i: PLA.sign(x_i, w), x))
Ejemplo n.º 2
0
def q17(x, y):
    # Question 17: Shuffled input, update step = 0.5, 2000 trials
    print('Question 17: Running PLA on training samples with update step 0.5 and shuffled input ...')
    updates = []
    for i in range(2000):
        updates.append(PLA.pla(x, y, True, 0.5))
    print('Average number of updates for PLA to converge with update step 0.5 and shuffled input {}'
          .format(sum(updates) / len(updates)))
    print()
Ejemplo n.º 3
0
def hw15():

    f = open('./hw1_15_train.dat', 'r')
    data = []
    for line in f:
        l = line.split()
        data.append([[float(i) for i in l[:-1]], int(l[-1])])
    f.close()

    print PLA(data)
Ejemplo n.º 4
0
def hw16():

    f = open('./hw1_15_train.dat', 'r')
    data = []
    for line in f:
        l = line.split()
        data.append([[float(i) for i in l[:-1]], int(l[-1])])
    f.close()

    updates = []

    for i in range(0, 2000):

        w, u = PLA(data, True)
        print '#%d' % (i), w, u
        updates.append(u)

    print 'Avg:', float(sum(updates)) / len(updates)
Ejemplo n.º 5
0
            err_idx = np.where(yhat != y)[0]
            if len(err_idx) == 0:
                break

            # randomly choose a sample where the current weight makes a mistake
            i = err_idx[np.random.permutation(len(err_idx))[0]]
            # adjust weight vector based on the chosen sample
            w += step * y[i, 0] * np.matrix(x[i]).T

            # get errors after weight update
            yhat = Pocket.yhat(x, w)
            new_err = Pocket.err_rate(yhat, y)
            if new_err < curr_err:
                w_pocket = w.copy()
                curr_err = new_err

        return w_pocket, w


# for local testing only
if __name__ == '__main__':
    x_train, y_train = PLA.load_samples('hw1_18_train.dat')
    x_eval, y_eval = PLA.load_samples('hw1_18_test.dat')

    w_pocket, w = Pocket.pocket(x_train, y_train, 1, 50)
    err_rate = Pocket.err_rate(Pocket.yhat(x_eval, w_pocket), y_eval)
    print('OTS error rate for w_pocket: {}'.format(err_rate))



Ejemplo n.º 6
0
def q15(x, y):
    # Question 15: No input shuffling, update step = 1
    print('Question 15: Running PLA on training samples with update step 1 and no shuffling ...')
    print('Number of updates to PLA convergence: {}'.format(PLA.pla(x, y, False, 1)))
    print()