-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregression1_linear.py
More file actions
executable file
·65 lines (49 loc) · 1.43 KB
/
regression1_linear.py
File metadata and controls
executable file
·65 lines (49 loc) · 1.43 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
import matplotlib.pyplot as plt
# 学習データを読み込む
train = np.loadtxt('click.csv', delimiter=',', dtype='int', skiprows=1)
train_x = train[:,0]
train_y = train[:,1]
# 標準化
mu = train_x.mean()
sigma = train_x.std()
def standardize(x):
return(x-mu)/sigma
train_z = standardize(train_x)
# パラメータを初期化
theta0 = np.random.rand()
theta1 = np.random.rand()
print(theta0)
print(theta1)
# 予測関数
def f(x):
return theta0 + theta1 * x
# 目的関数(y:実際の目的関数 fx:パラメータを仮置きした目的関数):誤差
def E(x, y):
return 0.5 * np.sum (y-f(x) **2)
# 学習率
ETA = 1e-3
# 誤差の差分
diff = 1
# 更新回数
count = 0
# 誤差の差分が0.01以下になるまでパラメータ更新を繰り返す
error = E(train_z, train_y)
while diff > 1e-2:
tmp_theta0 = theta0 - ETA * np.sum((f(train_z)- train_y))
tmp_theta1 = theta1 - ETA * np.sum((f(train_z)- train_y) * train_z)
# パラメータ更新
theta0 = tmp_theta0
theta1 = tmp_theta1
# 前回誤差との差分計算
current_error = E(train_z, train_y)
diff = error - current_error
error = current_error
count += 1
log = "{}回目:theta0 = {:.3f},theta1 = {:.3f}, 差分 = {:.4f}"
print(log.format(count,theta0,theta1,diff))
# プロットして確認
x = np.linspace(-3, 3, 100)
plt.plot(train_z, train_y, 'o')
plt.plot(x, f(x))
plt.show()