def compute_eigen_faces(X, k=20): ''' Compute top k eigen faces of the olivetti face image dataset using PCA. Each eigen face corresponds to an eigen vector in the PCA on matrix X. For example, suppose the top k eigen vectors in the PCA on matrix X are e1, e2, e3 (suppose k=3), Then e1 is a vector of length 4096, if we reshape e1, we can get the first eigen face image is a matrix of shape 64x64. Similar, reshaping e2, we get the second eigen face image. Input: X: the feature matrix, a float numpy matrix of shape (400, 4096). Here 400 is the number of images, 4096 is the number of features. k: the number of eigen face to keep. Output: P_images: the eigen faces, a python list of length k. The i-th element in the list is the i-th eigen face image, which is a numpy float matrix of shape 64 by 64. Xp: the feature matrix with reduced dimensions, a numpy float matrix of shape n by k. (400 by k) Note: this function may take 1-5 minutes and 1-2GB of memory to run. ''' ######################################### ## INSERT YOUR CODE HERE Xp, p = p2.PCA(X, k) P_images = list() for i in range(k): P_images.append(vector_to_image(p[:, i])) ######################################### return P_images, Xp '''
def compute_eigen_faces(X, k=20): ''' Compute top k eigen faces of the olivetti face image dataset using PCA. Input: X: the feature matrix, a float numpy matrix of shape (400, 4096). Here 400 is the number of images, 4096 is the number of features. k: the number of eigen face to keep. Output: P_images: the eigen faces, a python list of length k. Each element in the list is an eigen face image, which is a numpy float matrix of shape 64 by 64. Xp: the feature matrix with reduced dimensions, a numpy float matrix of shape n by k. (400 by k) Note: this function may take 1-5 minutes to run, and 1-2GB of memory while running. ''' ######################################### ## INSERT YOUR CODE HERE Xp, P_images_before_reshape = p2.PCA(X, k) P_images_before_reshape = P_images_before_reshape.T P_images = [] for row in range(k): P_images.append(reshape_to_image(P_images_before_reshape[row, :])) ######################################### return P_images, Xp