C1_W2_Linear_Regression (error:The truth value of an array with more than one element is ambiguous)

hi, i have a problem in my code for practice in week2
the error is :

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

i think my code is true , But it returns several numbers instead of one , I can’t solve the problem, can you help me?

Hi @Bita_Bayat What exercise are you doing?

Please post a screen capture image that shows the entire error message stack.
You haven’t identified which function you are working on.

it’s programming assignment in supervised machine learning in week2 :slight_smile:

There is an error in your compute_cost() function. The value it returns must be a scalar. Yours is a vector.


thanks , could you help me how can do i it?

Hi @Bita_Bayat

First you want to compute an y-hat(prediction = XW+b) after that you should subtract y from y-hat and doing doing mean square error(y - y-hat)**2 after that you got a vector you should doing sum of this vector by for example output =np.sum((y - y-hat)**2 ) or by for loop , finally you should divide this output by 2m (number of samples) to get the mean error
this photo show you the implementation in mathematical notations

note(there are many techniques you can do this cost function like using vectorizations or using for loop …etc)
please feel free to ask any questions,

1 Like

thanks a lot. i find the problem but i get different answer :

It’s interesting that you’re getting the message “All tests passed!”, which means the tests in compute_cost_test() passed, but you’re getting a different number than expected when you call

cost = compute_cost(x_train, y_train, initial_w, initial_b)

Is it possible you accidentally changed the values of initial_w or initial_b? It should be:

initial_w = 2
initial_b = 1
1 Like

yes , i think changed it accidentally :sweat_smile:
i do it with initial_b = 2

1 Like