diff --git a/assignments/hwk02/Eigenfaces.m b/assignments/hwk02/Eigenfaces.m index ea3649d..36f30a1 100644 --- a/assignments/hwk02/Eigenfaces.m +++ b/assignments/hwk02/Eigenfaces.m @@ -8,5 +8,6 @@ function [] = Eigenfaces(training_data, test_data) % TODO: perform PCA % TODO: show the first 5 eigenvectors (see homework for example) + imagesc(reshape(faces_data(i,1:end-1),32,30)') end % Function end \ No newline at end of file diff --git a/assignments/hwk02/HW2.typ b/assignments/hwk02/HW2.typ index 0544a83..d2085b0 100644 --- a/assignments/hwk02/HW2.typ +++ b/assignments/hwk02/HW2.typ @@ -37,4 +37,8 @@ b. #c[*(20 points)* Generate a plot of proportion of variance (see Figure 6.4 (b) in the main textbook) on the training data, and select the minimum number ($K$) of eigenvectors that explain at least 90% of the variance. Show both the plot and $K$ in the report. This can be accomplished by completing the _TODO_ headers in the `ProportionOfVariance.m` script. Project the training and test data to the $K$ principal components and run KNN on the projected data for $k = {1, 3, 5, 7}$. Print out the error rate on the test set. Implement your own version of and K-Nearest Neighbor classifier (KNN) for this problem. Classify each test point using a majority rule i.e., by choosing the most common class among the $k$ training points it is closest to. In the case where two classes are equally as frequent, perform a tie-breaker by choosing whichever class has on average a smaller distance to the test point. This can be accomplished by completing the _TODO_ comment headers in the `KNN.m` and `KNN_Error.m` scripts.] + #figure(image("images/prop_var.png")) + + I used $K = 41$. + c. #c[*(20 points)* Use the first $K = {10, 50, 100}$ principle components to approximate the first five images of the training set (first row of the data matrix) by projecting the centered data using the first $K$ principal components then "back project" (weighted sum of the components) to the original space and add the mean. For each $K$, plot the reconstructed image. This can be accomplished by completing the _TODO_ comment headers in the `Back_Project.m` script. Explain your observations in the report.] \ No newline at end of file diff --git a/assignments/hwk02/ProportionOfVariance.m b/assignments/hwk02/ProportionOfVariance.m index b5cbd76..1dee5ed 100644 --- a/assignments/hwk02/ProportionOfVariance.m +++ b/assignments/hwk02/ProportionOfVariance.m @@ -4,14 +4,24 @@ % variance explained. function [neigenvectors] = ProportionOfVariance(training_data) - % stack data - data = vertcat(training_data); + % stack data + data = vertcat(training_data); - % TODO: perform PCA + % perform PCA + [~,~,latent] = pca(data); - % TODO: compute proportion of variance explained + % compute proportion of variance explained + all_eigs = sum(latent); + prop_var = cumsum(latent) / all_eigs; - % TODO: show figure of proportion of variance explained where the x-axis is the number of eigenvectors and the y-axis is the percentage of + % show figure of proportion of variance explained where the x-axis is the number of eigenvectors and the y-axis is the percentage of % variance explained - + subplot(2,1,1); + plot(find(latent), latent); + + subplot(2,1,2); + plot(find(prop_var), prop_var); + + neigenvectors = find(prop_var > 0.9, 1); + end % Function end \ No newline at end of file diff --git a/assignments/hwk02/images/prop_var.png b/assignments/hwk02/images/prop_var.png new file mode 100644 index 0000000..4a97899 Binary files /dev/null and b/assignments/hwk02/images/prop_var.png differ