Beispiel #1
0
 def score_pca(k, p = 0.5, seed = 1):
     
     Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p, seed)
     mu = ps6.get_mean_face(Xtrain)
     eig_vecs, eig_vals = ps6.pca(Xtrain, k)
     Xtrain_proj = np.dot(Xtrain - mu, eig_vecs)
 
     # testing
     mu = ps6.get_mean_face(Xtest)
     Xtest_proj = np.dot(Xtest - mu, eig_vecs)
 
     good = 0
     bad = 0
 
     for i, obs in enumerate(Xtest_proj):
 
         dist = [np.linalg.norm(obs - x) for x in Xtrain_proj]
 
         idx = np.argmin(dist)
         y_pred = ytrain[idx]
 
         if y_pred == ytest[i]:
             good += 1
         else:
             bad += 1
     return good, bad
Beispiel #2
0
def part_1c():
    p = 0.5  # Select a split percentage value
    k = 5  # Select a value for k

    size = [32, 32]
    X, y = ps6.load_images(YALE_FACES_DIR, size)
    Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

    # training
    mu = ps6.get_mean_face(Xtrain)
    eig_vecs, eig_vals = ps6.pca(Xtrain, k)
    Xtrain_proj = np.dot(Xtrain - mu, eig_vecs)

    # testing
    mu = ps6.get_mean_face(Xtest)
    Xtest_proj = np.dot(Xtest - mu, eig_vecs)

    good = 0
    bad = 0

    for i, obs in enumerate(Xtest_proj):

        dist = [np.linalg.norm(obs - x) for x in Xtrain_proj]

        idx = np.argmin(dist)
        y_pred = ytrain[idx]

        if y_pred == ytest[i]:
            good += 1

        else:
            bad += 1

    print 'Good predictions = ', good, 'Bad predictions = ', bad
    print '{0:.2f}% accuracy'.format(100 * float(good) / (good + bad))
Beispiel #3
0
def part_1c():
    p = 0.5  # Select a split percentage value
    k = 5  # Select a value for k

    # testing values of k or comment this back in to see result set in a loop.
    # p_range = np.arange(0.1, 1.0, 0.1)
    # for j in p_range:

    size = [32, 32]
    X, y = ps6.load_images(YALE_FACES_DIR, size)
    Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

    # training
    mu = ps6.get_mean_face(Xtrain)
    eig_vecs, eig_vals = ps6.pca(Xtrain, k)
    Xtrain_proj = np.dot(Xtrain - mu, eig_vecs)

    # testing
    mu = ps6.get_mean_face(Xtest)
    Xtest_proj = np.dot(Xtest - mu, eig_vecs)

    good = 0
    bad = 0

    for i, obs in enumerate(Xtest_proj):
        dist = [np.linalg.norm(obs - x) for x in Xtrain_proj]
        idx = np.argmin(dist)
        y_pred = ytrain[idx]
        if y_pred == ytest[i]:
            good += 1
        else:
            bad += 1

    # Enable result comparsion to a random value selector.
    random_guess = np.random.randint(low=1, high=16, size=len(ytest))
    # random accuracy check.
    rand_good = 0
    rand_bad = 0
    for i in range(len(random_guess)):
        if random_guess[i] == ytest[i]:
            rand_good += 1
        else:
            rand_bad += 1

    print 'Results where P is {}'.format(p)
    print '-------------------------------'
    print 'Random Selection Results'
    print 'Good predictions = ', rand_good, 'Bad predictions = ', rand_bad
    print '(Random) Testing accuracy: {0:.2f}%'.format(100 * float(rand_good) / (rand_good + rand_bad))

    print 'Normal Dist Results'
    print 'Good predictions = ', good, 'Bad predictions = ', bad
    print '{0:.2f}% accuracy'.format(100 * float(good) / (good + bad))
    print '--------------------------------'
    print ''
def part_1c():
    p = 0.8  # Select a split percentage value
    k = 5  # Select a value for k

    size = [32, 32]
    X, y = ps6.load_images(YALE_FACES_DIR, size)
    Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

    # training
    mu = ps6.get_mean_face(Xtrain)
    eig_vecs, eig_vals = ps6.pca(Xtrain, k)
    Xtrain_proj = np.dot(Xtrain - mu, eig_vecs)

    # testing
    mu = ps6.get_mean_face(Xtest)
    Xtest_proj = np.dot(Xtest - mu, eig_vecs)

    good = 0
    bad = 0

    for i, obs in enumerate(Xtest_proj):

        dist = [np.linalg.norm(obs - x) for x in Xtrain_proj]

        idx = np.argmin(dist)
        y_pred = ytrain[idx]

        if y_pred == ytest[i]:
            good += 1

        else:
            bad += 1

    print('Good predictions = ', good, 'Bad predictions = ', bad)
    print('{0:.2f}% accuracy'.format(100 * float(good) / (good + bad)))

    rand_y = np.random.choice([1, 16], (len(ytrain)))
    temp_y = np.zeros_like(rand_y)
    temp_y[rand_y == ytrain] = 1
    rand_accuracy = 100 * float(np.sum(temp_y)) / (len(ytrain))  #None
    # raise NotImplementedError
    print('(Random) Training accuracy: {0:.2f}%'.format(rand_accuracy))
Beispiel #5
0
def part_2a():
    y0 = 1
    y1 = 2

    X, y = ps6.load_images(FACES94_DIR)
    
    # Select only the y0 and y1 classes
    idx = y == y0
    idx |= y == y1

    X = X[idx,:]
    y = y[idx]

    # Label them 1 and -1
    y0_ids = y == y0
    y1_ids = y == y1
    y[y0_ids] = 1
    y[y1_ids] = -1

    p = 0.8
    Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytrain)))
    # TODO: find which of these labels match ytrain and report its accuracy
    #rand_accuracy = None
    
    rand_accuracy = 100. * helper_func(rand_y, ytrain) / len(ytrain)  
    
    #raise NotImplementedError
    print '(Random) Training accuracy: {0:.2f}%'.format(rand_accuracy)

    # Using Weak Classifier
    uniform_weights = np.ones((Xtrain.shape[0],)) / Xtrain.shape[0]
    wk_clf = ps6.WeakClassifier(Xtrain, ytrain, uniform_weights)
    wk_clf.train()
    wk_results = [wk_clf.predict(x) for x in Xtrain]
    # TODO: find which of these labels match ytrain and report its accuracy
    wk_accuracy = None
    #raise NotImplementedError
  
    wk_accuracy = 100. * helper_func(wk_results, ytrain) / len(ytrain)
    
    print '(Weak) Training accuracy {0:.2f}%'.format(wk_accuracy)

    num_iter = 5

    boost = ps6.Boosting(Xtrain, ytrain, num_iter)
    boost.train()
    good, bad = boost.evaluate()
    boost_accuracy = 100 * float(good) / (good + bad)
    print '(Boosting) Training accuracy {0:.2f}%'.format(boost_accuracy)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytest)))
    # TODO: find which of these labels match ytest and report its accuracy
    rand_accuracy = None
    #raise NotImplementedError

    rand_accuracy = 100. * helper_func(rand_y, ytest) / len(ytest)
    
    print '(Random) Testing accuracy: {0:.2f}%'.format(rand_accuracy)

    # Using Weak Classifier
    wk_results = [wk_clf.predict(x) for x in Xtest]
    # TODO: find which of these labels match ytest and report its accuracy
    wk_accuracy = None
    #raise NotImplementedError

    wk_accuracy = 100.* helper_func(wk_results, ytest) / len(ytest)
    
    print '(Weak) Testing accuracy {0:.2f}%'.format(wk_accuracy)

    y_pred = boost.predict(Xtest)
    # TODO: find which of these labels match ytest and report its accuracy
    boost_accuracy = None
    #raise NotImplementedError   

    boost_accuracy = 100. * helper_func(y_pred, ytest) / len(ytest)
    
    print '(Boosting) Testing accuracy {0:.2f}%'.format(boost_accuracy)
Beispiel #6
0
def part_2a():
    y0 = 1
    y1 = 2

    X, y = ps6.load_images(FACES94_DIR)

    # Select only the y0 and y1 classes
    idx = y == y0
    idx |= y == y1   


    X = X[idx,:]
    y = y[idx]

    
    # Label them 1 and -1
    y0_ids = y == y0
    y1_ids = y == y1
    y[y0_ids] = 1
    y[y1_ids] = -1



    p = 0.9
    Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytrain)))

    # TODO: find which of these labels match ytrain and report its accuracy

    good = 0
    bad = 0
    for i in range(0, len(ytrain)):
        if(ytrain[i] == rand_y[i]):
            good += 1

        else:
            bad += 1


    rand_accuracy = 100 * float(good) / (good + bad)

    print '(Random) Training accuracy: {0:.2f}%'.format(rand_accuracy)

    # Using Weak Classifier
    uniform_weights = np.ones((Xtrain.shape[0],)) / Xtrain.shape[0]
    wk_clf = ps6.WeakClassifier(Xtrain, ytrain, uniform_weights)
    wk_clf.train()
    wk_results = [wk_clf.predict(x) for x in Xtrain]
    # TODO: find which of these labels match ytrain and report its accuracy
    wk_accuracy = None

    good = 0
    bad = 0
    for i in range(0, len(ytrain)):
        if(ytrain[i] == wk_results[i]):
            good += 1

        else:
            bad += 1


    wk_accuracy = 100* float(good) / (good + bad)
    print '(Weak) Training accuracy {0:.2f}%'.format(wk_accuracy)

    num_iter = 2

    boost = ps6.Boosting(Xtrain, ytrain, num_iter)
    boost.train()
    good, bad = boost.evaluate()
    boost_accuracy = 100 * float(good) / (good + bad)
    print '(Boosting) Training accuracy {0:.2f}%'.format(boost_accuracy)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytest)))
    # TODO: find which of these labels match ytest and report its accuracy
    rand_accuracy = None

    good = 0
    bad = 0
    for i in range(0, len(ytest)):
        if(ytest[i] == rand_y[i]):
            good += 1

        else:
            bad += 1


    rand_accuracy = 100 * float(good) / (good + bad)
    print '(Random) Testing accuracy: {0:.2f}%'.format(rand_accuracy)

    # Using Weak Classifier
    wk_results = [wk_clf.predict(x) for x in Xtest]
    # TODO: find which of these labels match ytest and report its accuracy
    wk_accuracy = None

    good = 0
    bad = 0
    for i in range(0, len(ytest)):
        if(ytest[i] == wk_results[i]):
            good += 1

        else:
            bad += 1

    wk_accuracy = 100* float(good) / (good + bad)
    print '(Weak) Testing accuracy {0:.2f}%'.format(wk_accuracy)

    y_pred = boost.predict(Xtest)
    # TODO: find which of these labels match ytest and report its accuracy
    boost_accuracy = None

    good = 0
    bad = 0

    for i in range(0, len(ytest)):
        if(ytest[i] == y_pred[i]):
            good += 1

        else:
            bad += 1
    
    boost_accuracy = 100* float(good) / (good + bad)
    print '(Boosting) Testing accuracy {0:.2f}%'.format(boost_accuracy)
def evaluate_faces_94(p):
    y0 = 1
    y1 = 2

    X, y = ps6.load_images(FACES94_DIR)

    # Select only the y0 and y1 classes
    idx = y == y0
    idx |= y == y1

    X = X[idx, :]
    y = y[idx]

    # Label them 1 and -1
    y0_ids = y == y0
    y1_ids = y == y1
    y[y0_ids] = 1
    y[y1_ids] = -1

    Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

    results = []

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytrain)))
    matching_indices = np.where(rand_y == ytrain)[0]
    rand_train_accuracy = matching_indices.shape[0] / ytrain.shape[0]
    # print('(Random) Training accuracy: {0:.2f}%'.format(rand_train_accuracy))

    # Using Weak Classifier
    uniform_weights = np.ones((Xtrain.shape[0], )) / Xtrain.shape[0]
    wk_clf = ps6.WeakClassifier(Xtrain, ytrain, uniform_weights)
    wk_clf.train()
    wk_results = [wk_clf.predict(x) for x in Xtrain]
    matching_indices = np.where(wk_results == ytrain)[0]
    wk__train_accuracy = matching_indices.shape[0] / ytrain.shape[0]
    results.append(wk__train_accuracy)
    # print('(Weak) Training accuracy {0:.2f}%'.format(wk__train_accuracy))

    num_iter = 4
    boost = ps6.Boosting(Xtrain, ytrain, num_iter)
    boost.train()
    good, bad = boost.evaluate()
    boost_train_accuracy = float(good) / (good + bad)
    results.append(boost_train_accuracy)
    # print('(Boosting) Training accuracy {0:.2f}%'.format(boost_train_accuracy))

    num_iter = 12
    boost_12 = ps6.Boosting(Xtrain, ytrain, num_iter)
    boost_12.train()
    good, bad = boost_12.evaluate()
    boost_train_accuracy_12 = float(good) / (good + bad)
    results.append(boost_train_accuracy_12)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytest)))
    matching_indices = np.where(rand_y == ytest)[0]
    rand_test_accuracy = matching_indices.shape[0] / ytest.shape[0]
    # print('(Random) Testing accuracy: {0:.2f}%'.format(rand_test_accuracy))

    # Using Weak Classifier
    wk_results = [wk_clf.predict(x) for x in Xtest]
    matching_indices = np.where(wk_results == ytest)[0]
    wk_test_accuracy = matching_indices.shape[0] / ytest.shape[0]
    results.append(wk_test_accuracy)
    # print('(Weak) Testing accuracy {0:.2f}%'.format(wk_test_accuracy))

    y_pred = boost.predict(Xtest)
    matching_indices = np.where(y_pred == ytest)[0]
    boost_test_accuracy = matching_indices.shape[0] / ytest.shape[0]
    results.append(boost_test_accuracy)
    # print('(Boosting) Testing accuracy {0:.2f}%'.format(boost_test_accuracy))

    y_pred = boost_12.predict(Xtest)
    matching_indices = np.where(y_pred == ytest)[0]
    boost_test_accuracy_12 = matching_indices.shape[0] / ytest.shape[0]
    results.append(boost_test_accuracy_12)

    return results
def part_2a():
    y0 = 1
    y1 = 2

    X, y = ps6.load_images(FACES94_DIR)

    # Select only the y0 and y1 classes
    idx = y == y0
    idx |= y == y1

    X = X[idx, :]
    y = y[idx]

    # Label them 1 and -1
    y0_ids = y == y0
    y1_ids = y == y1
    y[y0_ids] = 1
    y[y1_ids] = -1

    p = 0.8
    Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytrain)))
    random_correctness = np.zeros_like(rand_y)
    random_correctness[rand_y == ytrain] = 1
    rand_accuracy = 100 * float(np.sum(random_correctness)) / (len(ytrain))
    print('(Random) Training accuracy: {0:.2f}%'.format(rand_accuracy))

    # Using Weak Classifier
    uniform_weights = np.ones((Xtrain.shape[0], )) / Xtrain.shape[0]
    wk_clf = ps6.WeakClassifier(Xtrain, ytrain, uniform_weights)
    wk_clf.train()
    wk_results = [wk_clf.predict(x) for x in Xtrain]
    wk_correctness = np.zeros_like(wk_results)
    wk_correctness[wk_results == ytrain] = 1
    wk_accuracy = 100 * float(np.sum(wk_correctness)) / (len(ytrain))

    print('(Weak) Training accuracy {0:.2f}%'.format(wk_accuracy))

    num_iter = 5

    boost = ps6.Boosting(Xtrain, ytrain, num_iter)
    boost.train()
    good, bad = boost.evaluate()
    boost_accuracy = 100 * float(good) / (good + bad)
    print('(Boosting) Training accuracy {0:.2f}%'.format(boost_accuracy))

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytest)))
    random_correctness = np.zeros_like(rand_y)
    random_correctness[rand_y == ytest] = 1
    rand_accuracy = 100 * float(np.sum(random_correctness)) / (len(ytest))
    print('(Random) Testing accuracy: {0:.2f}%'.format(rand_accuracy))

    # Using Weak Classifier
    wk_results = [wk_clf.predict(x) for x in Xtest]
    wk_correctness = np.zeros_like(wk_results)
    wk_correctness[wk_results == ytest] = 1
    wk_accuracy = 100 * float(np.sum(wk_correctness)) / (len(ytest))
    print('(Weak) Testing accuracy {0:.2f}%'.format(wk_accuracy))

    y_pred = boost.predict(Xtest)
    boost_correctness = np.zeros_like(y_pred)
    boost_correctness[y_pred == ytest] = 1
    boost_accuracy = 100 * float(np.sum(boost_correctness)) / (len(ytest))
    print('(Boosting) Testing accuracy {0:.2f}%'.format(boost_accuracy))
