def GDescent(X, Y, theta, alpha, num_iters): m = float(Y.size) J_history = np.zeros((num_iters, 1)) for iter in xrange(0, num_iters): th0 = theta[0, 0] - alpha * (1 / m) * np.dot((np.dot(X, theta) - Y).T, X[:, 0]) th1 = theta[1, 0] - alpha * (1 / m) * np.dot((np.dot(X, theta) - Y).T, X[:, 1]) # th2 = - alpha * (1/m) * np.dot( (np.dot(X, theta) - Y).T, X[:, 1]) # print th2, alpha * (1/m) theta[0, 0] = th0 theta[1, 0] = th1 J_history[iter] = cC.compCost(X, Y, theta) return theta, J_history
def GDescent(X, Y, theta, alpha, num_iters): m = float(Y.size) J_history = np.zeros((num_iters, 1)) for iter in xrange(0, num_iters): th0 = theta[0, 0] - alpha * (1/m) * np.dot((np.dot(X, theta) - Y).T , X[:, 0]) th1 = theta[1, 0] - alpha * (1/m) * np.dot((np.dot(X, theta) - Y).T , X[:, 1]) #th2 = - alpha * (1/m) * np.dot( (np.dot(X, theta) - Y).T, X[:, 1]) #print th2, alpha * (1/m) theta[0, 0] = th0 theta[1, 0] = th1 J_history[iter] = cC.compCost(X, Y, theta) return theta, J_history
import loaddata as ld import computeCost as cC import gradientdescent as GD import math X, Y, m = ld.load('ex1data.txt')#load X's and Y's m - len of dataset theta = np.zeros((2, 1))#init fitting params #should be 32.07 print 'Cost:', cC.compCost(X, Y, theta) #Some gradient descent settings iterations = 1500; alpha = 0.01; theta,J_history = GD.GDescent(X, Y, theta, alpha, iterations) print 'theta: ', theta pvalue = 3.5 #predict = 1/(1+ math.exp( np.dot( np.array([1, pvalue]).reshape(1, 2), theta))) predict = np.dot( np.array([1, pvalue]).reshape(1, 2), theta ) print 'predict for ',pvalue, predict
import numpy as np import loaddata as ld import computeCost as cC import gradientdescent as GD import math X, Y, m = ld.load('ex1data.txt') #load X's and Y's m - len of dataset theta = np.zeros((2, 1)) #init fitting params #should be 32.07 print 'Cost:', cC.compCost(X, Y, theta) #Some gradient descent settings iterations = 1500 alpha = 0.01 theta, J_history = GD.GDescent(X, Y, theta, alpha, iterations) print 'theta: ', theta pvalue = 3.5 #predict = 1/(1+ math.exp( np.dot( np.array([1, pvalue]).reshape(1, 2), theta))) predict = np.dot(np.array([1, pvalue]).reshape(1, 2), theta) print 'predict for ', pvalue, predict