def check_project_onto_PC():
    ex_name = "Project onto PC"
    X = np.array([
        [0, 1, 2, 3],
        [1, 2, 3, 4],
        [2, 3, 4, 5],
    ])
    pcs = features.principal_components(X)
    #    exp_res = np.array([
    #        [-2, 0, 0],
    #        [0, 0, 0],
    #        [2, 0, 0]
    #    ])
    exp_res = np.array([[2, 0, 0], [0, 0, 0], [-2, 0, 0]])
    n_components = 3
    if check_array(ex_name, features.project_onto_PC, exp_res, X, pcs,
                   n_components):
        return

    X = np.array([
        [1, 2, 3],
        [2, 4, 6],
        [3, 6, 9],
        [4, 8, 12],
    ])
    pcs = features.principal_components(X)
    exp_res = np.array([
        [5.61248608, 0],
        [1.87082869, 0],
        [-1.87082869, 0],
        [-5.61248608, 0],
    ])
    n_components = 2
    if check_array(ex_name, features.project_onto_PC, exp_res, X, pcs,
                   n_components):
        return

    log(green("PASS"), ex_name, "")
Exemplo n.º 2
0
def check_project_onto_PC():
    ex_name = "Project onto PC"
    X = np.array([
        [0, 1, 2, 3],
        [1, 2, 3, 4],
        [2, 3, 4, 5],
    ])
    pcs = features.principal_components(X)
    exp_res = np.array([[-2, 0, 0], [0, 0, 0], [2, 0, 0]])
    n_components = 3
    if check_array(ex_name, features.project_onto_PC, exp_res, X, pcs,
                   n_components):
        return
    log(green("PASS"), ex_name, "")
Exemplo n.º 3
0
def check_project_onto_PC():
    ex_name = "Project onto PC"
    X = np.array([
        [1, 2, 3],
        [2, 4, 6],
        [3, 6, 9],
        [4, 8, 12],
    ])
    x_centered, feature_means = features.center_data(X)
    pcs = features.principal_components(x_centered)
    exp_res = np.array([
        [5.61248608, 0],
        [1.87082869, 0],
        [-1.87082869, 0],
        [-5.61248608, 0],
    ])
    n_components = 2
    if check_array(ex_name, features.project_onto_PC, exp_res, X, pcs,
                   n_components, feature_means):
        return
    log(green("PASS"), ex_name, "")
Exemplo n.º 4
0
    # raise NotImplementedError


# TODO: Run run_softmax_on_MNIST_mod3(), report the error rate
print('Mod3 softmax test_error=', run_softmax_on_MNIST_mod3(temp_parameter=1))

#######################################################################
# 7. Classification Using Manually Crafted Features
#######################################################################

## Dimensionality reduction via PCA ##

# TODO: First fill out the PCA functions in features.py as the below code depends on them.

n_components = 18
pcs = principal_components(train_x)
train_pca = project_onto_PC(train_x, pcs, n_components)
test_pca = project_onto_PC(test_x, pcs, n_components)
# train_pca (and test_pca) is a representation of our training (and test) data
# after projecting each example onto the first 18 principal components.

# TODO: Train your softmax regression model using (train_pca, train_y)
#       and evaluate its accuracy on (test_pca, test_y).
temp_parameter = 1
theta, cost_function_history = softmax_regression(train_pca,
                                                  train_y,
                                                  temp_parameter,
                                                  alpha=0.3,
                                                  lambda_factor=1.0e-4,
                                                  k=10,
                                                  num_iterations=150)
Exemplo n.º 5
0
    print('(t = {})  \t\t{:.3}'.format(temp_parameter, test_error_mod3))

print('\nsoftmax mod3 \t\ttest_error:')
for temp_parameter in temp_parameters:
    theta = run_softmax("mod3", train_x, update_y(train_y), temp_parameter, 3)
    test_error = compute_test_error(test_x, update_y(test_y), theta,
                                    temp_parameter)
    print('(t = {})  \t\t{:.3}'.format(temp_parameter, test_error))

#######################################################################
# 8. Dimensionality Reduction Using PCA
#######################################################################

n_components = 18
train_x_c, mean = center_data(train_x)
pcs = principal_components(train_x_c)
train_pca = project_onto_PC(train_x_c, pcs, n_components)
test_pca = project_onto_PC(test_x - mean, pcs, n_components)

# Scatterplot of the first 100 MNIST images, as represented in the space
# spanned by the first 2 principal components found above.
# plot_PC(train_x[range(100), ], pcs, train_y[range(100)])

# First and second MNIST images as reconstructed solely from their 18-dimensional
# principal component representation, compared with the originals.
firstimage_reconstructed = reconstruct_PC(train_pca[0, ], pcs, n_components,
                                          train_x)
secondimage_reconstructed = reconstruct_PC(train_pca[1, :], pcs, n_components,
                                           train_x)
# plot_images(np.vstack((firstimage_reconstructed, secondimage_reconstructed, train_x[0, :], train_x[1, :])))