-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
50 lines (38 loc) · 1.24 KB
/
Copy pathmain.py
File metadata and controls
50 lines (38 loc) · 1.24 KB
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
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import sys
sys.setrecursionlimit(3000)
import math
#cost function -->시작위치 지정, theta 개수 지정(여기선 1차방정식으로만)
# gradient -->alpha값 지정
dataset=pd.DataFrame(pd.read_csv("C:/processed file.csv"))
A=pd.DataFrame([dataset.iloc[:,4],dataset.iloc[:,10]])
x=dataset.iloc[:,4]
y=dataset.iloc[:,10]
m=len(x)
theta0=1010;theta1=0
count=0
gradient_function_moving=[]
def gradient_function(theta0,theta1,alpha):
global count
count+=1
H_x = theta0 + theta1 * x
result_theta0=theta0-alpha*sum(np.array(H_x)-np.array(y))/m
result_theta1=theta1-alpha*sum((np.array(H_x)-np.array(y))*np.array(x))/m
print(result_theta0,result_theta1)
gradient_function_moving.append(1/2/m*sum((np.array(H_x)-np.array(y))**2))
if count==2000:
return [result_theta0,result_theta1]
else:
return gradient_function(result_theta0,result_theta1,alpha=alpha)
def cost_function():
pass
result=gradient_function(theta0,theta1,0.008)
plt.figure(1)
plt.scatter(x,y)
plt.plot(x,result[0]+result[1]*x,color='red')
print(gradient_function_moving[0],gradient_function_moving[1])
plt.figure(2)
plt.plot(gradient_function_moving)
plt.show()