Cost Function Linear Regression trying to avoid hard coding theta Octave

0 votes

I'm in the second week of Professor Andrew Ng's Machine Learning course through Coursera. We're working on linear regression and right now I'm dealing with coding the cost function.

The code I've written solves the problem correctly but does not pass the submission process and fails the unit test because I have hard coded the values of theta and not allowed for more than two values for theta.

Here's the code I've got so far

function J = computeCost(X, y, theta)

m = length(y);
J = 0;

for i = 1:m,
    h = theta(1) + theta(2) * X(i)
    a = h - y(i);
    b = a^2;
    J = J + b;
    end;
J = J * (1 / (2 * m));

end

the unit test is

computeCost( [1 2 3; 1 3 4; 1 4 5; 1 5 6], [7;6;5;4], [0.1;0.2;0.3])

and should produce ans = 7.0175

So I need to add another for loop to iterate over theta, therefore allowing for any number of values for theta, but I'll be damned if I can wrap my head around how/where.
Can anyone suggest a way I can allow for any number of values for theta within this function? If you need more information to understand what I'm trying to ask, I will try my best to provide it.

Mar 26, 2022 in Machine Learning by Dev
• 6,000 points
595 views

1 answer to this question.

0 votes

In Octave/Matlab, you can use vectorize operations. Iterate over the full vector - if your programming language allows you to vectorize operations, this is a bad idea. This is possible with R, Octave, Matlab, and Python (numpy). If theta = (t0, t1, t2, t3) and X = (x0, x1, x2, x3), you can get scalar production in the following way: t0*x0 + t1*x1 + t2*x2 + t3*x3' = theta * X' = (t0, t1, t2, t3) * (x0, x1, x2, x3)' = t0*x0 + t1*x1 + t2*x2 + t3*x3' = The outcome will be scalar.

You can vectorize h in your code in the following fashion, for example:

H = (theta'*X')';
S = sum((H - y) .^ 2);
J = S / (2*m);
answered Apr 4, 2022 by Nandini
• 5,480 points

Related Questions In Machine Learning

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

What is LassoLars? - Linear regression

LassoLars is a lasso model implemented using ...READ MORE

answered May 22, 2019 in Machine Learning by Basu
2,408 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP