Creating cost function and gradient for gradient descent
Since I started to learn Machine Learning, it is important to fully understand the algorithms. Therefore, I will implement ML algorithms from scratch and compare them with the implementation of scikit-learn library.
To start, I created functions for the gradient descent algorithm.
Here is the cost J(w, b).
And these are partial derivatives.
import math, copy
import numpy as np
x_train = np.array([1.0, 2.0, 3.0]) #features
y_train = np.array([300.0, 500.0, 700.0]) #target value
#I am going to create a cost function, which shows how different cost function and actual value is.
#This cost function can calculate how much difference there is when you use a certain w and b.
#This returns J(w, b)
def compute_cost(x, y, w, b):
m = x.shape[0] #The length of the array
cost =0
for i in range(m):
f_wb = w*x[i]+b
cost += (f_wb-y[i])**2
total_cost = 1/(2*m)*cost
return total_cost
#Next, this is the calculation of gradient. If you calculate the partial derivative of J(w, b).
def compute_gradient(x, y, w, b):
m=x.shape[0]
dj_dw = 0
dj_db = 0
for i in range(m):
f_wb = w*x[i]+b
dj_dw_i = (f_wb - y[i]) * x[i]
dj_db_i = f_wb - y[i]
dj_db += dj_db_i
dj_dw += dj_dw_i
dj_dw = dj_dw / m
dj_db = dj_db / m
return dj_dw, dj_db