Beispiel #1
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],
    ])
    pcs = features.principal_components(features.center_data(X)[0])
    exp_res = np.array([
        [5.61248608, 0],
        [1.87082869, 0],
        [-1.87082869, 0],
        [-5.61248608, 0],
    ])
    n_components = 2
    feature_means = features.center_data(X)[1]
    if check_array(ex_name, features.project_onto_PC, exp_res, X, pcs,
                   n_components, feature_means):
        return
    log(green("PASS"), ex_name, "")
                                              temp_parameter)
    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, :])))