# Week 3 Cost Function

## I am getting following Error for Cost FUnction, can anyobidy guide me what am I doing wrong.

TypeError Traceback (most recent call last)
in
1 A2, t_Y = compute_cost_test_case()
----> 2 cost = compute_cost(A2, t_Y)
3 print("cost = " + str(compute_cost(A2, t_Y)))
4
5 compute_cost_test(compute_cost)

in compute_cost(A2, Y)
21 # cost = β¦
22 # YOUR CODE STARTS HERE
β> 23 cost = np.multiply(-1/m,np.sum(np.dot(np.log(A2),Y.T),np.dot(np.log(1-A2),(1-Y).T)))
24 # YOUR CODE ENDS HERE
25

<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

I think the problem is that the argument you are feeding to np.sum is a comma separated list of two inputs. What you really want to do is add the output of those two inner np.dot calls. You donβt need np.sum to do that: just use +. The point of np.sum is that it takes a single vector or array and adds the contents of that input.

1 Like