def part_1c():
    runk = 1
    runp = 1
    if runk:
        p = 0.5  # Select a split percentage value
        ks = []
        accuracy = []
        for k in range(1, 30):
            size = (32, 32)
            X, y = ps6.load_images(YALE_FACES_DIR, size)
            Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

            # training
            mu = ps6.get_mean_face(Xtrain)
            eig_vecs, eig_vals = ps6.pca(Xtrain, k)
            Xtrain_proj = np.dot(Xtrain - mu, eig_vecs)

            # testing
            mu = ps6.get_mean_face(Xtest)
            Xtest_proj = np.dot(Xtest - mu, eig_vecs)

            good = 0
            bad = 0

            for i, obs in enumerate(Xtest_proj):

                dist = [np.linalg.norm(obs - x) for x in Xtrain_proj]

                idx = np.argmin(dist)
                y_pred = ytrain[idx]

                if y_pred == ytest[i]:
                    good += 1
                else:
                    bad += 1

            print 'Good predictions = ', good, 'Bad predictions = ', bad
            ks.append(k)
            accuracy.append(100 * float(good) / (good + bad))
            print '{0:.2f}% accuracy'.format(100 * float(good) / (good + bad))

        plt.figure()
        plt.ylabel('Accuracy')
        plt.xlabel('# of PCs')
        plt.title('Accuracy vs Number of PCs ')
        plt.plot(ks, accuracy)
        plt.grid()
        plt.draw()
        plt.savefig('./pca_plot.png')
        #plt.show()

    if runp:
        k = 10
        accuracy = []
        ps = []
        for p in np.arange(0.2, 1.0, 0.1):
            size = (32, 32)
            X, y = ps6.load_images(YALE_FACES_DIR, size)
            Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

            # training
            mu = ps6.get_mean_face(Xtrain)
            eig_vecs, eig_vals = ps6.pca(Xtrain, k)
            Xtrain_proj = np.dot(Xtrain - mu, eig_vecs)

            # testing
            mu = ps6.get_mean_face(Xtest)
            Xtest_proj = np.dot(Xtest - mu, eig_vecs)

            good = 0
            bad = 0

            for i, obs in enumerate(Xtest_proj):

                dist = [np.linalg.norm(obs - x) for x in Xtrain_proj]

                idx = np.argmin(dist)
                y_pred = ytrain[idx]

                if y_pred == ytest[i]:
                    good += 1
                else:
                    bad += 1

            print 'Good predictions = ', good, 'Bad predictions = ', bad
            ps.append(p)
            accuracy.append(100 * float(good) / (good + bad))
            print '{0:.2f}% accuracy'.format(100 * float(good) / (good + bad))

        plt.figure()
        plt.ylabel('Accuracy')
        plt.xlabel('Percentage of data split')
        plt.title('Accuracy vs data split percentage ')
        plt.plot(ps, accuracy)
        plt.grid()
        plt.draw()
        plt.savefig('./split_P_plot.png')
