-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregression2_polynomial.py
More file actions
executable file
·63 lines (48 loc) · 1.31 KB
/
regression2_polynomial.py
File metadata and controls
executable file
·63 lines (48 loc) · 1.31 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
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)
# パラメータを初期化
theta = np.random.rand(3)
# 学習データの行列を作る
def to_matrix(x):
return np.vstack([np.ones(x.size), x, x ** 2]).T
X = to_matrix(train_z)
# 予測関数
def f(x):
return np.dot(x, theta)
# 目的関数
def E(x, y):
return 0.5 * np.sum((y - f(x)) ** 2)
# 学習率
ETA = 1e-2
# 誤差の差分
diff = 1
# 更新回数
count = 0
# 誤差の差分が0.01以下になるまでパラメータ更新を繰り返す
error = E(X, train_y)
while diff > 1e-3:
# 更新結果を一時変数に保存
theta = theta - ETA * np.dot(f(X) - train_y, X)
# 前回の誤差との差分を計算
current_error = E(X, train_y)
diff = error - current_error
error = current_error
# ログの出力
count += 1
log = '{}回目: theta = {}, 差分 = {:.4f}'
print(log.format(count, theta, diff))
# プロットして確認
x = np.linspace(-3, 3, 100)
plt.plot(train_z, train_y, 'o')
plt.plot(x, f(to_matrix(x)))
plt.show()