-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassification2_logistic_regression.py
More file actions
executable file
·57 lines (43 loc) · 1.2 KB
/
classification2_logistic_regression.py
File metadata and controls
executable file
·57 lines (43 loc) · 1.2 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
import numpy as np
import matplotlib.pyplot as plt
# 学習データを読み込む
train = np.loadtxt('images2.csv', delimiter=',', skiprows=1)
train_x = train[:,0:2]
train_y = train[:,2]
# パラメータを初期化
theta = np.random.rand(3)
# 標準化
mu = train_x.mean(axis=0)
sigma = train_x.std(axis=0)
def standardize(x):
return (x - mu) / sigma
train_z = standardize(train_x)
# x0を加える
def to_matrix(x):
x0 = np.ones([x.shape[0], 1])
return np.hstack([x0, x])
X = to_matrix(train_z)
# シグモイド関数
def f(x):
return 1 / (1 + np.exp(-np.dot(x, theta)))
# 分類関数
def classify(x):
return (f(x) >= 0.5).astype(np.int)
# 学習率
ETA = 1e-3
# 繰り返し回数
epoch = 5000
# 更新回数
count = 0
# 学習を繰り返す
for _ in range(epoch):
theta = theta - ETA * np.dot(f(X) - train_y, X)
# # ログの出力
# count += 1
# print('{}回目: theta = {}'.format(count, theta))
# プロットして確認
x0 = np.linspace(-2, 2, 100)
plt.plot(train_z[train_y == 1, 0], train_z[train_y == 1, 1], 'o')
plt.plot(train_z[train_y == 0, 0], train_z[train_y == 0, 1], 'x')
plt.plot(x0, -(theta[0] + theta[1] * x0) / theta[2], linestyle='dashed')
plt.show()