Beispiel #10
0
def part_2a(p = 0.8, num_iter = 5, seed = 1, verbose = True):
    
    y0 = 1
    y1 = 2

    X, y = ps6.load_images(FACES94_DIR)

    # Select only the y0 and y1 classes
    idx = y == y0
    idx |= y == y1

    X = X[idx.flatten(),:]
    y = y[idx]

    # Label them 1 and -1
    y0_ids = y == y0
    y1_ids = y == y1
    y[y0_ids] = 1
    y[y1_ids] = -1

    Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p, seed = seed)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytrain)))
    # TODO: find which of these labels match ytrain and report its accuracy
    rand_accuracy =  100*sum(ytrain==rand_y)*1./ytrain.shape[0]
    # raise NotImplementedError
    if verbose:
        print '(Random) Training accuracy: {0:.2f}%'.format(rand_accuracy)

    # Using Weak Classifier
    uniform_weights = np.ones((Xtrain.shape[0],)) / Xtrain.shape[0]
    wk_clf = ps6.WeakClassifier(Xtrain, ytrain, uniform_weights)
    wk_clf.train()
    wk_results = [wk_clf.predict(x) for x in Xtrain]
    # TODO: find which of these labels match ytrain and report its accuracy
    wk_accuracy = 100*sum(ytrain==wk_results)*1./ytrain.shape[0]
    #raise NotImplementedError
    if verbose:
        print '(Weak) Training accuracy {0:.2f}%'.format(wk_accuracy)

    boost = ps6.Boosting(Xtrain, ytrain, num_iter)
    boost.train()
    good, bad = boost.evaluate()
    boost_accuracy = 100 * float(good) / (good + bad)
    if verbose:
        print '(Boosting) Training accuracy {0:.2f}%'.format(boost_accuracy)
    results = [p, num_iter, rand_accuracy, wk_accuracy, boost_accuracy ]

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytest)))
    # TODO: find which of these labels match ytest and report its accuracy
    rand_accuracy = 100*sum(ytest==rand_y)*1./ytest.shape[0]
    if verbose:
        print '(Random) Testing accuracy: {0:.2f}%'.format(rand_accuracy)

    # Using Weak Classifier
    wk_results = [wk_clf.predict(x) for x in Xtest]
    # TODO: find which of these labels match ytest and report its accuracy
    wk_accuracy = 100*sum(ytest==wk_results)*1./ytest.shape[0]
    if verbose:
        print '(Weak) Testing accuracy {0:.2f}%'.format(wk_accuracy)

    y_pred = boost.predict(Xtest)
    # TODO: find which of these labels match ytest and report its accuracy
    boost_accuracy = 100*sum(ytest==y_pred)*1./ytest.shape[0]
    if verbose:
        print '(Boosting) Testing accuracy {0:.2f}%'.format(boost_accuracy)
    results += [rand_accuracy, wk_accuracy, boost_accuracy ]
    return results
