# coding: utf-8 # In[385]: import math import numpy as np import matplotlib.pyplot as plt def norm(m,s,n): return np.random.normal(m, s,n) def exp(x): return np.exp(x) def dot(x,y): return np.dot(x,y) N=100 #size of data def h(theta,x): z = [] for d in range(0,len(theta)): z.append(np.power(x,d)) return dot(theta,z) def powers(x,D): X=[] for d in range(0,D): X.append(np.power(x,d)) return np.array(X) x=abs(norm(0., 2., N)) y=exp(-x+ norm(0.,0.1,N)) # In[388]: plt.plot(x, y, 'b.',t, np.exp(-1*t), 'g-') plt.axis([0, 6, 0, 2]) plt.show() # In[394]: D=1 theta=np.zeros(D) alpha=0.001#np.power(0.01,D) for t in range(0,1000): for i, yi in enumerate(y): X=powers(x[i],len(theta)) theta = theta - alpha*(h(theta,x[i]) - yi)*X print(theta) t = np.arange(0., 5., 0.2) plt.plot(x, y, 'b.',t, h(theta,t), 'r-') plt.axis([0, 6, 0, 2]) plt.show() # In[395]: D=2 theta=np.array(theta) theta=np.append(theta, 0) alpha=0.001#np.power(0.01,D) for t in range(0,1000): for i, yi in enumerate(y): X=powers(x[i],len(theta)) theta = theta - alpha*(h(theta,x[i]) - yi)*X print(theta) t = np.arange(0., 5., 0.2) plt.plot(x, y, 'b.',t, h(theta,t), 'r-') plt.axis([0, 6, 0, 2]) plt.show() # In[396]: D=3 theta=np.array(theta) theta=np.append(theta, 0) alpha=0.01#np.power(0.01,D) for t in range(0,1000): for i, yi in enumerate(y): X=powers(x[i],len(theta)) theta = theta - alpha*(h(theta,x[i]) - yi)*X print(theta) t = np.arange(0., 5., 0.2) plt.plot(x, y, 'b.',t, h(theta,t), 'r-') plt.axis([0, 6, 0, 2]) plt.show() # In[397]: D=4 theta=np.array(theta) theta=np.append(theta, 0) alpha=0.0001#np.power(0.01,D) for t in range(0,1000): for i, yi in enumerate(y): X=powers(x[i],len(theta)) theta = theta - alpha*(h(theta,x[i]) - yi)*X print(theta) t = np.arange(0., 5., 0.2) plt.plot(x, y, 'b.',t, h(theta,t), 'r-') plt.axis([0, 6, 0, 2]) plt.show() # In[436]: import csv Data = np.array(list(csv.reader(open('Ford_Focus.csv')))) Data = np.delete(Data, (0), axis=0) Data = np.delete(Data, (0), axis=1) Data = np.delete(Data, (0), axis=1) x=Data[:,0] y=Data[:,1] plt.plot(x,y,'b.') plt.show() # In[ ]: