ratio = sum_ev / total_sum if ratio > threshold: break k += 1 return k """ Step 0a: Load data Here we provide the code to load natural image data into x. x will be a 144 * 10000 matrix, where the kth column x(:, k) corresponds to the raw image data from the kth 12x12 image patch sampled. You do not need to change the code below. """ # Return patches from sample images x = sample_images_raw('data/IMAGES_RAW.mat') # n is the number of dimensions and m is the number of patches n, m = x.shape random_sel = np.random.randint(0, m, 200) image_x = display_network(x[:, random_sel]) fig = plt.figure() plt.imshow(image_x, cmap=plt.cm.gray) plt.title('Raw patch images') """ Step 0b: Zero-mean the data (by row) """ x -= np.mean(x, axis=1).reshape(-1, 1)
import sample_images import random import display_network import numpy as np ##================================================================ ## Step 0a: Load data # Here we provide the code to load natural image data into x. # x will be a 144 * 10000 matrix, where the kth column x(:, k) corresponds to # the raw image data from the kth 12x12 image patch sampled. # You do not need to change the code below. patches = sample_images.sample_images_raw() num_samples = patches.shape[1] random_sel = random.sample(range(num_samples), 400) display_network.display_network(patches[:, random_sel], 'raw_pca.png') ##================================================================ ## Step 0b: Zero-mean the data (by row) # You can make use of the mean and repmat/bsxfun functions. # patches = patches - patches.mean(axis=0) patch_mean = patches.mean(axis=1) patches = patches - np.tile(patch_mean, (patches.shape[1], 1)).transpose() ##================================================================ ## Step 1a: Implement PCA to obtain xRot # Implement PCA to obtain xRot, the matrix in which the data is expressed # with respect to the eigenbasis of sigma, which is the matrix U.