Question: Machine Learning Python Code. Problem 3: Using Gradient Descent for Perceptron Learning (10 code + 5 report = 15 points) For this problem, you will
Machine Learning Python Code.



Problem 3: Using Gradient Descent for Perceptron Learning (10 code + 5 report = 15 points) For this problem, you will training a perceptron, which has a squared loss function which is exactly the same as linear regression (See (1)), i.e., N 1 J(w) . wx;)2 (8) i=1 which means that you can call the same functions, regressionObjVal and regressionGradient, imple- mented in Problem 2, to train the perceptron. Implement two functions: 1. a testing function, predictLinearModel that returns the predictions of a model on a test data set 2. an evaluation function, evaluateLinearModel, that computes the accuracy of the model on the test data by calculating the fraction of observations for which the predicted label is same as the true label. REPORT 2 def predictLinearModel(w,Xtest): # Inputs: # W = d x 1 # Xtest N xd # Output: # ypred = N x 1 vector of predictions # IMPLEMENT THIS METHOD REMOVE THE NEXT LINE ypred np.zeros( [Xtest. shape [0],1]) return ypred def evaluateLinearModel(w,Xtest,ytest): # Inputs: # W = d x 1 # Xtest = N x d # ytest N x 1 # Output: # acc = scalar values REMOVE THE NEXT LINE # IMPLEMENT THIS METHOD acc = 0 return acc = In [22]: Xtrain, ytrain, Xtest, ytest pickle.load(open('sample.pickle', 'rb')) # add intercept Xtrain_i np.concatenate((np.ones ((Xtrain.shape [0],1)), Xtrain), axis=1) Xtest_i = np.concatenate((np.ones ((Xtest. shape [0],1)), Xtest), axis=1) W = args (Xtrain_i, ytrain) opts {'maxiter' : 50} # Preferred value. w_init = np.zeros((Xtrain_i. shape[1], 1)) soln = minimize(regressionObjVal, w_init, jac=regressionGradient, args=args, method='C', options=opts) np. transpose(np.array(soln.x)) W = w[:, np.newaxis] acc = evaluateLinearModel(w,Xtrain_i, ytrain) print('Perceptron Accuracy on train data %.2f'%acc) acc = evaluateLinearModel(w,Xtest_i,ytest) print('Perceptron Accuracy on test data %.2f'%acc)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
