<jupyter_start><jupyter_text>Part (a)<jupyter_code>close all;
clear all;
load('PCA_Exercise_Images.mat')
vectorized_images = [];
images = {};
images{1} = Photo_Images;
images{2} = VTEC_Images;
imgindex = 1;
R= [1,10,80];
img = images{imgindex}; 
img_title = 'pca-part-c-VTEC_Images';
index = 170;

if (imgindex ==1)
    img_title = 'pca-part-c-Photo_Images';
    index = 70;
end
disp(img_title);
[nrow, ncol, nimg] = size(img);
original = double(img(:,:,index));

for j=1:numel(R)
    r= R(j);
    for i=1:nrow/20
        for j=1:ncol/20
            patch = original(20*(i-1)+1:20*(i), 20*(j-1)+1:20*j);
            vimg = double(vectorize_image(patch));
            vectorized_images = [vectorized_images; vimg];
        end
    end
    mean_patch = mean(vectorized_images);
    [eigvals, eigvecs, projected, reconstructed] = perform_pca(vectorized_images, r);

    imagesc(original);
    print(sprintf('%s-original', img_title), '-dpng');
    close all;

    rec_patches_all = [];
    rec_patches_temp = [];

    for i=1:(nrow*ncol)/400
        rec_patch = reshape(reconstructed(i, :),20,20);
        rec_patches_temp = [rec_patches_temp  rec_patch];
        if (mod(i,ncol/20)==0)
            %rest
            rec_patches_all= [rec_patches_all;rec_patches_temp];
            rec_patches_temp = [];

        end
    end

    %imagesc(reshape(reconstructed,600,600));
    imagesc(rec_patches_all);
    print(sprintf('%s-reconstructed-r=%d',img_title, r), '-dpng');
    close all;


    all_patch_minus_mean = bsxfun(@minus, vectorized_images, mean_patch);
    all_patch_minus_mean = all_patch_minus_mean.^2;
    euc_distance = sum(sqrt(sum(all_patch_minus_mean,2)))/(size(vectorized_images,1));
    total_variance = sum(eigvals);

    new_distance = bsxfun(@minus, reconstructed, mean_patch);
    new_distance = new_distance.^2;
    new_euc_distance = sum(sqrt(sum(new_distance,2)))/(size(vectorized_images,1));
    captured_variance = sum(eigvals(1:r));

    variance_ratio = captured_variance/total_variance;
    disp(sprintf('r=%f \t Captured Variance: %f',r, variance_ratio));
end<jupyter_output><empty_output><jupyter_text>Part (b)<jupyter_code>%%matlab
load('PCA_Exercise_Images.mat')
vectorized_images = [];
[nrow, ncol, nimg] = size(Photo_Images);
for i=1:nimg
    vimg = vectorize_image(Photo_Images(:,:,i));
    vectorized_images = [vectorized_images; vimg];
end
vectorized_images = double(vectorized_images);
[eigvals, eigvecs, projected, reconstructed] = perform_pca(vectorized_images, 25);
figure('position', [0, 0, 5000, 5000]) 
for i=1:25
    eigimage = reshape(eigvecs(:,i),20,20);
    g=subplot(5,5,i);
    colormap(gray);
    imagesc(eigimage);
    axis off;
    title(i, 'FontSize', 6);

end


from IPython.display import Image
from IPython.display import display
display(Image(filename="Mean-Patch-VTEC-Images.png"), 'Est')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1537391304347826, 'fraction_numerical': 0.02782608695652174, 'mean_word_length': 4.426415094339623, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '658473', 'license': 'MIT', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1095, 'n_words': 174, 'path': '2015_Fall/MATH-541B/pca/PCA.ipynb', 'repo_name': 'NeveIsa/hatex', 'size': '2875'}