Please refer to the code below:
import numpy as np
x = np.array([[2200,3,4] , [1500,4,2]])
y = np.array([50000 , 45000])
def Predict(x , w , b):
return np.dot(x , w) + b
def Compute_Cost(x , y , w , b):
m = x.shape[0]
cost = 0
for i in range(m):
tmp = y[i] - (Predict(x[i] , w ,b))
print(tmp , tmp**2 , math.pow(tmp , 2))
cost += tmp
return cost / (2*m)
Compute_Cost(x , y , [1 , 7 , 5] , 500)
The expected Output On the Console would be:
47259 2233413081.0 2233413081.0
42962 1845733444 1845733444.0
22555.25
But what I receive actually is:
47259 -2061554215 2233413081.0
42962 1845733444 1845733444.0
22555.25
The print statement should first print the value of a tmp variable, then print its square via the exponent operator then print the square via math.pow() function which should be the same however there seems to be an exception in the first print statement because how can a square of positive numbers be negative?
what is leading to such a big divergence while calculating the same thing but from different methods?
Python Version: 3.10.1
Numpy Version: 1.24.2
Since your tmp
variable is taken from an array, I'm assuming it is not a regular int, but a np.int32
, so it is subject to regular 32 bit overflow. Consider:
>>> import numpy as np
>>> np.int32(47259) ** 2
-2061554215
The result of the operation is too big to fit in 32 bits, so it overflows back to negative.
This is not happening in case of math.pow
. From the docs:
Unlike the built-in ** operator, math.pow() converts both its arguments to type float. Use ** or the built-in pow() function for computing exact integer powers.
Which you can see from the fact that the third print is a float (it ends in .0).
Floating point number can store much larger numbers than NumPy integers.