-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmake_data.py
More file actions
83 lines (58 loc) · 2.31 KB
/
Copy pathmake_data.py
File metadata and controls
83 lines (58 loc) · 2.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from AdalineSGD import AdalineSGD
from sklearn.linear_model import LogisticRegression
plt.style.use('ggplot')
def make_data_plane(N):
# データ点のために乱数列を固定
np.random.seed(0)
# ランダムな N×2 行列を生成 = 2次元空間上のランダムな点 N 個
X = np.random.randn(N, 2)
def h(x, y):
return 5 * x + 3 * y - 1 # 真の分離平面 5x + 3y = 1
T = np.array([1 if h(x, y) > 0 else 0 for x, y in X])
return X,T
def make_data(N, draw_plot=True, is_confused=False, confuse_bin=50):
'''N個のデータセットを生成する関数
データをわざと複雑にするための機能 is_confusedを実装する
'''
np.random.seed(1) # シードを固定して、乱数が毎回同じ出力になるようにする
feature = np.random.randn(N, 2)
df = pd.DataFrame(feature, columns=['x', 'y'])
# 2値分類の付与:人為的な分離線の上下どちらに居るかで機械的に判定
df['c'] = df.apply(lambda row : 1 if (5*row.x + 3*row.y - 1)>0 else 0, axis=1)
# 撹乱:データを少し複雑にするための操作
if is_confused:
def get_model_confused(data):
c = 1 if (data.name % confuse_bin) == 0 else data.c
return c
df['c'] = df.apply(get_model_confused, axis=1)
# 可視化:どんな感じのデータになったか可視化するモジュール
# c = df.c つまり2値の0と1で色を分けて表示するようにしてある
if draw_plot:
plt.scatter(x=df.x, y=df.y, c=df.c, alpha=0.6)
plt.xlim([df.x.min() -0.1, df.x.max() +0.1])
plt.ylim([df.y.min() -0.1, df.y.max() +0.1])
return df
def draw_split_line(weight_vector):
a,b,c = weight_vector
x = np.array(range(-10,10,1))
y = (a * x + c)/-b
plt.plot(x,y, alpha=0.3)
def sample():
df = make_data(1000)
df.head(5)
# plt.show()
X = np.c_[ df['x'], df['y']]
y = df['c']
lr = AdalineSGD(eta=0.5,n_iter=100)
lr.fit(X,y)
# plot_decision_regions(X, y, classifier=lr)
print(lr.w_)
draw_split_line(lr.w_)
plt.show()
plt.plot(range(1,len(lr.cost_)+1), lr.cost_, marker='o')
plt.xlabel('Epochs')
plt.ylabel("Average Cost")
plt.show()