"""
Random projection, Assignment 1c
"""

@@ -30,12 +30,1759 @@
 ment 1c%0A%22%22%22%0A
+import numpy as np%0Aimport matplotlib.pylab as plt%0Aimport random, mnist_dataloader%0Afrom numpy import dtype%0A%0A%22%22%22%0AGenerate random projection matrix R%0A@param: k, the reduced number of dimensions%0A@param: d, the original number of dimensions%0A@return: R, the generated random projection matrix, k * d size %0A%22%22%22%0A%0Adef generate_random_projection_matrix(k, d):%0A    R = np.zeros((k, d), dtype = np.float64)%0A    for r in np.nditer(R, op_flags=%5B'readwrite'%5D):%0A        r%5B...%5D = random.randint(0, 1)%0A        if r%5B...%5D == 0:%0A            r%5B...%5D = -1%0A    %0A    R *= 1.0 / np.sqrt(k)%0A    %0A    return R%0A%0A%22%22%22%0Arandom projection matrix P into R%0A@param R: random projection matrix%0A@param P: matrix to be reduced in dimension%0A@return: Q: projected matrix of P on R%0A%22%22%22%0Adef random_projection(R, P):%0A    if R.shape%5B1%5D != P.shape%5B0%5D:%0A        return False%0A    %0A    print R.shape, P.shape%0A    %0A    return np.dot(R, P)%0A%0Aif __name__ == %22__main__%22:%0A    # load data set%0A    training_data, validation_data, test_data = mnist_dataloader.load_data()%0A    # row vector (matrix)%0A    training_data_instances = training_data%5B0%5D%0A    training_data_labels = training_data%5B1%5D%0A    # row vector (matrix)%0A    test_data_instances = test_data%5B0%5D%0A    test_data_labels = test_data%5B1%5D%0A    # dimension of a training data instance%0A    d = training_data_instances.shape%5B1%5D%0A        %0A    for k in %5B50, 100, 500%5D:%0A        random_projection_matrix =  generate_random_projection_matrix(k, d)%0A        # transpose to column vector (matrix) before projection and recover after projection%0A        random_projected_matrix = np.transpose(random_projection(random_projection_matrix, np.transpose(training_data_instances%5B0:20%5D)))%0A        %0A        print random_projected_matrix%5B0%5D, random_projected_matrix.shape%0A        

Add random projection matrix generation and random projection.