I think your cost code should work. You don’t need the np.sum call there, but I think it will give the correct answer. If I’m interpreting what you are saying correctly, I think you pass the test case for propagate, but fail the test for optimize. In the test case output that you show, notice that the first cost value agrees, but it is the second one that does not. That indicates that your cost code is correct, but that the problem is with some other aspect of the optimize logic. E.g. the “update parameters” portion may be wrong.
Doh! My w and b updates were incorrect. (I spent so much time validating my cost function.) I was multiplying the learning rate by w and b when I should have been multiplying them by the derivatives. Thanks for pointing me in the right direction.