Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We now will compare the normal equation method from problem 7 to gradient descent with the cost function: J()=2n1i=1n(h(x(i))y(i))2, where h=Tx=0x0+1x1. Before you can implement

image text in transcribed

We now will compare the normal equation method from problem 7 to gradient descent with the cost function: J()=2n1i=1n(h(x(i))y(i))2, where h=Tx=0x0+1x1. Before you can implement gradient descent, you need a way to track your performance, which computes J(). Write a matlab function computeCost.m, that takes in three arguments: X Xdata, size nD, Theta, size D1, Y, size n1, where n is the number of samples, and D is the dimension of the sample plus 1 (the plus 1 accounts for the constant column equal to 1 : X will be 972 in this example); and returns a single overall cost. The function cannot include any loops (it can be done with one line of code). Show the code here. Do not continue until you get a cost value of 32.0727. clear ; data =load( 'ex1data1.txt'); \% Dataset from Andrew Ng, Machine Learning MOOC X=data(:,1) y=data(:,2); Xdata =[ ones ( length (X),1)X] theta =zeros(2,1);% initialize fitting parameters to zero computeCost(Xdata,y,theta); Given the computeCost.m from the previous problem, you can now implement gradient descent using the given function gradientDescentLinear.m. Use the following matlab code: clear ; close all; data = load('ex1data1.txt'); \% Dataset from Andrew Ng, Machine Learning MOOC X=data(:,1); y=data(:,2) M=[ ones(length (X),1)X] theta_init = zeros (2,1);% initialize fitting parameters to zero % Some gradient descent settings iterations =1500 alpha =0.01; % run gradient descent theta = gradientDescentLinear(M, y, theta_init, alpha, iterations); Do not continue until you get a theta value of [-3.6303 1.1664]. For this problem, plot the linear regression line that results from gradient descent as a blue solid line, linewidth =3. In the same figure plot also as a green line your result from problem 7, where you did linear regression using the normal equations method. Show code and include plot here x-axis should go from 0 to 25 and y-axis should go from -5 to 25 (Note: part of your code will repeat your solution to problem 7, to get the results using the normal equations method)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Informix Database Administrators Survival Guide

Authors: Joe Lumbley

1st Edition

0131243144, 978-0131243149

More Books

Students also viewed these Databases questions

Question

3. Identify cultural universals in nonverbal communication.

Answered: 1 week ago

Question

2. Discuss the types of messages that are communicated nonverbally.

Answered: 1 week ago