"nan" value for cost at test w,b


I really appreciate any comment on this error.


Perhaps your ‘i’ index value is invalid?

1 Like

Note that if you’re doing this assignment by using for-loops, you’re working excessively hard.

The np.dot(), np.log(), and np.sum() functions will make quick work of computing the cost without using any loops, in just a couple of lines of code.

1 Like

There are potentially two errors. 1) “nan” and 2) “index 1 is out of bounds for axis 0 with size 1”.

“nan” is caused by an error in calculating a loss including sigmoid step. You may want to check with print statements to see the exact location that caused “nan”.
The 2nd one is from the hidden test with w = np.array([0]). Along with this definition, the shape of X is now (4,1). (On the other hand, the shape of a global variable X_train is (100,2)).
So, index 1 for “j” is out of bounds, since “n” should be 1 in this case. And, “n” comes from the 2nd axis of X. If you properly get it from X, then, there shouldn’t be any problem. Did you hard-code or use X_Train to get this shape ? Please double check.

And, as Tom suggested, using a vector operation is preferable from the computational speed with less complexity. But, I suppose, the guide (hint) is to use a nested for loop… So, you may want to go with a nested for loop approach.

1 Like

Thanks for your help. I solved the problem.

Thanks for your detailed description. I solved the problem.