質問編集履歴
6
コード
test
CHANGED
File without changes
|
test
CHANGED
@@ -180,7 +180,7 @@
|
|
180
180
|
|
181
181
|
edgecolor='black')
|
182
182
|
|
183
|
-
|
183
|
+
|
184
184
|
|
185
185
|
|
186
186
|
|
5
コード
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,6 +26,22 @@
|
|
26
26
|
|
27
27
|
```pytthon
|
28
28
|
|
29
|
+
|
30
|
+
|
31
|
+
import numpy as np
|
32
|
+
|
33
|
+
from sklearn import datasets
|
34
|
+
|
35
|
+
from sklearn.model_selection import train_test_split
|
36
|
+
|
37
|
+
import matplotlib.pyplot as plt
|
38
|
+
|
39
|
+
%matplotlib inline
|
40
|
+
|
41
|
+
from matplotlib.colors import ListedColormap
|
42
|
+
|
43
|
+
|
44
|
+
|
29
45
|
iris=datasets.load_iris()
|
30
46
|
|
31
47
|
x=iris.data[:,[2,3]]
|
@@ -34,6 +50,140 @@
|
|
34
50
|
|
35
51
|
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1,stratify=y)
|
36
52
|
|
53
|
+
|
54
|
+
|
55
|
+
plot_decision_regions(x=x_train_01_subset,
|
56
|
+
|
57
|
+
y=y_train_01_subset,
|
58
|
+
|
59
|
+
classifier=lrgd)
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
class LogisticRegressionGD:
|
64
|
+
|
65
|
+
def __init__(self,eta=0.05,n_iter=100,random_state=1):
|
66
|
+
|
67
|
+
self.eta=eta
|
68
|
+
|
69
|
+
self.n_iter=n_iter
|
70
|
+
|
71
|
+
self.random_state=random_state
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
def fit(self,x,y):
|
76
|
+
|
77
|
+
rgen=np.random.RandomState(self.random_state) #インスタンス
|
78
|
+
|
79
|
+
self.w_=rgen.normal(loc=0.0,scale=0.01,size=1+x.shape[1]) #特徴量+バイアス
|
80
|
+
|
81
|
+
self.cost_=[]
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
for i in range(self.n_iter):
|
86
|
+
|
87
|
+
net_input=self.net_input(x)
|
88
|
+
|
89
|
+
out_put=self.activation(net_input)
|
90
|
+
|
91
|
+
errors=(y-out_put)
|
92
|
+
|
93
|
+
self.w_[1:]+=self.eta*x.T.dot(errors)
|
94
|
+
|
95
|
+
self.w_[0]+=self.eta*errors.sum()
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
cost=-y.dot(np.log(out_put))-((1-y).dot(np.log(1-out_put)))
|
100
|
+
|
101
|
+
self.cost_.append(cost)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
def net_input(self,x):
|
108
|
+
|
109
|
+
return np.dot(x,self.w_[1:])+self.w_[0]
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
def activation(self,z):
|
114
|
+
|
115
|
+
return 1./(1.+np.exp(np.clip(z,-250,250)))
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
def predict(self,x):
|
120
|
+
|
121
|
+
return np.where(self.activation(self.net_input(x))>=0.5,1,0)
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
def plot_decision_regions(x,y,classifier,test_idx=None,resolution=0.02):
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
markers=('s','x','o','^','v')
|
134
|
+
|
135
|
+
colors=('blue','red','lightgreen','gray','cyan')
|
136
|
+
|
137
|
+
cmap=ListedColormap(colors[:len(np.unique(y))])
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
x1_min,x1_max=x[:,0].min()-1,x[:,0].max()+1
|
144
|
+
|
145
|
+
x2_min,x2_max=x[:,1].min()-1,x[:,1].max()+1
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
xx1,xx2=np.meshgrid(np.arange(x1_min,x1_max,resolution),
|
150
|
+
|
151
|
+
np.arange(x2_min,x2_max,resolution))
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
z=classifier.predict(np.array([xx1.ravel(),xx2.ravel()]).T)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
z=z.reshape(xx1.shape)
|
160
|
+
|
161
|
+
plt.contourf(xx1,xx2,z,alpha=0.8,cmap=cmap)
|
162
|
+
|
163
|
+
plt.xlim(xx1.min(),xx1.max())
|
164
|
+
|
165
|
+
plt.ylim(xx2.min(),xx2.max())
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
for idx,c1 in enumerate(np.unique(y)):
|
170
|
+
|
171
|
+
plt.scatter(x=x[y==c1,0],y=x[y==c1,1],
|
172
|
+
|
173
|
+
alpha=0.8,
|
174
|
+
|
175
|
+
c=colors[idx],
|
176
|
+
|
177
|
+
marker=markers[idx],
|
178
|
+
|
179
|
+
label=c1,
|
180
|
+
|
181
|
+
edgecolor='black')
|
182
|
+
|
183
|
+
ここまでが関数です
|
184
|
+
|
185
|
+
|
186
|
+
|
37
187
|
x_train_01_subset=x_train[(y_train==0)|(y_train==1)]
|
38
188
|
|
39
189
|
y_train_01_subset=y_train[(y_train==0)|(y_train==1)]
|
@@ -48,142 +198,6 @@
|
|
48
198
|
|
49
199
|
classifier=lrgd)
|
50
200
|
|
51
|
-
|
52
|
-
|
53
|
-
class LogisticRegressionGD:
|
54
|
-
|
55
|
-
def __init__(self,eta=0.05,n_iter=100,random_state=1):
|
56
|
-
|
57
|
-
self.eta=eta
|
58
|
-
|
59
|
-
self.n_iter=n_iter
|
60
|
-
|
61
|
-
self.random_state=random_state
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
def fit(self,x,y):
|
66
|
-
|
67
|
-
rgen=np.random.RandomState(self.random_state) #インスタンス
|
68
|
-
|
69
|
-
self.w_=rgen.normal(loc=0.0,scale=0.01,size=1+x.shape[1]) #特徴量+バイアス
|
70
|
-
|
71
|
-
self.cost_=[]
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
for i in range(self.n_iter):
|
76
|
-
|
77
|
-
net_input=self.net_input(x)
|
78
|
-
|
79
|
-
out_put=self.activation(net_input)
|
80
|
-
|
81
|
-
errors=(y-out_put)
|
82
|
-
|
83
|
-
self.w_[1:]+=self.eta*x.T.dot(errors)
|
84
|
-
|
85
|
-
self.w_[0]+=self.eta*errors.sum()
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
cost=-y.dot(np.log(out_put))-((1-y).dot(np.log(1-out_put)))
|
90
|
-
|
91
|
-
self.cost_.append(cost)
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
def net_input(self,x):
|
98
|
-
|
99
|
-
return np.dot(x,self.w_[1:])+self.w_[0]
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
def activation(self,z):
|
104
|
-
|
105
|
-
return 1./(1.+np.exp(np.clip(z,-250,250)))
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
def predict(self,x):
|
110
|
-
|
111
|
-
return np.where(self.activation(self.net_input(x))>=0.5,1,0)
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
def plot_decision_regions(x,y,classifier,test_idx=None,resolution=0.02):
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
markers=('s','x','o','^','v')
|
124
|
-
|
125
|
-
colors=('blue','red','lightgreen','gray','cyan')
|
126
|
-
|
127
|
-
cmap=ListedColormap(colors[:len(np.unique(y))])
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
x1_min,x1_max=x[:,0].min()-1,x[:,0].max()+1
|
134
|
-
|
135
|
-
x2_min,x2_max=x[:,1].min()-1,x[:,1].max()+1
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
xx1,xx2=np.meshgrid(np.arange(x1_min,x1_max,resolution),
|
140
|
-
|
141
|
-
np.arange(x2_min,x2_max,resolution))
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
z=classifier.predict(np.array([xx1.ravel(),xx2.ravel()]).T)
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
z=z.reshape(xx1.shape)
|
150
|
-
|
151
|
-
plt.contourf(xx1,xx2,z,alpha=0.8,cmap=cmap)
|
152
|
-
|
153
|
-
plt.xlim(xx1.min(),xx1.max())
|
154
|
-
|
155
|
-
plt.ylim(xx2.min(),xx2.max())
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
for idx,c1 in enumerate(np.unique(y)):
|
160
|
-
|
161
|
-
plt.scatter(x=x[y==c1,0],y=x[y==c1,1],
|
162
|
-
|
163
|
-
alpha=0.8,
|
164
|
-
|
165
|
-
c=colors[idx],
|
166
|
-
|
167
|
-
marker=markers[idx],
|
168
|
-
|
169
|
-
label=c1,
|
170
|
-
|
171
|
-
edgecolor='black')
|
172
|
-
|
173
|
-
ここまでが関数です
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
lrgd=LogisticRegressionGD(eta=0.05,n_iter=1000,random_state=1)
|
180
|
-
|
181
|
-
plot_decision_regions(x=x_train_01_subset,
|
182
|
-
|
183
|
-
y=y_train_01_subset,
|
184
|
-
|
185
|
-
classifier=lrgd)
|
186
|
-
|
187
201
|
コード
|
188
202
|
|
189
203
|
```
|
4
n
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
![イメージ説明](8a2e139be2241e34d19c6f4f396c4f7d.png)
|
2
|
+
|
1
3
|
質問は決定領域のコードでこの下のコードを実行すると、
|
2
4
|
|
3
5
|
データーはプロっとされるのですが、
|
3
こーど
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
plot_decision_regionsこの関数が決定領域の図です。
|
14
14
|
|
15
|
-
|
15
|
+
|
16
16
|
|
17
17
|
特徴量は2つです。
|
18
18
|
|
@@ -23,6 +23,94 @@
|
|
23
23
|
よろしくお願いします。
|
24
24
|
|
25
25
|
```pytthon
|
26
|
+
|
27
|
+
iris=datasets.load_iris()
|
28
|
+
|
29
|
+
x=iris.data[:,[2,3]]
|
30
|
+
|
31
|
+
y=iris.target
|
32
|
+
|
33
|
+
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1,stratify=y)
|
34
|
+
|
35
|
+
x_train_01_subset=x_train[(y_train==0)|(y_train==1)]
|
36
|
+
|
37
|
+
y_train_01_subset=y_train[(y_train==0)|(y_train==1)]
|
38
|
+
|
39
|
+
lrgd=LogisticRegressionGD(eta=0.05,n_iter=1000,random_state=1)
|
40
|
+
|
41
|
+
lrgd.fit(x_train_01_subset,y_train_01_subset)
|
42
|
+
|
43
|
+
plot_decision_regions(x=x_train_01_subset,
|
44
|
+
|
45
|
+
y=y_train_01_subset,
|
46
|
+
|
47
|
+
classifier=lrgd)
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
class LogisticRegressionGD:
|
52
|
+
|
53
|
+
def __init__(self,eta=0.05,n_iter=100,random_state=1):
|
54
|
+
|
55
|
+
self.eta=eta
|
56
|
+
|
57
|
+
self.n_iter=n_iter
|
58
|
+
|
59
|
+
self.random_state=random_state
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
def fit(self,x,y):
|
64
|
+
|
65
|
+
rgen=np.random.RandomState(self.random_state) #インスタンス
|
66
|
+
|
67
|
+
self.w_=rgen.normal(loc=0.0,scale=0.01,size=1+x.shape[1]) #特徴量+バイアス
|
68
|
+
|
69
|
+
self.cost_=[]
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
for i in range(self.n_iter):
|
74
|
+
|
75
|
+
net_input=self.net_input(x)
|
76
|
+
|
77
|
+
out_put=self.activation(net_input)
|
78
|
+
|
79
|
+
errors=(y-out_put)
|
80
|
+
|
81
|
+
self.w_[1:]+=self.eta*x.T.dot(errors)
|
82
|
+
|
83
|
+
self.w_[0]+=self.eta*errors.sum()
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
cost=-y.dot(np.log(out_put))-((1-y).dot(np.log(1-out_put)))
|
88
|
+
|
89
|
+
self.cost_.append(cost)
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
def net_input(self,x):
|
96
|
+
|
97
|
+
return np.dot(x,self.w_[1:])+self.w_[0]
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
def activation(self,z):
|
102
|
+
|
103
|
+
return 1./(1.+np.exp(np.clip(z,-250,250)))
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
def predict(self,x):
|
108
|
+
|
109
|
+
return np.where(self.activation(self.net_input(x))>=0.5,1,0)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
26
114
|
|
27
115
|
def plot_decision_regions(x,y,classifier,test_idx=None,resolution=0.02):
|
28
116
|
|
2
コード
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,11 +22,7 @@
|
|
22
22
|
|
23
23
|
よろしくお願いします。
|
24
24
|
|
25
|
-
|
25
|
+
```pytthon
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
26
|
|
31
27
|
def plot_decision_regions(x,y,classifier,test_idx=None,resolution=0.02):
|
32
28
|
|
@@ -34,11 +30,11 @@
|
|
34
30
|
|
35
31
|
|
36
32
|
|
37
|
-
|
33
|
+
markers=('s','x','o','^','v')
|
38
34
|
|
39
|
-
|
35
|
+
colors=('blue','red','lightgreen','gray','cyan')
|
40
36
|
|
41
|
-
|
37
|
+
cmap=ListedColormap(colors[:len(np.unique(y))])
|
42
38
|
|
43
39
|
|
44
40
|
|
@@ -97,3 +93,7 @@
|
|
97
93
|
y=y_train_01_subset,
|
98
94
|
|
99
95
|
classifier=lrgd)
|
96
|
+
|
97
|
+
コード
|
98
|
+
|
99
|
+
```
|
1
もじ
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,11 +34,11 @@
|
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
-
markers=('s','x','o','^','v')
|
37
|
+
markers=('s','x','o','^','v')
|
38
38
|
|
39
|
-
colors=('blue','red','lightgreen','gray','cyan')
|
39
|
+
colors=('blue','red','lightgreen','gray','cyan')
|
40
40
|
|
41
|
-
cmap=ListedColormap(colors[:len(np.unique(y))])
|
41
|
+
cmap=ListedColormap(colors[:len(np.unique(y))])
|
42
42
|
|
43
43
|
|
44
44
|
|