teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

6

コード

2020/07/07 16:34

投稿

assa
assa

スコア35

title CHANGED
File without changes
body CHANGED
@@ -89,7 +89,7 @@
89
89
  marker=markers[idx],
90
90
  label=c1,
91
91
  edgecolor='black')
92
- ここまでが関数です
92
+
93
93
 
94
94
  x_train_01_subset=x_train[(y_train==0)|(y_train==1)]
95
95
  y_train_01_subset=y_train[(y_train==0)|(y_train==1)]

5

コード

2020/07/07 16:34

投稿

assa
assa

スコア35

title CHANGED
File without changes
body CHANGED
@@ -12,14 +12,19 @@
12
12
 
13
13
  よろしくお願いします。
14
14
  ```pytthon
15
+
16
+ import numpy as np
17
+ from sklearn import datasets
18
+ from sklearn.model_selection import train_test_split
19
+ import matplotlib.pyplot as plt
20
+ %matplotlib inline
21
+ from matplotlib.colors import ListedColormap
22
+
15
23
  iris=datasets.load_iris()
16
24
  x=iris.data[:,[2,3]]
17
25
  y=iris.target
18
26
  x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1,stratify=y)
19
- x_train_01_subset=x_train[(y_train==0)|(y_train==1)]
27
+
20
- y_train_01_subset=y_train[(y_train==0)|(y_train==1)]
21
- lrgd=LogisticRegressionGD(eta=0.05,n_iter=1000,random_state=1)
22
- lrgd.fit(x_train_01_subset,y_train_01_subset)
23
28
  plot_decision_regions(x=x_train_01_subset,
24
29
  y=y_train_01_subset,
25
30
  classifier=lrgd)
@@ -86,8 +91,10 @@
86
91
  edgecolor='black')
87
92
  ここまでが関数です
88
93
 
89
-
94
+ x_train_01_subset=x_train[(y_train==0)|(y_train==1)]
95
+ y_train_01_subset=y_train[(y_train==0)|(y_train==1)]
90
96
  lrgd=LogisticRegressionGD(eta=0.05,n_iter=1000,random_state=1)
97
+ lrgd.fit(x_train_01_subset,y_train_01_subset)
91
98
  plot_decision_regions(x=x_train_01_subset,
92
99
  y=y_train_01_subset,
93
100
  classifier=lrgd)

4

n

2020/07/07 16:31

投稿

assa
assa

スコア35

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,4 @@
1
+ ![イメージ説明](8a2e139be2241e34d19c6f4f396c4f7d.png)
1
2
  質問は決定領域のコードでこの下のコードを実行すると、
2
3
  データーはプロっとされるのですが、
3
4
  決定境界を表す直線が出てきません。(グラフを二つに分ける)

3

こーど

2020/07/07 16:26

投稿

assa
assa

スコア35

title CHANGED
File without changes
body CHANGED
@@ -5,12 +5,56 @@
5
5
  間違えている箇所がわかりません。
6
6
  教えてください。
7
7
  plot_decision_regionsこの関数が決定領域の図です。
8
- データーとかは関係ないと思うので省略しました。
8
+
9
9
  特徴量は2つです。
10
10
  zのあたいは0か1です。
11
11
 
12
12
  よろしくお願いします。
13
13
  ```pytthon
14
+ iris=datasets.load_iris()
15
+ x=iris.data[:,[2,3]]
16
+ y=iris.target
17
+ x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1,stratify=y)
18
+ x_train_01_subset=x_train[(y_train==0)|(y_train==1)]
19
+ y_train_01_subset=y_train[(y_train==0)|(y_train==1)]
20
+ lrgd=LogisticRegressionGD(eta=0.05,n_iter=1000,random_state=1)
21
+ lrgd.fit(x_train_01_subset,y_train_01_subset)
22
+ plot_decision_regions(x=x_train_01_subset,
23
+ y=y_train_01_subset,
24
+ classifier=lrgd)
25
+
26
+ class LogisticRegressionGD:
27
+ def __init__(self,eta=0.05,n_iter=100,random_state=1):
28
+ self.eta=eta
29
+ self.n_iter=n_iter
30
+ self.random_state=random_state
31
+
32
+ def fit(self,x,y):
33
+ rgen=np.random.RandomState(self.random_state) #インスタンス
34
+ self.w_=rgen.normal(loc=0.0,scale=0.01,size=1+x.shape[1]) #特徴量+バイアス
35
+ self.cost_=[]
36
+
37
+ for i in range(self.n_iter):
38
+ net_input=self.net_input(x)
39
+ out_put=self.activation(net_input)
40
+ errors=(y-out_put)
41
+ self.w_[1:]+=self.eta*x.T.dot(errors)
42
+ self.w_[0]+=self.eta*errors.sum()
43
+
44
+ cost=-y.dot(np.log(out_put))-((1-y).dot(np.log(1-out_put)))
45
+ self.cost_.append(cost)
46
+
47
+
48
+ def net_input(self,x):
49
+ return np.dot(x,self.w_[1:])+self.w_[0]
50
+
51
+ def activation(self,z):
52
+ return 1./(1.+np.exp(np.clip(z,-250,250)))
53
+
54
+ def predict(self,x):
55
+ return np.where(self.activation(self.net_input(x))>=0.5,1,0)
56
+
57
+
14
58
  def plot_decision_regions(x,y,classifier,test_idx=None,resolution=0.02):
15
59
 
16
60
 

2

コード

2020/07/07 16:07

投稿

assa
assa

スコア35

title CHANGED
File without changes
body CHANGED
@@ -10,15 +10,13 @@
10
10
  zのあたいは0か1です。
11
11
 
12
12
  よろしくお願いします。
13
-
13
+ ```pytthon
14
-
15
-
16
14
  def plot_decision_regions(x,y,classifier,test_idx=None,resolution=0.02):
17
15
 
18
16
 
19
-    markers=('s','x','o','^','v')
17
+ markers=('s','x','o','^','v')
20
-     colors=('blue','red','lightgreen','gray','cyan')
18
+ colors=('blue','red','lightgreen','gray','cyan')
21
-    cmap=ListedColormap(colors[:len(np.unique(y))])
19
+ cmap=ListedColormap(colors[:len(np.unique(y))])
22
20
 
23
21
 
24
22
  x1_min,x1_max=x[:,0].min()-1,x[:,0].max()+1
@@ -47,4 +45,6 @@
47
45
  lrgd=LogisticRegressionGD(eta=0.05,n_iter=1000,random_state=1)
48
46
  plot_decision_regions(x=x_train_01_subset,
49
47
  y=y_train_01_subset,
50
- classifier=lrgd)
48
+ classifier=lrgd)
49
+ コード
50
+ ```

1

もじ

2020/07/07 15:31

投稿

assa
assa

スコア35

title CHANGED
File without changes
body CHANGED
@@ -16,9 +16,9 @@
16
16
  def plot_decision_regions(x,y,classifier,test_idx=None,resolution=0.02):
17
17
 
18
18
 
19
- markers=('s','x','o','^','v')
19
+    markers=('s','x','o','^','v')
20
- colors=('blue','red','lightgreen','gray','cyan')
20
+     colors=('blue','red','lightgreen','gray','cyan')
21
- cmap=ListedColormap(colors[:len(np.unique(y))])
21
+    cmap=ListedColormap(colors[:len(np.unique(y))])
22
22
 
23
23
 
24
24
  x1_min,x1_max=x[:,0].min()-1,x[:,0].max()+1