-
Notifications
You must be signed in to change notification settings - Fork 15
/
gradientdescent.py
65 lines (55 loc) · 1.47 KB
/
gradientdescent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
from mpl_toolkits.basemap import Basemap
from matplotlib import colors
import numpy as np
import csv
import matplotlib.pyplot as plt
#setting
learnrate = 0.0001
iterasi = 1000
#regression = sx + c
c_begin = 5
s_begin = 5
datafilename = 'datasampel'
#y array
analysis = []
with open(datafilename, 'r') as input_file:
reader = csv.reader(input_file, delimiter = '\t')
for line in reader:
if line: #check if list is not empty
analysis.append(float(line[1]))
print analysis
#x array
week = range(1,len(analysis)+1)
#error array
err = []
for i in xrange(iterasi):
s_grad = 0.0
c_grad = 0.0
errtemp = 0.0
for j in xrange(len(week)):
s_grad += (analysis[j] - (s_begin * week[j] + c_begin)) * week[j]
c_grad += (analysis[j] - (s_begin * week[j] + c_begin))
s_grad = -(2.0/len(week)) * s_grad
c_grad = -(2.0/len(week)) * c_grad
s_begin = s_begin - learnrate * s_grad
c_begin = c_begin - learnrate * c_grad
#calculate error
for k in xrange(len(week)):
errtemp += abs(analysis[k] - (s_begin * week[k] + c_begin))
err.append(errtemp/len(week))
#this is the coefficient and contant we get
print c_begin, s_begin
ideal = []
for data in week:
ideal.append(s_begin * data + c_begin)
print s_begin, c_begin
plt.plot(week,analysis, 'o')
plt.plot(week, ideal)
plt.ylabel('jumlah penjualan mobil')
plt.show()
plt.ylabel('rata-rata error absolute')
plt.xlabel('jumlah iterasi')
plt.title('perkembangan error ordinary gradient descent')
plt.plot(range(iterasi), err)
plt.show()