Course1_week2_Assignment2_Ex5_Propagate

Error:-

TypeError Traceback (most recent call last)
in
3 X =np.array([[1., 2., -1.], [3., 4., -3.2]])
4 Y = np.array([[1, 0, 1]])
----> 5 grads, cost = propagate(w, b, X, Y)
6
7 assert type(grads[“dw”]) == np.ndarray

in propagate(w, b, X, Y)
30 # YOUR CODE STARTS HERE
31 A = sigmoid(np.dot(w.T,X)+b)
—> 32 cost = (-1/m)*(np.sum(Y * np.log(A),(1-Y) * np.log(1-A),keepdims=True))
33
34 # YOUR CODE ENDS HERE

<array_function internals> in sum(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/fromnumeric.py in sum(a, axis, dtype, out, keepdims, initial, where)
2227
2228 return _wrapreduction(a, np.add, ‘sum’, axis, dtype, out, keepdims=keepdims,
→ 2229 initial=initial, where=where)
2230
2231

/opt/conda/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
88 return reduction(axis=axis, out=out, **passkwargs)
89
—> 90 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
91
92

TypeError: only integer scalar arrays can be converted to a scalar index

Please help as soon as possible
Thanks

Hi, I think there is an error in the formula you are using to calculate the cost. As you can see below what you are trying to do won’t work.

>>> x = np.array([0, 1, 2])
>>> y = np.array([2, 4, 5])
>>> np.sum(x, y, keepdims=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in sum
  File "D:\anaconda\envs\tf2\lib\site-packages\numpy\core\fromnumeric.py", line 2247, in sum
    return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,
  File "D:\anaconda\envs\tf2\lib\site-packages\numpy\core\fromnumeric.py", line 87, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
TypeError: only integer scalar arrays can be converted to a scalar index

However, this will work:

>>> np.sum(x+y, keepdims=True)
array([14])

In any case, could you please remove you solution from your post?

Thanks a lot!! That helped in rectifying the error.