Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- import math
- #import matplotlib.pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
- from matplotlib import cm
- from matplotlib.ticker import LinearLocator, FormatStrFormatter
- import matplotlib.pyplot as plt
- import numpy as np
- def loss(w0,w1, data):
- dataSum=0.0
- for d in data:
- xd = d[0]
- yd = d[1]
- hwxd=float(w0)+float(w1*xd)
- tmp = pow((yd-float(hwxd)),2)
- dataSum+= tmp
- #return(math.sqrt(1.0/(2.0*len(data))*dataSum))
- return(1.0/(2.0*len(data))*dataSum)
- #return(rmse(w0,w1, data))
- def rmse(w0,w1, data):
- w0= w0 *1000
- w1=0.3047073401501218
- dataSum=0.0
- for d in data:
- xd = d[0]*1000
- yd = d[1]*1000
- hwxd=float(w0)+float(w1*xd)
- tmp = pow((yd-float(hwxd)),2)
- dataSum+= tmp
- return(math.sqrt(1.0/(2.0*len(data))*dataSum))
- #return(1.0/(2.0*len(data))*dataSum)
- def gradDesc(eta,maxit,data):
- # wi := wi - eta(deriv(w))
- w0 = random.random()
- w1 = random.random()
- w0 = 0
- w1=0
- # w0=-32.632
- # w1=0.304315
- #
- # nw0 = -32.632
- threshold = 0.1
- iterations =0
- lossfunc=loss(w0,w1,data)
- print('initial loss =', loss(w0,w1,data))
- # fig = plt.figure()
- # ax = fig.gca(projection='3d')
- # plt.ion()
- # plt.show()
- # graph.plot(x,y,'ro',markersize=5)
- xarr=[]
- yarr=[]
- lossf=[]
- w0arr=[]
- w1arr=[]
- while(iterations < maxit):
- iterations+=1;
- print('iter:',iterations)
- w0sum=0.0
- for d in data:
- xd = d[0]
- yd = d[1]
- hwxd=float(w0)+float(w1*xd)
- tmp = (float(hwxd)-yd)
- w0sum+= tmp
- w0sum = (1.0/len(data))*w0sum
- nw0=w0-(eta*w0sum)
- w1sum=0.0
- for d in data:
- xd = d[0]
- yd = d[1]
- hwxd=float(w0)+float(w1*xd)
- tmp = (float(hwxd)-yd)*xd
- w1sum+= tmp
- w1sum = (1.0/len(data))*w1sum
- nw1=w1-(eta*w1sum)
- w0=nw0
- w1=nw1
- prevlossfunc = lossfunc
- lossfunc=loss(w0,w1,data)
- w0arr.append(w0)
- w1arr.append(w1)
- lossf.append(lossfunc)
- # plt.scatter(w1,lossfunc)
- #
- # plt.draw()
- #time.sleep(0.05)
- print(iterations,w0,w1,lossfunc, rmse(w0,w1,data))
- xmin = 1000000000000
- xmax =0
- for d in data:
- xarr.append(d[0])
- yarr.append(d[1])
- if (d[0] < xmin):
- xmin=d[0]
- if (d[0] > xmax):
- xmax=d[0]
- fig=plt.figure()
- graph= fig.add_subplot(111)
- graph.plot(xarr,yarr,'ro',markersize=5)
- #(w0+(xarr[0]*w1))
- graph.plot([xmin,xmax],[(w0+(xmin*w1)),(w0+(xmax*w1))])
- fig2=plt.figure()
- graph2= fig2.add_subplot(111)
- graph2.plot(w0arr,lossf,'r-',label='w0')
- graph2.plot(w1arr,lossf,'g-',label='w1')
- graph2.legend(loc=0)
- plt.show()
- # for d in data:
- # xarr.append(d[0])
- # yarr.append(d[1])
- # plt.scatter(xarr,yarr)
- #
- # plt.plot([200,1000], [((w1*200)+w0),((w1*1000)+w0)] )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement