# Coursera: Machine Learning (Week 2) [Assignment Solution] - Andrew NG

I have recently completed the Machine Learning course from Coursera by Andrew NG.

While doing the course we have to go through various quiz and assignments.

Here, I am sharing my solutions for the weekly assignments throughout the course.

**These solutions are for reference only.**

**> It is recommended that you should solve the assignments by yourself honestly then only it makes sense to complete the course.**

**>**

**But, In case you stuck in between, feel free to refer to the solutions provided by me.**

####
**NOTE:**

Don't just copy paste the code for the sake of completion.

Even if you copy the code, make sure you understand the code first.

**Since there is NO assignment in week-1,**

**Let's start with the**

__week-2__assignment....In this exercise, you will implement linear regression and get to see it work on data. Before starting on this programming exercise, we strongly recommend watching the video lectures and completing the review questions for the associated topics.

**It consist of the following files:**

**ex1.m -**Octave/MATLAB script that steps you through the exercise**ex1 multi.m -**Octave/MATLAB script for the later parts of the exercise**ex1data1.txt -**Dataset for linear regression with one variable**ex1data2.txt -**Dataset for linear regression with multiple variables**submit.m -**Submission script that sends your solutions to our servers**[*] warmUpExercise.m -**Simple example function in Octave/MATLAB**[*] plotData.m -**Function to display the dataset**[*] computeCost.m -**Function to compute the cost of linear regression**[*] gradientDescent.m -**Function to run gradient descent**[#] computeCostMulti.m -**Cost function for multiple variables**[#] gradientDescentMulti.m -**Gradient descent for multiple variables**[#] featureNormalize.m -**Function to normalize features**[#] normalEqn.m -**Function to compute the normal equations**Video -**YouTube videos featuring Free IOT/ML tutorials

*****indicates files you will need to complete

**#**indicates optional exercises

### warmUpExercise.m :

function A = warmUpExercise() %WARMUPEXERCISE Example function in octave % A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix A = []; % ============= YOUR CODE HERE ============== % Instructions: Return the 5x5 identity matrix % In octave, we return values by defining which variables % represent the return values (at the top of the file) % and then set them accordingly. A = eye(5); %It's a built-in function to create identity matrix % =========================================== end

### plotData.m :

function plotData(x, y) %PLOTDATA Plots the data points x and y into a new figure % PLOTDATA(x,y) plots the data points and gives the figure axes labels of % population and profit. figure; % open a new figure window % ====================== YOUR CODE HERE ====================== % Instructions: Plot the training data into a figure using the % "figure" and "plot" commands. Set the axes labels using % the "xlabel" and "ylabel" commands. Assume the % population and revenue data have been passed in % as the x and y arguments of this function. % % Hint: You can use the 'rx' option with plot to have the markers % appear as red crosses. Furthermore, you can make the % markers larger by using plot(..., 'rx', 'MarkerSize', 10); plot(x, y, 'rx', 'MarkerSize', 10); % Plot the data ylabel('Profit in $10,000s'); % Set the y-axis label xlabel('Population of City in 10,000s'); % Set the x-axis label % ============================================================ end

### computeCost.m :

function J = computeCost(X, y, theta) %COMPUTECOST Compute cost for linear regression % J = COMPUTECOST(X, y, theta) computes the cost of using theta as the % parameter for linear regression to fit the data points in X and y % Initialize some useful values m = length(y); % number of training examples % You need to return the following variables correctly J = 0; % ====================== YOUR CODE HERE ====================== % Instructions: Compute the cost of a particular choice of theta % You should set J to the cost. %%%%%%%%%%%%% CORRECT %%%%%%%%% % h = X*theta; % temp = 0; % for i=1:m % temp = temp + (h(i) - y(i))^2; % end % J = (1/(2*m)) * temp; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%% CORRECT: Vectorized Implementation %%%%%%%%% J = (1/(2*m))*sum(((X*theta)-y).^2); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ========================================================================= end

### gradientDescent.m :

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters) %GRADIENTDESCENT Performs gradient descent to learn theta % theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by % taking num_iters gradient steps with learning rate alpha % Initialize some useful values m = length(y); % number of training examples J_history = zeros(num_iters, 1); for iter = 1:num_iters % ====================== YOUR CODE HERE ====================== % Instructions: Perform a single gradient step on the parameter vector % theta. % % Hint: While debugging, it can be useful to print out the values % of the cost function (computeCost) and gradient here. % %%%%%%%%% CORRECT %%%%%%% %error = (X * theta) - y; %temp0 = theta(1) - ((alpha/m) * sum(error .* X(:,1))); %temp1 = theta(2) - ((alpha/m) * sum(error .* X(:,2))); %theta = [temp0; temp1]; %%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%% CORRECT %%%%%%% %error = (X * theta) - y; %temp0 = theta(1) - ((alpha/m) * X(:,1)'*error); %temp1 = theta(2) - ((alpha/m) * X(:,2)'*error); %theta = [temp0; temp1]; %%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%% CORRECT %%%%%%% error = (X * theta) - y; theta = theta - ((alpha/m) * X'*error); %%%%%%%%%%%%%%%%%%%%%%%%% % ============================================================ % Save the cost J in every iteration J_history(iter) = computeCost(X, y, theta); end end

### computeCostMulti.m :

function J = computeCostMulti(X, y, theta) %COMPUTECOSTMULTI Compute cost for linear regression with multiple variables % J = COMPUTECOSTMULTI(X, y, theta) computes the cost of using theta as the % parameter for linear regression to fit the data points in X and y % Initialize some useful values m = length(y); % number of training examples % You need to return the following variables correctly J = 0; % ====================== YOUR CODE HERE ====================== % Instructions: Compute the cost of a particular choice of theta % You should set J to the cost. J = (1/(2*m))*(sum(((X*theta)-y).^2)); % ========================================================================= end

### gradientDescentMulti.m :

function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters) %GRADIENTDESCENTMULTI Performs gradient descent to learn theta % theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by % taking num_iters gradient steps with learning rate alpha % Initialize some useful values m = length(y); % number of training examples J_history = zeros(num_iters, 1); for iter = 1:num_iters % ====================== YOUR CODE HERE ====================== % Instructions: Perform a single gradient step on the parameter vector % theta. % % Hint: While debugging, it can be useful to print out the values % of the cost function (computeCostMulti) and gradient here. % %%%%%%%% CORRECT %%%%%%%%%% error = (X * theta) - y; theta = theta - ((alpha/m) * X'*error); %%%%%%%%%%%%%%%%%%%%%%%%%%% % ============================================================ % Save the cost J in every iteration J_history(iter) = computeCostMulti(X, y, theta); end end

###
**Check-out our free tutorials on IOT (Internet of Things):**

### featureNormalize.m :

function [X_norm, mu, sigma] = featureNormalize(X) %FEATURENORMALIZE Normalizes the features in X % FEATURENORMALIZE(X) returns a normalized version of X where % the mean value of each feature is 0 and the standard deviation % is 1. This is often a good preprocessing step to do when % working with learning algorithms. % You need to set these values correctly X_norm = X; mu = zeros(1, size(X, 2)); sigma = zeros(1, size(X, 2)); % ====================== YOUR CODE HERE ====================== % Instructions: First, for each feature dimension, compute the mean % of the feature and subtract it from the dataset, % storing the mean value in mu. Next, compute the % standard deviation of each feature and divide % each feature by it's standard deviation, storing % the standard deviation in sigma. % % Note that X is a matrix where each column is a % feature and each row is an example. You need % to perform the normalization separately for % each feature. % % Hint: You might find the 'mean' and 'std' functions useful. % mu = mean(X); sigma = std(X); X_norm = (X - mu)./sigma; % ============================================================ end

### normalEqn.m :

function [theta] = normalEqn(X, y) %NORMALEQN Computes the closed-form solution to linear regression % NORMALEQN(X,y) computes the closed-form solution to linear % regression using the normal equations. theta = zeros(size(X, 2), 1); % ====================== YOUR CODE HERE ====================== % Instructions: Complete the code to compute the closed form solution % to linear regression and put the result in theta. % % ---------------------- Sample Solution ---------------------- theta = pinv(X'*X)*X'*y; % ------------------------------------------------------------- % ============================================================ end

I tried to provide optimized solutions like

**vectorized implementation**for each assignment. If you think that more optimization can be done, then put suggest the corrections / improvements.

--------------------------------------------------------------------------------

Click here to see solutions for all **Machine Learning**Coursera Assignments.

&

Click here to see more codes for **Raspberry Pi 3**and similar Family.

&

Click here to see more codes for **NodeMCU ESP8266**and similar Family.

&

Click here to see more codes for **Arduino Mega (ATMega 2560)**and similar Family.

Feel free to ask doubts in the comment section. I will try my best to solve it.

If you find this helpful by any mean like, comment and share the post.

This is the simplest way to encourage me to keep doing such work.

Thanks and Regards,

**-Akshay P. Daga**

Have you got prediction values as expected?

ReplyDeleteYes. We got prediction values as expected.

DeleteMy program was successfully run.But after hitting submit and giving the token this error is showing please help

DeleteERROR--

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

100 1115 100 25 100 1090 12 554 0:00:02 0:00:01 0:00:01 558

error: structure has no member 'message'

error: called from

submitWithConfiguration at line 35 column 5

submit at line 45 column 3

error: evaluating argument list element number 2

error: called from

submitWithConfiguration at line 35 column 5

submit at line 45 column 3

>>

Submitting configuration is generally related that your directory is not right! Or it could also mean you didn't extract the file properly...it did happen with me at times

DeleteThanks for your comments. I still have some problems with the solutions, could you help me. In this case is with line 17, J History....

ReplyDeleteWeek 2

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)

%GRADIENTDESCENT Performs gradient descent to learn theta

% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by

% taking num_iters gradient steps with learning rate alpha

% Initialize some useful values

data = load('ex1data1.txt')

X = data(:,1)

y = data(:,2)

m = length(y)

x = [ones(m, 1), data(:,1)]

theta = zeros(2, 1)

iterations = 1500

alpha = 0.01

J = (1 / (2* m) ) * sum(((x* theta)-y).^2)

J_history = zeros(num_iters, 1)

for iter = 1:num_iters

% ====================== YOUR CODE HERE ======================

% Instructions: Perform a single gradient step on the parameter vector

% theta.

%

% Hint: While debugging, it can be useful to print out the values

% of the cost function (computeCost) and gradient here.

%

%error = (X * theta) - y;

%temp0 = theta(1) - ((alpha/m) * sum(error .* X(:,1)));

%temp1 = theta(2) - ((alpha/m) * sum(error .* X(:,2)));

%theta = [temp0; temp1];

% ============================================================

% Save the cost J in every iteration

J_history(iter) = computeCost(X, y, theta);

end

end

change the variable name of iteration.num_iters must be same with declared variable named iteration

DeleteCan you elaborate?

DeleteHi Can anyone help me. Just now i started ML. I am using Octave UI where i write the code but i don't know how to submit using UI. Can anybody please help me.

Deletehttps://www.youtube.com/watch?v=Vsg-cq7169U&feature=youtu.be

DeleteWatch this video by one of the mentors you will get it .

Thanks Hrishikesh, your comment might help many people.

Delete>> gradientDescent()

ReplyDeleteerror: 'y' undefined near line 7 column 12

error: called from

gradientDescent at line 7 column 3

>> computeCost()

error: 'y' undefined near line 7 column 12

error: called from

computeCost at line 7 column 3

How to correct this?

I tried to re-ran the code and everything worked perfectly fine with me.

DeletePlease check you code.

In the code, you can variable "y" is defined in parameter list itself.

So, logically you should not get that error.

There must something else you might be missing outside these functions.

I used to get the same error! i realized i have to execute ex1.m file and ex1_multi.m files to correct our code.

DeleteThank you for your response.

DeleteIt will be helpful for many others...

Hey @Akshay...I am facing same problem of 'y' undefined.

DeleteI tried all the ways suggested by you and by others can you please help me out.

Can u please tell which version of octave should i use for windows 8.1 64 bit,presently I am using 4.4.1 may be due to that I am facing this problem,please help

please tell how to execute ex1.m file in online MATLAB please help

DeletecomputeCost

ReplyDeleteerror: 'y' undefined near line 8 column 12

error: called from

computeCost at line 8 column 3

gradientDescent

error: 'y' undefined near line 7 column 12

error: called from

gradientDescent at line 7 column 3

How to correct this?

I tried to re-ran the code and everything worked perfectly fine with me.

DeletePlease check you code.

In the code, you can variable "y" is defined in parameter list itself.

So, logically you should not get that error.

There must something else you might be missing outside these functions.

If you got the solution please confirm here. It will be helpful for others.

Hi,

DeleteReceiving similar error. Found a solution?

Hello,

DeleteGot a similar error!

found the solution?

Hello,

DeleteGot a similar error!

found the solution?

Hi Sasank,

Deletebecause small y already is used as a input argument for the mentioned functions. So, you can't get the error like y is undefined.

Are you sure you haven't made any mistake like small y and Capital Y ?

Please check it and try again.

error: 'X' undefined near line 9 column 10

Deleteerror: called from

featureNormalize at line 9 column 8

anyone have find the solution? getting the same error from the program. i have try number of way but getting he same problem

DeleteI was stuck for two months in Week 2 Assignment of Machine Learning . Thanx for your guidance due to which I can now understand coding in a better way and finally I have passed 2nd Week Assignment.

ReplyDeleteGlad to know that my work helped you in understanding the topic / coding.

DeleteYou can also checkout free IOT tutorials with source codes and demo here: https://www.apdaga.com/search/label/IoT%20%28Internet%20of%20Things%29

Thanks.

I tried to reran the code. But i am getting error this:

ReplyDeleteerror: 'num_iters' undefined near line 17 column 19

error: called from

gradientDescent at line 17 column 11

how to correct this??

i m also facing the same problem, plz help me to out of the problem

Deletei am also submitting these assignments . i have also done the same . But i dont know where to load data .thus my score is 0. how can i improve? please suggest me.

ReplyDeleteRefer the forum within the course in Coursera.

DeleteThey have explained the step to submit the assignments in datails.

Hello ,

ReplyDeleteIn the gradient descent.m file : theta = theta - ((alpha/m) * X'*error);

I m confused, why do we take the transpose of X (X'*error) instead

of X ?

Thanks in advance

B

Hi Bruno,

DeleteI got your confusion, Here X (capital X) represent all the training data together, each row as one training sample, each column as a feature. We want to multiply each training data with corresponding error. To make it happen you have to transpose of X (capital X).

if you take x (small x) as single training sample then you don't have to worry about transpose and all.

Simply (x * error) will work.

Try to do it manually on a notebook. You will understand it.

Hi Akshay

DeleteThank you for the quick reply & help ...It s totally clear now, make sense !!!

Have a great day

Bruno

Good day,please am kind of stuck with week2 programming assignment,and this is under computecost.m

ReplyDeleteI already imported the data and plotted the scatter plot.Do I also after to import the data in computecost.m working area,and and when I just in inputted the code J = (1/(2*m))*(sum(((X*theta)-y).^2)); I got an error message.please how do I fix this.

Thanks

What error you got?

DeleteplotData

ReplyDeleteerror: 'X' undefined near line 20 column 6

error: called from

plotData at at line 20 column 1

What is the solution to this?

Hi Amit, As I checked I have used small x as an input argument for plotData function.

Deleteand in your error there is capital X.

Are you sure you haven't made mistake in small and capital X?

Please check and try once again.

i can see you have used a X there not x,still showing the error saying not enough input arguments

DeleteHey Akshay, The error 'y' undefined problem do exist, but it is not othe problem only for the code you gave,any solution the internet gives that error.Even running through gui or through command, it says undefined.There is no clear solution for this on the net, I tried adding path too as it was said in the net.Couldnt solve the issue.I have octave 5.1.0

ReplyDeleteI found the solition for those who were getting u defi ed error.

Deleteif you are using octave

then

the file shouldnot first start with function, octave takes it as a function, not as a script.

solution

add anything to first line

example

add 1; first line and then start function.

If you wanna test your function when you run, first initialize the variables to matrices and respective values.

then pass these as parameters to the function.

Thanks Chethan,

DeleteIt will be a great help for others as well.

I didn't understand.can u explain clearly

Deleteinclude two lines of code

Deletex=[];

y=[];

This should work

Its still not working. I'm getting:

Deleteerror: 'y' undefined near line 7 column 12

error: called from

computeCost at line 7 column 3

Hi Akshay,

ReplyDeleteI am getting error like this

m=lenght(y); %number of training example

Can you help me

Thanks

This comment has been removed by a blog administrator.

ReplyDeleteHello, within gradientDescent you use the following code

ReplyDeleteerror = (X * theta) - y;

theta = theta - ((alpha/m) * X'*error)

What is the significance of 'error' in this? Within Ng's lectures I can't remember him making reference to 'error'

Error is similar to that of "cost" (J)

Delete!! Submission failed: 'data' undefined near line 19 column 18

ReplyDeleteFunction: gradientDescent

FileName: C:\Users\Gunasekar\Desktop\GNU Oct\machine-learning-ex1\ex1\gradientDescent.m

LineNumber: 19

Please correct your code and resubmit.

This is my problem how to correct it

Hi, I think you are doing this assignment in Octave and that's why you are facing this issue.

DeleteChethan Bhandarkar has provided solution for it. Please check out the comment by Chethan Bhandarkar: https://www.apdaga.com/2018/06/coursera-machine-learning-week-2.html?showComment=1563986935868#c4682866656714070064

Thanks

Code that is given is not running as always give error 'y' undefined near line 7 column 12 for every code.

ReplyDeleteHi, I think you are doing this assignment in Octave and that's why you are facing this issue.

DeleteChethan Bhandarkar has provided solution for it. Please check out the comment by Chethan Bhandarkar: https://www.apdaga.com/2018/06/coursera-machine-learning-week-2.html?showComment=1563986935868#c4682866656714070064

Thanks

did the same as of chethan said but still the issue is not resolved getting the same error y not defined

Delete@Shilp, I think, You should raise your concern on Coursera forum.

Delete>> gradientDescent()

ReplyDeleteerror: 'y' undefined near line 7 column 12

error: called from

gradientDescent at line 7 column 3

>> computeCost()

error: 'y' undefined near line 7 column 12

error: called from

computeCost at line 7 column 3

i am getting this kind of error how to solve this

hey

ReplyDeletei think the errors related to undefined variables are due to people not passing arguments while calling the func from octave window. Can you post an example of command to run computeCost with arguments

the Predicted prices using normal equations and gradient descent are not equals

ReplyDelete(NE price= 293081.464335 and GD price=289314.62034) is it correct ?

For compute.m function, i am continuosly getting below error message:

ReplyDeleteError in computeCost (line 31)

J = (1/(2*m))*sum(((X*theta)-y).^2);

What error you are getting exactly?

Deletewhat is the predicted value of house..mine it is getting $0000.00 with huge theta value how is that possible?

ReplyDeleteOk so for the people facing problem regarding y is undefined error.....you can directly submit the program it tests ex1.m file as a whole and it is compiled successfully and gives the correct answer

ReplyDeletehow can i directly submit the ex1.m file?

DeleteplotData

ReplyDeleteNot enough input arguments.

Error in plotData (line 19)

plot(x, y, 'rx', 'MarkerSize', 10); % Plot the data

I got this error. how can I solve this?

not enough input arguments.

ReplyDeleteError in computeCost (line 7)

m = length(y); % number of training examples

I got the same error. have you found out a solution yet?

DeleteHi, I am getting the same error and the program doesn't give the solution. Please advise.

ReplyDeleteHaving problems with nearly everyone of these solutions. I am 12 and learning machine learning for the first time and having troubles referring to this as i find these solutions do not work. Any help?

ReplyDeleteHello I am stuck in WK2 PlotData

ReplyDeleteI keep getting errors: >> Qt terminal communication error: select() error 9 Bad file descriptor like that one

or

error: /Users/a69561/Desktop/machine-learning-ex1/ex1/plotData.m at line 19, column 3

Can somebody help me ??

thank you for the solution but i m still getting 2 different values of price of house( with normal equation and gradient descent method)

ReplyDeletehi i have same problem as undefined. Please help me, please. I am using in the octave. Any other way to submit the programming assignment. Please help?

ReplyDeleteWhats is your leaning rate alpha and number of iterations?

ReplyDeleteI have provided only function definitions here.

DeleteYou can find the parameter (alpha, num of iterations) values in execution section of your assignment.

In Linear regression with multiple variables by 1st method ( gradientDescent method) the price value of the house was different whn compared to the 2nd method(Normal Equations).still not able to match the values of both the methods ?

ReplyDeleteNote : i have copied all the code as per your guidance.

hi, thanks for all your help. But I have some problem in submission. When I finished all work, I tried to submit all in once and got this:

ReplyDelete>> submit

Warning: Function Warning: Name is nonexistent or not a directory: /MATLAB Drive/./lib

> In path (line 109)

In addpath (line 86)

In addpath (line 47)

In submit (line 2)

Warning: Function Warning: Name is nonexistent or not a directory: /MATLAB Drive/./lib/jsonlab

> In path (line 109)

In addpath (line 86)

In addpath (line 47)

In submitWithConfiguration (line 2)

In submit (line 45)

'parts' requires one of the following:

Automated Driving Toolbox

Navigation Toolbox

Robotics System Toolbox

Sensor Fusion and Tracking Toolbox

Error in submitWithConfiguration (line 4)

parts = parts(conf);

Error in submit (line 45)

submitWithConfiguration(conf);

>> submit

>> submitWithConfiguration

Warning: Function Warning: Name is nonexistent or not a directory: /MATLAB Drive/./lib/jsonlab

> In path (line 109)

In addpath (line 86)

In addpath (line 47)

In submitWithConfiguration (line 2)

'parts' requires one of the following:

Automated Driving Toolbox

Navigation Toolbox

Robotics System Toolbox

Sensor Fusion and Tracking Toolbox

Error in submitWithConfiguration (line 4)

parts = parts(conf);

Check if your are in the same directory ex1 folder and to submit the solution use ''submit()'' not submit add parenthesis

DeleteThis is happening because variable parts has the same name as of parts(conf) function in file ex1/lib/submitWithConfiguration.m

DeleteMake the following changes to resolve this :

Line 4 - parts_1 = parts(conf);

Line 92 - function [parts_1] = parts(conf)

Line 93 - parts_1 = {};

Line 98 - parts_1{end + 1} = part;

Basically, I've just renamed the variables.

Same thing is happening with one more variable, so make the following changes :

Line 66 - submissionUrl_1 = submissionUrl();

Line 68 - responseBody = getResponse(submissionUrl_1, body);

Line 22: response = submitParts(conf, email, token, parts_1);

Line 37: showFeedback(parts_1, response);

This worked for me.

Hello Akshay,

ReplyDeleteIn computeCost, how to declate or compute 'theta' because, it's giving an error - 'theta' undefined.

error: structure has no member 'message'

ReplyDeleteerror: called from

submitWithConfiguration at line 35 column 5

submit at line 45 column 3

error: evaluating argument list element number 2

error: called from

submitWithConfiguration at line 35 column 5

submit at line 45 column 3

how to solve this

Hello Akshay Daga (APDaga,

ReplyDeleteVery glad to come across your guide on ML by Andred NG.

I been stuck months, could complete the Programming Assisgment.

Have done up to computeCost but got stuck at gradientDescent

Below is the error. I don't want to drop out of this course please help me out.

"error: 'num_iters' undefined near line 1 column 58"

Here is my update

h=(theta(1)+ theta(2)*X)';

theta(1) = theta(1) - alpha * (1/m) * theta(1) + theta(2)*X'* X(:, 1);

theta(2) = theta(2) - alpha * (1/m) * htheta(1) + theta(2)*X' * X(:, 2);

I count on your assistance.

Hello Akshay Daga (APDaga,

ReplyDeleteVery glad to come across your guide on ML by Andred NG.

I been stuck months, could complete the Programming Assisgment.

Have done up to computeCost but got stuck at gradientDescent

Below is the error. I don't want to drop out of this course please help me out.

"error: 'num_iters' undefined near line 1 column 58"

Here is my update

h=(theta(1)+ theta(2)*X)';

theta(1) = theta(1) - alpha * (1/m) * theta(1) + theta(2)*X'* X(:, 1);

theta(2) = theta(2) - alpha * (1/m) * htheta(1) + theta(2)*X' * X(:, 2);

I count on your assistance.

gradientDescent()

ReplyDeleteerror: 'y' undefined near line 7 column 14

error: evaluating argument list element number 1

error: called from:

error: /Users/apple/Downloads/machine-learning-ex1/ex1/gradientDescent.m at line 7, column 5

I am getting this error for both gradient descent and computeCost. plz helpme out

Hi, I think you are doing this assignment in Octave and that's why you are facing this issue.

DeleteChethan Bhandarkar has provided solution for it. Please check out the comment by Chethan Bhandarkar: https://www.apdaga.com/2018/06/coursera-machine-learning-week-2.html?showComment=1563986935868#c4682866656714070064

Thanks

function [theta, J_history] = gradientDescent(X, y, theta, alpha, iterations)

ReplyDelete%GRADIENTDESCENT Performs gradient descent to learn theta

% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by

% taking num_iters gradient steps with learning rate alpha

% Initialize some useful values

m = length(y); % number of training examples

h=X*theta;

error=(h-y);

theta_c=(alpha/m)*(sum((error)*X'));

theta=theta-theta_c;

J_history = zeros(num_iters, 1);

for iter = 1:iterations

% ====================== YOUR CODE HERE ======================

% Instructions: Perform a single gradient step on the parameter vector

% theta.

%

% Hint: While debugging, it can be useful to print out the values

% of the cost function (computeCost) and gradient here.

%

% ============================================================

% Save the cost J in every iteration

J_history(iter) = computeCost(X, y, theta);

end

end

while running on octave it's showing

Running Gradient Descent ...

error: gradientDescent: operator *: nonconformant arguments (op1 is 97x1, op2 is 2x97)

error: called from

gradientDescent at line 10 column 8

ex1 at line 77 column 7

where is the problem???

i got an error in computeCost.m as following:

ReplyDeletemax_recursion_depth reached.

How to solve this?

i got an error as:

ReplyDeleteerror: computeCost: operator /: nonconformant arguments (op1 is 1x1, op2 is 1x2)

How to solve this?

I can't see any variable used in codes as op1 or op2.

DeletePlease check once again where did you get those variables from?

Hi, great guidance. Only, I still have the confusion how single parameter costfunction and multi parameter costfunction codes are same? (same confusion for both gradientdescent (single and multi).Am I missing something?

ReplyDeletesingle parameter costfunction is as follows:

Deleteh = X*theta;

temp = 0;

for i=1:m

temp = temp + (h(i) - y(i))^2;

end

J = (1/(2*m)) * temp;

Which doesn't work for multi parameter costfunction.

But, I have also provided vectorized implementation. (It is generic code and works for both single as well as multi parameters).

Hello,

ReplyDeleteI am getting x is undefined while submitting plotData in assignmnet2 several times I checked But I am getting the same error will u please help me?

function plotData(x, y)

ReplyDeleteplot(x, y, 'rx', 'MarkerSize', 10);

ylabel('Profit in $10,000s');

xlabel('Population of City in 10,000s');

Always I am getting x is undefined.I cant able to understand where the error is plzz help me??

figure;

function plotData(x, y)

ReplyDeleteplot(x, y, 'rx', 'MarkerSize', 10);

ylabel('Profit in $10,000s');

xlabel('Population of City in 10,000s');

figure;

Always I am getting x is undefined.I cant able to understand where the error is plzz help me??

Thanks

While doing in matlab also it is saying error in submitwithconfiguration in submit.m file accutally it was defaultly givern by them but why it is show error there???

DeleteWhile doing in matlab it is saying error in submitwithconfiguration in submit.m file accutally it was defaultly givern by them but why it is show error there???

ReplyDeleteStill the same problem with undefined y (small letter) using Octave 5.2.0

ReplyDeleteadding anything as first line didn't help

What could I do else? Has somebody working version. I got stuck in this point

instead of running codes individually, run 'ex1' after completing all the problems....then it will not show any error

DeleteHi..

ReplyDeleteI am using MATLAB R2015a version offline and getting error submitwithconfiguration(line 158).How to rectify this error??

Raise this concern in Coursera forum.

Deleteif you implement featureNormalize this way, it gives dimensions disagreement error so i suggest it would be better to do it in the following way;

ReplyDeletemu = ones(size(X,1),1)* mean(X);

sigma = ones(size(X,1),1)* std(X);

X_norm = (X - mu)./(sigma);

P.S: it gives me accurate results

I entered submit () ,but I geeting error so pls help to how to submit my assignment

ReplyDeleteI think you should raise this concern to Coursera forum.

Deletetry just submit without the brackets.

Deleteur code is not working when i use it

ReplyDeleteSorry to know that. But I was working 100% for me and some others as well.

Deletenum_iters not defined error.. Plz help

ReplyDeletejust got the answer for num_iters not defined...You have to fix line 59 in submit.m

ReplyDeleteI have a problem running the below line of code:

ReplyDelete(X * theta) - y;

it gives error: operator *: nonconformant arguments (op1 is 97x1, op2 is 2x1)

I can understand because X is a 97x1 matrix and cannot be multiplied with a 2x1 matrix. Any ideas?

I get the below error when executing ex1 for testing the gradientDescent function:

ReplyDeleteerror: computeCost: operator *: nonconformant arguments (op1 is 97x2, op2 is 194x1)

error: called from

computeCost at line 15 column 2

gradientDescent at line 36 column 21

ex1 at line 77 column 7

My gradientDescent function has the below lines of code as per the tutorial.

temp0 = theta(1) - ((alpha/m) * sum((X * theta) - y) .* X(:,1));

temp1 = theta(2) - ((alpha/m) * sum((X * theta) - y) .* X(:,2));

theta = [temp0; temp1];

My computeCost function has this line of code on line number 15:

J=1/(2*m)*sum(((X*theta)-y).^2)

NB: surprisingly I can run the gradientDescent lines individually on octave command without problems

Hey, how do you calculate the value of theta?

ReplyDeleteThe values of theta1 and theta2 are initially set to 0, theta = zeros(2,1)

DeleteGetting an error, theta is undefined...

ReplyDeleteI get the below error when executing ex1 for Submitting the gradient Descent:

ReplyDelete>> submit

'parts' requires one of the following:

Automated Driving Toolbox

Navigation Toolbox

Robotics System Toolbox

Sensor Fusion and Tracking Toolbox

Error in submitWithConfiguration (line 4)

parts = parts(conf);

Error in submit (line 45)

submitWithConfiguration(conf);

did you get this answer? , I see this error

Deletesome of these answers are incorrect. for example the feature normalization question is wrong. when you calculate X-u /sigma X and u are different dimensions so it doesn't work.

ReplyDeleteThanks for the feedback.

DeleteAll these answers worked 100% for me. and they are working fine for many of others as well (you can get idea from comments.)

But coursera keeps on updating the assignments time to time. So, You might be right in that case.

Please use above codes just for reference and then solve your assignment on your own.

Thanks

hello brother, can you please briefly explain the working in these two lines of GD

ReplyDeleteerror = (X * theta) - y;

theta = theta - ((alpha/m) * X'*error)

ReplyDeleteHow can I solve this problem??

submit

'parts' requires one of the following:

Automated Driving Toolbox

Navigation Toolbox

Robotics System Toolbox

Sensor Fusion and Tracking Toolbox

Error in submitWithConfiguration (line 4)

parts = parts(conf);

Error in submit (line 45)

submitWithConfiguration(conf);

same problem here

ReplyDeleteHi, when I run my code, the predicted price of the house (in ex1_multi.m), it says 0.0000. How can I fix that?

ReplyDelete>> [Xn mu sigma] = featureNormalize([1 ; 2 ; 3])

ReplyDeleteerror: Invalid call to std. Correct usage is:

-- std (X)

-- std (X, OPT)

-- std (X, OPT, DIM)

error: called from

print_usage at line 91 column 5

std at line 69 column 5

featureNormalize at line 32 column 8

>>

Even after I am doing it the right way i hope:

'''

mu = mean(X);

sigma = std(X, 1);

X_norm = (X - mu) ./ std;

'''

Anyone any idea, why i am facing this error?

I tried simply this also:

Deletesigma = std(X);

>> submit()

ReplyDelete'parts' requires one of the following:

Automated Driving Toolbox

Navigation Toolbox

Robotics System Toolbox

Sensor Fusion and Tracking Toolbox

Error in submitWithConfiguration (line 4)

parts = parts(conf);

Error in submit (line 45)

submitWithConfiguration(conf);

This is happening because variable parts has the same name as of parts(conf) function in file ex1/lib/submitWithConfiguration.m

DeleteMake the following changes to resolve this :

Line 4 - parts_1 = parts(conf);

Line 92 - function [parts_1] = parts(conf)

Line 93 - parts_1 = {};

Line 98 - parts_1{end + 1} = part;

Basically, I've just renamed the variables.

Same thing is happening with one more variable, so make the following changes :

Line 66 - submissionUrl_1 = submissionUrl();

Line 68 - responseBody = getResponse(submissionUrl_1, body);

Line 22: response = submitParts(conf, email, token, parts_1);

Line 37: showFeedback(parts_1, response);

This worked for me

which is better to use to submit assignments ( Octave or Matlab)... The solutions that have been provided are for Matlab or Octave?

ReplyDeleteI have provided solution in MATLAB but It works in Octave as well.

Deletehi I don't understand why X*theta . I mean theta is a 2X1 vector right? I understand the formula, but i get confused in this exercise.

ReplyDeleteI figure it out because I thought X is a 97x1 vector. I have another question. Is this a gradient descent with one variable? I thought it is two variables? Does the theta0 count 1 variable?

Delete