Beispiel #11
0
def part_2a():
    y0 = 1
    y1 = 2

    X, y = ps6.load_images(FACES94_DIR)

    # Select only the y0 and y1 classes
    idx = y == y0
    idx |= y == y1

    X = X[idx, :]
    y = y[idx]

    # Label them 1 and -1
    y0_ids = y == y0
    y1_ids = y == y1
    y[y0_ids] = 1
    y[y1_ids] = -1

    p = 0.8
    Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytrain)))
    matches = [rand_y[i] == ytrain[i] for i in range(0, len(rand_y))]
    corr = np.sum([int(x) for x in matches])
    rand_accuracy = float(corr) / float(len(ytrain)) * 100
    print '(Random) Training accuracy: {0:.2f}%'.format(rand_accuracy)

    # Using Weak Classifier
    uniform_weights = np.ones((Xtrain.shape[0], )) / Xtrain.shape[0]
    wk_clf = ps6.WeakClassifier(Xtrain, ytrain, uniform_weights)
    wk_clf.train()
    wk_results = [wk_clf.predict(x) for x in Xtrain]
    matches = [ytrain[i] == wk_results[i] for i in range(0, len(ytrain))]
    corr = np.sum([int(x) for x in matches])
    wk_accuracy = float(corr) / float(len(ytrain)) * 100
    print '(Weak) Training accuracy {0:.2f}%'.format(wk_accuracy)

    # num_iter = 5
    for i in range(5, 10):
        print i
        num_iter = i
        boost = ps6.Boosting(Xtrain, ytrain, num_iter)
        boost.train()
        good, bad = boost.evaluate()
        # print good, bad
        boost_accuracy = 100 * float(good) / (good + bad)
        print '(Boosting) Training accuracy {0:.2f}%'.format(boost_accuracy)

        y_pred = boost.predict(Xtest)
        matches = [y_pred[i] == ytest[i] for i in range(0, len(ytest))]
        corr = np.sum(np.array([int(x) for x in matches]))
        boost_accuracy = corr / float(len(ytest)) * 100
        print '(Boosting) Testing accuracy {0:.2f}%'.format(boost_accuracy)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytest)))
    matches = [rand_y[i] == ytest[i] for i in range(0, len(ytest))]
    # print matches
    corr = np.sum(np.array([int(x) for x in matches]))
    rand_accuracy = corr / float(len(ytest)) * 100
    print '(Random) Testing accuracy: {0:.2f}%'.format(rand_accuracy)

    # Using Weak Classifier
    wk_results = [wk_clf.predict(x) for x in Xtest]
    matches = [ytest[i] == wk_results[i] for i in range(0, len(Xtest))]
    # print matches
    corr = np.sum(np.array([int(x) for x in matches]))
    # print corr
    wk_accuracy = corr / float(len(ytest)) * 100
    print '(Weak) Testing accuracy {0:.2f}%'.format(wk_accuracy)
Beispiel #12
0
def part_2a():
    y0 = 1
    y1 = 2

    X, y = ps6.load_images(FACES94_DIR)

    # Select only the y0 and y1 classes
    idx = y == y0
    idx |= y == y1

    X = X[idx, :]
    y = y[idx]

    # Label them 1 and -1
    y0_ids = y == y0
    y1_ids = y == y1
    y[y0_ids] = 1
    y[y1_ids] = -1

    p = 0.8
    Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytrain)))
    # the accuracy is the percent of correct answer. Loop over the lenght
    correct = 0
    for i in range(len(ytrain)):
        if rand_y[i] == ytrain[i]:
            correct += 1
    # convert to a %
    rand_accuracy = 100 * (float(correct) / float(len(ytrain)))
    print '(Random) Training accuracy: {0:.2f}%'.format(rand_accuracy)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytest)))
    correct = 0
    for i in range(len(ytest)):
        if rand_y[i] == ytest[i]:
            correct += 1
    rand_accuracy = 100 * (float(correct) / float(len(ytest)))
    print '(Random) Testing accuracy: {0:.2f}%'.format(rand_accuracy)

    # Using Weak Classifier
    uniform_weights = np.ones((Xtrain.shape[0],)) / Xtrain.shape[0]
    wk_clf = ps6.WeakClassifier(Xtrain, ytrain, uniform_weights)
    wk_clf.train()
    wk_results = [wk_clf.predict(x) for x in Xtrain]
    correct = 0
    for i in range(len(ytrain)):
        if wk_results[i] == ytrain[i]:
            correct += 1
    # convert to a %
    wk_accuracy = 100 * (float(correct) / float(len(ytrain)))
    print '(Weak) Training accuracy {0:.2f}%'.format(wk_accuracy)

    # Using Weak Classifier
    wk_results = [wk_clf.predict(x) for x in Xtest]
    correct = 0
    for i in range(len(ytest)):
        if wk_results[i] == ytest[i]:
            correct += 1
    wk_accuracy = 100 * (float(correct) / float(len(ytest)))
    print '(Weak) Testing accuracy {0:.2f}%'.format(wk_accuracy)

    num_iter = 5
    # for num_iter in range(1, 11):
    boost = ps6.Boosting(Xtrain, ytrain, num_iter)
    boost.train()
    good, bad = boost.evaluate()
    boost_accuracy = 100 * float(good) / (good + bad)
    print '(Boosting) Training accuracy {0:.2f}%'.format(boost_accuracy)

    y_pred = boost.predict(Xtest)
    correct = 0
    for i in range(len(ytest)):
        if y_pred[i] == ytest[i]:
            correct += 1
    boost_accuracy = 100 * (float(correct) / float(len(ytest)))
    print '(Boosting) Testing accuracy {0:.2f}%'.format(boost_accuracy)
