def cal\_cost(theta, X, y): ''' Calculates the cost for given X and Y. The following shows and example of a single dimensional X theta = Vector of thetas X = Row of X's np.zeros((2,j)) y = Actual y's np.zeros((2,1)) where: j is the no of features ''' m = len(y) predictions = X.dot(theta) cost = (1/2\*m) \* np.sum(np.square(predictions - y)) return cost
def gradient\_descent(X, y, theta, learning\_rate = 0.01, iterations = 100): ''' X = Matrix of X with added bias units y = Vector of Y theta=Vector of thetas np.random.randn(j,1) learning\_rate iterations = no of iterations Returns the final theta vector and array of cost history over no of iterations ''' m = len(y) # learning\_rate = 0.01 # iterations = 100 cost\_history = np.zeros(iterations) theta\_history = np.zeros((iterations, 2)) for i in range(iterations): prediction = np.dot(X, theta) theta = theta - (1/m) \* learning\_rate \* (X.T.dot((prediction - y))) theta\_history\[i, :\] = theta.T cost\_history\[i\] = cal\_cost(theta, X, y) return theta, cost\_history, theta\_history
def stocashtic\_gradient\_descent(X, y, theta, learning\_rate=0.01, iterations=10): ''' X = Matrix of X with added bias units y = Vector of Y theta=Vector of thetas np.random.randn(j,1) learning\_rate iterations = no of iterations Returns the final theta vector and array of cost history over no of iterations ''' m = len(y) cost\_history = np.zeros(iterations) for it in range(iterations): cost = 0.0 for i in range(m): rand\_ind = np.random.randint(0, m) X\_i = X\[rand\_ind, :\].reshape(1, X.shape\[1\]) y\_i = y\[rand\_ind, :\].reshape(1, 1) prediction = np.dot(X\_i, theta) theta -= (1/m) \* learning\_rate \* (X\_i.T.dot((prediction - y\_i))) cost += cal\_cost(theta, X\_i, y\_i) cost\_history\[it\] = cost return theta, cost\_history
def minibatch\_gradient\_descent(X, y, theta, learning\_rate=0.01, iterations=10, batch\_size=20): ''' X = Matrix of X without added bias units y = Vector of Y theta=Vector of thetas np.random.randn(j,1) learning\_rate iterations = no of iterations Returns the final theta vector and array of cost history over no of iterations ''' m = len(y) cost\_history = np.zeros(iterations) n\_batches = int(m / batch\_size) for it in range(iterations): cost = 0.0 indices = np.random.permutation(m) X = X\[indices\] y = y\[indices\] for i in range(0, m, batch\_size): X\_i = X\[i: i+batch\_size\] y\_i = y\[i: i+batch\_size\] X\_i = np.c\_\[np.ones(len(X\_i)), X\_i\] prediction = np.dot(X\_i, theta) theta -= (1/m) \* learning\_rate \* (X\_i.T.dot((prediction - y\_i))) cost += cal\_cost(theta, X\_i, y\_i) cost\_history\[it\] = cost return theta, cost\_history