my cost function goes to infinity and I don't know what is the problem, the dataset has 5 variables to predict the house prices and I removed the null rows. the dataset had 20k row then reduced to 8k and still has this problem, should I reduce the variables?
and is this the right way to slice a dataframe and change it an array to implement the functions on it ?
x = df.iloc[:, 1:6]
x = np.array(x.to_numpy())
y = df.iloc[:,0]
y = np.array(y.to_numpy())
`
def compute_cost(x, y, w, b):
# fw_b = wn * xn + b , wn and xn are arrays
# cost function = 1 / 2m sum ( y(Prediction) - y )^2
cost = 0.0
m = x.shape[0]
for i in range(m):
f_wb_i = np.dot(x[i],w) + b #(n,)(n,) = scalar (see np.dot)
cost = cost + (f_wb_i - y[i])**2 #scalar
print(cost)
cost *= 1/(2 * m) #scalar
return cost
`
`
# Compute cost with some initial values for paramaters w, b ,x = [ bedroom , bathroom , sqft living , sqft lot , floors']
initial_w = np.zeros(x.shape[1])
initial_b = 0.
cost = compute_cost(x, y , initial_w, initial_b)
print(f'Cost at optimal w : {cost}')
`
Output exceeds the size limit. Open the full output data in a text editor
49239610000.0
387923220000.0
808246440000.0
1981308880000.0
4222717760000.0
9958335520000.0
19982977290000.0
40051131002500.0
80154932255000.0
160414193510000.0
321267293270000.0
642753610540000.0
1285603321080000.0
2571366642160000.0
5143014184320000.0
1.028645086864e+16
2.057305776228e+16
4.114635074956e+16
8.229275439912e+16
1.6458565702324e+17
3.2917531404648e+17
6.5835070931796e+17
1.3167014824932099e+18
2.6334030732274196e+18
5.26680620074384e+18
...
inf
inf
inf
inf
I made the cost function to later use it on gradient descent, but since the cost is so high the predictions and w b final values are 'nan'
def compute_cost(x, y, w, b,lambda_ = 0.7):
# fw_b = wn * xn + b , wn and xn are arrays
# cost function = 1 / 2m sum ( y(Prediction) - y )^2
m = len(x)
n = len(w)
cost = 0.
for i in range(m):
f_wb_i = np.dot(x[i], w) + b #(n,)(n,)=scalar, see np.dot
cost = cost + (f_wb_i - y[i])**2 #scalar
print(np.format_float_scientific(cost))
cost = cost / (2 * m) #scalar
reg_cost = 0
for j in range(n):
reg_cost += (w[j]**2) #scalar
reg_cost = (lambda_/(2*m)) * reg_cost #scalar
total_cost = cost + reg_cost #scalar
return total_cost #scalar
# Compute cost with some initial values for paramaters w, b
,x = [ bedroom , bathroom , sqft living , sqft lot ,floors']
x = [3,2,120,80,1], [6,4,300,150,2] , [10,5,500,320,2],
[20,9,786,589,3],[2,1,100,100,1]
y = [20000.0,60000.0,100000.0,364000.0,55000.0]
initial_w = np.zeros(len(x[0]))
initial_b = 0.
cost = compute_cost(x, y , initial_w, initial_b)
print(f'Cost at optimal w : {cost}')
4.e+08
4.e+09
1.4e+10
1.46496e+11
1.49521e+11
and this was the prediction output
prediction: 110783.88, target value: 60000.0
prediction: 94826.23, target value: 100000.0
prediction: 332935.79, target value: 364000.0
prediction: 68392.33, target value: 55000.0