def part_2a():
    y0 = 1
    y1 = 2

    X, y = ps6.load_images(FACES94_DIR)

    # Select only the y0 and y1 classes
    idx = y == y0
    idx |= y == y1

    # print(np.shape(X))
    X = X[idx, :]
    y = y[idx]
    # print(np.shape(y))

    # Label them 1 and -1
    y0_ids = y == y0
    y1_ids = y == y1
    # print(np.shape(y))
    y[y0_ids] = 1
    y[y1_ids] = -1

    p = 0.1
    Xtrain, ytrain, Xtest, ytest = ps6.split_dataset(X, y, p)

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytrain)))
    # TODO: find which of these labels match ytrain and report its accuracy
    temp_y = np.zeros_like(rand_y)
    temp_y[rand_y == ytrain] = 1
    rand_accuracy = 100 * float(np.sum(temp_y)) / (len(ytrain))  #None
    # raise NotImplementedError
    print('(Random) Training accuracy: {0:.2f}%'.format(rand_accuracy))

    # Using Weak Classifier
    uniform_weights = np.ones((Xtrain.shape[0], )) / Xtrain.shape[0]
    wk_clf = ps6.WeakClassifier(Xtrain, ytrain, uniform_weights)
    wk_clf.train()
    wk_results = [wk_clf.predict(x) for x in Xtrain]
    temp_wk = np.zeros_like(wk_results)
    temp_wk[wk_results == ytrain] = 1
    # TODO: find which of these labels match ytrain and report its accuracy
    wk_accuracy = 100 * float(np.sum(temp_wk)) / (len(ytrain))  #None
    # raise NotImplementedError
    print('(Weak) Training accuracy {0:.2f}%'.format(wk_accuracy))

    num_iter = 20

    # print(np.shape(Xtrain))

    boost = ps6.Boosting(Xtrain, ytrain, num_iter)
    boost.train()
    good, bad = boost.evaluate()
    boost_accuracy = 100 * float(good) / (good + bad)
    print('(Boosting) Training accuracy {0:.2f}%'.format(boost_accuracy))

    # Picking random numbers
    rand_y = np.random.choice([-1, 1], (len(ytest)))
    # TODO: find which of these labels match ytest and report its accuracy
    temp_y = np.zeros_like(rand_y)
    temp_y[rand_y == ytest] = 1
    rand_accuracy = 100 * float(np.sum(temp_y)) / (len(ytest))
    # raise NotImplementedError
    print('(Random) Testing accuracy: {0:.2f}%'.format(rand_accuracy))

    # Using Weak Classifier
    wk_results = [wk_clf.predict(x) for x in Xtest]
    # TODO: find which of these labels match ytest and report its accuracy
    temp_wk = np.zeros_like(wk_results)
    temp_wk[wk_results == ytest] = 1
    wk_accuracy = 100 * float(np.sum(temp_wk)) / (len(ytest))
    # raise NotImplementedError
    print('(Weak) Testing accuracy {0:.2f}%'.format(wk_accuracy))

    y_pred = boost.predict(Xtest)
    # TODO: find which of these labels match ytest and report its accuracy
    temp_b = np.zeros_like(y_pred)
    temp_b[y_pred == ytest] = 1
    boost_accuracy = 100 * float(np.sum(temp_b)) / (len(ytest))
    # raise NotImplementedError
    print('(Boosting) Testing accuracy {0:.2f}%'.format(boost_accuracy))