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

COMP 5541 Assignment 1 (9% of the Final Score)

2022

1    Linear Regression (30 marks)

Suppose that x0 , x1 , . . . , xt , xt1 , . . . , denote the (daily) share prices of a particular stock over time. Answer the following questions (you should add a bias term):

1. Write a linear model to predict xt1  using the share prices on the two preceeding days, i.e., xt  and xt - 1 .  (6 marks)

2.  The more useful quantity to predict is t1:= xt1  - xt  (the ‘:=’ means is defined to be’), the change in share value. Write a linear model to predict t1  using xt  and xt - 1 .  (6 marks)

3. Write a linear model to predict t1  using t .  (6 marks)

4. Write a linear model to predict t1  using t  and xt .  (6 marks)

5. Which of the above four models, if any, are equivalent?  Justify your answer briefly. (6 marks)

Note:  This example is for illustrative purposes to help you understand linear regression. It is not recommended that you use this for actual share price prediction.  For question 1-4, you only need to write down the mathematical relationship between input and output. There is no need to solve the models.

2    Nearest Neighbour and Linear Classication (30 marks)

We have studied a linear classifier Logistic Regression y = σ(wz + w0 ). If z >= 0, σ(z) >= 0.5 and y can be regarded as class ‘1’, and otherwise ‘0’ if z < 0.  The parameters to be learnt are w and w0 .  The  Nearest neighbour classifier” (NN) is a different approach to learning from data. Suppose we are given N points (zl , y1 ), . . . , (zN , yN ) where yi  ∈ e0, 1}; for a parameter k and given a new point z* , the k-NN approach does the following:  find zjl , . . . , zjk   the k-closest points to z* , then output yˆ*  as the majority label from the set eyjl , . . . , yjk }, i.e., the most commonly occurring label among the k-nearest neighbours.

1. What advantage(s) does the k-NN approach offer over a linear classifier like the logistic regression?  (10 marks)

2. What advantage(s) does the logistic regression offer over the k-NN approach?   (10 marks)

3. What is the computational cost of predicting the label yˆ*  ?  (10 marks)

Remark: You do not have to write precise numbers or even mathematical expressions for the answers above. Make sure you understand the behaviour qualitatively.

3    Naive Bayes Classier (40 marks)

You need to implement NBC on the Iris dataset. You can obtain this dataset as follows:

from sklearn.datasets import load iris

iris = load iris()

X, y = iris[‘data’], iris[‘target’]

There are three classes denoted by 0, 1, 2, which stand for Setosa, Versicolour and Virginica, three varieties of Iris. There are four features, all real-valued, measurements of sepal length and width, and petal length and width.

Experiment

1) You need to shuffle the dataset, put 20% aside for testing.

N, D = X.shape

Ntrain = int(0.8 * N)

shuffler = np.random.permutation(N)

Xtrain = X[shuer[:Ntrain]]

ytrain = y[shuffler[:Ntrain]]

Xtest = X[shuffler[Ntrain:]]

ytest = y[shuffler[Ntrain:]]

2) Train a Naive Bayes Classifier and report the training and testing errors.

3) Submit your code (a single .py le) and results (included in PDF).

Note:

You should implement a Na¨ıve Bayes Classifier directly in python.  Using the NBC from existing packages is not allowed.  To keep your code tidy, we recommend implementing it as a class.  The data has 4 different real-valued features, and there are 3 classes.  Write an implementation that you can initialise as follows:

nbc = NBC(feature types=[‘r’, ‘r’, ‘r’, ‘r’], num classes=3)

You need to implement two more functions, ht  and predict.  The t function should esti- mate all the parameters of the NBC. You should model all real-valued features as univariate Gaussians.

You also need to estimate the class probabilities, πc  = p(y = c). The predict function should compute the class conditional probabilities for the new inputs on all classes, and then return the class that has the largest one.

Implementation Issues

● Remember to do all the calculations in log space, to avoid running into underflow issues.

● As far as possible, use matrix operations.  So assume that Xtrain, ytrain, Xtest will all be numpy arrays. Try and minimise your use of python loops.  (In general, looping over classes is OK, but looping over data is probably not a good idea.)

● The variance parameter for Gaussian distributions should never be exactly 0, so in case your calculated variance is 0, you may want to set it to a small value such as 1e-6. Note that this is essential to ensure that your code never encounters division by zero or taking logarithms of 0 errors.

For training data Xtrain, ytrain and test data Xtest, ytest you should be able to run:

nbc.t(Xtrain, ytrain)

yhat = nbc.predict(Xtest)

test accuracy = np.mean(yhat == ytest)

Evaluation Criteria

● Can load dataset successfully.  (5 marks)

● Can correctly split train and test sets.  (5 marks)

● For implementation of function ht, can correctly compute πc .  (5 marks)

● For implementation of function ht, can correctly compute all conditionally distribu- tions of four features.  (10 marks)

● For implementation of function predict, can successfully obtain the prediction of all Xtest points.  (10 marks)

● Can correctly compute the nal training and testing accuracy.  (5 marks)