import numpy as np from scipy.misc import imread, imsave, imresize from matplotlib import pyplot as plt from scipy.io import loadmat,whosmat from numpy.linalg import inv def linregpredictmat(xmat,thetavec): #Find the predicted value of y=xmat*theta t=1 yhat = xmat.dot(thetavec) return yhat def linregloss(y,yhat): #Compute the loss as mean squared error over all samples # m = len(y) n mseloss = np.sum((yhat-y)**2)/(2*m) mseloss1 = mseloss return mseloss1 def normalequations(xmat,y,thetavec): thetanorm=np.zeros(thetavec.shape) xinv = inv(xmat.T.dot(xmat)) xprod = xinv.dot(xmat.T) res = xprod.dot(y) thetanorm = res return thetanorm def gradientdescent(yhat,y,epsilon,thetavec,xmat,x): # Use gradient descent with learning rate epsilon to predict the next value of thetavec # First, compute theta0 and theta1 one at a time m = len(yhat) [mm, nn] = xmat.shape diffvec = yhat - y deltatheta = np.zeros(xmat.shape) deltatheta = xmat * diffvec[:, np.newaxis] corrvector = np.zeros(nn) nythetavectorized = np.zeros(thetavec.shape) for j in range(0, nn): corrvector[j] = epsilon * np.sum(deltatheta[:, j]) / m nythetavectorized = thetavec - corrvector return nythetavectorized f = open('linregy.asc', 'r') ydata = np.genfromtxt(f) f = open('ex1data1.txt','r') tmpdata = np.genfromtxt(f,delimiter=",") x=tmpdata[:,0] nofsamp=x.shape ydata=tmpdata[:,1] thetavec = np.zeros((2)) nofsamp = len(ydata) xmat = np.zeros((nofsamp,2)) xmat[:,0]=np.ones(nofsamp).T xmat[:,1]= x.T theta0 = 0.0 theta1 = 0.0 thetavec[0] = theta0 thetavec[0]=theta1 maxit = 2500; lossvec = np.zeros(maxit) for it in range(1,maxit): yhat = linregpredictmat(xmat,thetavec) mseloss=linregloss(ydata,yhat) epsilon = 0.01 thetavecny = gradientdescent(yhat,ydata, epsilon,thetavec,xmat,x) thetavec = thetavecny lossvec[it-1] = mseloss #Control the answer using the normal equations #thetanorm = normalequations(xmattest,ytest,thetavec) thetanorm = normalequations(xmat,ydata,thetavec) print thetavec print thetanorm ynormpred = linregpredictmat(xmat,thetanorm) plt.plot(x,ydata,'ro',x,yhat,'b-',x,ynormpred,'g--') plt.show() plt.plot(lossvec[1:200]) plt.show() #