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 166 views

## 1 answer to this question.

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);```
• 5,480 points

## Can someone explain to me the difference between a cost function and the gradient descent equation in logistic regression?

when we train a model with data, ...READ MORE

## Alternatives to linear regression for dataset with many points with small value and some extreme values

The above situation is the case where ...READ MORE

## How to resolve heteroscedasticity in Multiple Linear Regression in R?

Try to use a different form of ...READ MORE

## Is there a way to force the coefficient of the independent variable to be a positive coefficient in the linear regression model used in R?

A Few Constraints This is an example of ...READ MORE

## Python code for basic linear regression

Hi @Dipti, you could try something like ...READ MORE

## Show python implementation of Lasso class - regression

Hey @Tanmay, try something like this: >>> from ...READ MORE

## What is LassoLars? - Linear regression

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

## Can you give LassoLars python example?

Hey @Vivek, Try something like this: >>> from ...READ MORE