0

can someone please tell me the difference between these following two notations, one is working, the other one isn't but they seem the same to me.

def GradientDescentCostTest():

    theta = numpy.array([0, 0, 0], numpy.float)
    features = numpy.array([[80, 20, 0], [65, 30, 1], [70, 23, 1]], numpy.float)
    values = numpy.array([20, 10, 14], numpy.float)    

    # This works and returns a value for cost:
    sumOfSquareErrors = numpy.square(numpy.dot(features, theta) - values).sum()
    cost = sumOfSquareErrors/(len(values)*2)

    # This doesn't work and returns value 0.0 for cost:
    cost = 1/(len(values)*2) * numpy.square(numpy.dot(features, theta) - values).sum()    

    return cost

print GradientDescentCostTest()

Thanks

3
  • 1
    sure can you provide enough to make your samples runnable (ie an example for all the undefined variables) Commented Feb 23, 2015 at 22:13
  • I learned BODMAS at school. Commented Feb 23, 2015 at 22:14
  • added a full code example now, thanks Commented Feb 23, 2015 at 22:23

1 Answer 1

3

1/(len(values)*2) is equal to 0 because all operations are on integers. A simple fix is to instead use 1.0/(len(values)*2).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.