Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSCI 5521: Intro to Machine Learning

(Spring 2023)

Homework 2 Solution

1.  (50 points) In this problem, you will implement a program to it two mul- tivariate Gaussian distributions to the 2-class data and classify the test data by computing the log odds logP(P)C2jx(C1jx)   (equivalent to comparing discriminant

functions).  Three pairs of training data and test data are given.   The priors P (C1 ) and P (C2 ) are estimated from the training data.  The parameters μ1 , μ2 , S1  and S2 , the mean and covariance for class 1 and class 2, are learned in the following three models for each training data and test data pair,

Model 1:  Assume independent S1  and S2  (the discriminant function is as equation (5.17) in the textbook).

.  Model  2:   Assume  S1   =  S2 .    In  other words,  shared  S  between two classes (the discriminant function is as equation (5.21) and (5.22) in the textbook).

.  Model 3:  Assume  S1   and S2  are diagonal (the Naive Bayes’ model in equation (5.24)).

(a)  (30  points) Implement all the three models and test your program on the three pairs of training data and test data.  The main script function, Problem1(training data ile,test data ile) is given and this script should not be modiied. There are 3 scripts that need to be completed for Prob- lem 1 (Error Rate.m, Param Est.m, Classify.m) .  The  TODO:  comment headers must be illed in in all 3 of these iles.   These TODO headers describe exactly what code needs to be written to obtain full credit. The script Error Rate.m is for calculating the error rate .  Param Est.m is for estimating the parameters of each multivariante Gaussian distribution un- der the 3 diferent models.  Classify.mis for classify the test data using the learned models. For each test dataset, the problem calls several functions and print out the training error rate and test error rate of each model to the MATLAB command window.

(b)  (5 points) State which model works best on each test data set and explain why you believe this is the case. Discuss your observations.

(c)  (15  points) Write the log likelihood function and derive S1  and S2  by maximum likelihood estimation of model 2.  Note that since S1  and S2 are shared as S, you need to add the log likelihood function of the two classes to maximizing for deriving S.

2.  (50 points) In this problem, you will work on dimension reduction and clas- siication on a Faces dataset from the UCI repository. We provided the pro- cessed iles face train data 960. txt and face test data 960.txt with 500 and 124 images, respectively.  Each image is of size 30 x 32 with the pixel values in a row in the iles and the last column identiies the labels:  1  (sunglasses), and 0 (open) of the image. You can visualize the ith image with the following matlab command line:

imagesc(reshape(faces data(i,1:end-1),32,30)’) .

The main script function, Problem2(training data ile,test data ile) is given and this  script  should  not  be  modiied.   There  are  5  scripts  that  need  to be completed for Problem 2 (Eigenfaces.m, ProportionOfVariance.m, KNN.m KNN Error.m, Back Project.m). The TODO: comment headers must be illed in in all 5 of these iles. These TODO comment headers describe exactly what code needs to be written to obtain full credit.

(a)  (10 points) Apply Principal Component Analysis (PCA) to ind the prin- cipal components with combined training and test sets.  First, visualize the irst 5 eigen-faces using a similar command line as above.  This can be accomplished by completing the TODO comment headers in the Eigen- faces.m script.

(b)  (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 num- ber (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 com- pleting 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 classiier (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 .

(c)  (20 points) Use the irst K = {10; 50; 100} principle components to ap- proximate the irst ive images of the training set (irst row of the data matrix) by projecting the centered data using the irst K principal com- ponents 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.

(Hint:  Read section 6.3 on page  126 and  127 of the textbook for the projection and ”back projection” to the original space.)

Things to submit:

(a) hw2 sol.pdf:  A document which contains the report with solutions to Q1 and Q2.  Ensure that everything that is speciied to be reported or discussed in the questions is included in the report.  Scanned answer sheets need to be clean and 100% legible.

(b)  A compressed ile hw2 code.zip that contains all of the source code and

data iles for Q1 and Q2. This folder should include Problem1.m, Param Est.m,  Classify.m, Error Rate.m, training data1.txt, test data1.txt, training data2.txt, test data2.txt, training data3.txt, test data3.txt, Problem2.m, Eigenfaces.m,

ProportionOfVariance.m, KNN.m, KNN Error.m, Back Project.m, face train data 960.txt, face test data 960.txt .

(c) All material must be submitted electronically via Canvas.