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