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

回答編集履歴

5

d

2020/07/07 16:51

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -7,6 +7,6 @@
7
7
 
8
8
  ![イメージ説明](9da8000629acd92bb4066f7155375b9c.png)
9
9
 
10
- 書籍のコードは GitHub に上がっているようなので、動かない場合はこちらのものと比較すると思います。
10
+ 書籍のコードは GitHub に上がっているようなので、動かない場合はこちらのものと比較するとよいかと思います。
11
11
 
12
12
  [python-machine-learning-book-2nd-edition/ch03.ipynb at master · rasbt/python-machine-learning-book-2nd-edition](https://github.com/rasbt/python-machine-learning-book-2nd-edition/blob/master/code/ch03/ch03.ipynb)

4

d

2020/07/07 16:51

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -1,89 +1,12 @@
1
- > 決定境界を表直線せん(グラフを二つに分ける)
1
+ コピペミスだと思いますが、シグモイド関数の分母の指数が e^{-x} でなく、e^x になっいて、間違ってい
2
2
 
3
- その現象が再現しません。
3
+ ```diff
4
+ - 1.0 / (1.0 + np.exp(np.clip(z, -250, 250)))
4
- (200, 2) の配列 X (200,) の配列 y を与えてロジスティクス回帰モデルで学習し、質問の関数に渡したら、意図通り描画されました。
5
+ + 1.0 / (1.0 + np.exp(-np.clip(z, -250, 250)))
6
+ ```
5
7
 
6
- ```python
7
- import matplotlib.pyplot as plt
8
- import numpy as np
9
- from matplotlib.colors import ListedColormap
8
+ ![イメージ説明](9da8000629acd92bb4066f7155375b9c.png)
10
- from sklearn.datasets import make_blobs
11
- from sklearn.linear_model import LogisticRegression
12
9
 
10
+ 書籍のコードは GitHub に上がっているようなので、動かない場合はこちらのものと比較すると思います。
13
11
 
14
- class LogisticRegressionGD(object):
15
- def __init__(self, eta=0.05, n_iter=100, random_state=1):
16
- self.eta = eta
17
- self.n_iter = n_iter
18
- self.random_state = random_state
19
-
20
- def fit(self, X, y):
21
- rgen = np.random.RandomState(self.random_state)
22
- self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
23
- self.cost_ = []
24
-
25
- for i in range(self.n_iter):
26
- net_input = self.net_input(X)
27
- output = self.activation(net_input)
28
- errors = y - output
29
- self.w_[1:] += self.eta * X.T.dot(errors)
30
- self.w_[0] += self.eta * errors.sum()
31
- cost = -y.dot(np.log(output)) - ((1 - y).dot(np.log(1 - output)))
32
- self.cost_.append(cost)
33
- return self
34
-
35
- def net_input(self, X):
36
- return np.dot(X, self.w_[1:]) + self.w_[0]
37
-
38
- def activation(self, z):
39
- return 1.0 / (1.0 + np.exp(-np.clip(z, -250, 250)))
40
-
41
- def predict(self, X):
42
- return np.where(self.net_input(X) >= 0.0, 1, 0)
43
-
44
-
45
- def plot_decision_regions(x, y, classifier, test_idx=None, resolution=0.02):
46
- markers = ("s", "x", "o", "^", "v")
47
- colors = ("blue", "red", "lightgreen", "gray", "cyan")
48
- cmap = ListedColormap(colors[: len(np.unique(y))])
49
-
50
- x1_min, x1_max = x[:, 0].min() - 1, x[:, 0].max() + 1
51
- x2_min, x2_max = x[:, 1].min() - 1, x[:, 1].max() + 1
52
-
53
- xx1, xx2 = np.meshgrid(
54
- np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution)
55
- )
56
-
57
- z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
58
-
59
- z = z.reshape(xx1.shape)
60
- plt.contourf(xx1, xx2, z, alpha=0.8, cmap=cmap)
61
- plt.xlim(xx1.min(), xx1.max())
62
- plt.ylim(xx2.min(), xx2.max())
63
-
64
- for idx, c1 in enumerate(np.unique(y)):
65
- plt.scatter(
66
- x=x[y == c1, 0],
67
- y=x[y == c1, 1],
68
- alpha=0.8,
69
- c=colors[idx],
70
- marker=markers[idx],
71
- label=c1,
72
- edgecolor="black",
73
- )
74
-
75
-
76
- # データを作成する。
77
- X, y = make_blobs(
78
- random_state=0, n_samples=200, n_features=2, cluster_std=0.8, centers=2
79
- )
80
-
81
-
82
- # ロジスティック回帰モデルで学習する。
83
- model = LogisticRegressionGD(eta=0.05, n_iter=1000, random_state=1).fit(X, y)
84
- plot_decision_regions(X, y, model)
85
- ```
86
-
87
- ![イメージ説明](6b8febd777e2382cb6a353d0e82aebe7.jpeg)
88
-
89
- もし、上記のようにならない場合は、現状の図を質問欄に貼ってください
12
+ [python-machine-learning-book-2nd-edition/ch03.ipynb at master · rasbt/python-machine-learning-book-2nd-edition](https://github.com/rasbt/python-machine-learning-book-2nd-edition/blob/master/code/ch03/ch03.ipynb)

3

d

2020/07/07 16:49

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -6,15 +6,42 @@
6
6
  ```python
7
7
  import matplotlib.pyplot as plt
8
8
  import numpy as np
9
+ from matplotlib.colors import ListedColormap
9
10
  from sklearn.datasets import make_blobs
10
11
  from sklearn.linear_model import LogisticRegression
11
12
 
12
- # データを作成する。
13
- X, y = make_blobs(
14
- random_state=0, n_samples=200, n_features=2, cluster_std=0.8, centers=2
15
- )
16
13
 
14
+ class LogisticRegressionGD(object):
15
+ def __init__(self, eta=0.05, n_iter=100, random_state=1):
16
+ self.eta = eta
17
+ self.n_iter = n_iter
18
+ self.random_state = random_state
17
19
 
20
+ def fit(self, X, y):
21
+ rgen = np.random.RandomState(self.random_state)
22
+ self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
23
+ self.cost_ = []
24
+
25
+ for i in range(self.n_iter):
26
+ net_input = self.net_input(X)
27
+ output = self.activation(net_input)
28
+ errors = y - output
29
+ self.w_[1:] += self.eta * X.T.dot(errors)
30
+ self.w_[0] += self.eta * errors.sum()
31
+ cost = -y.dot(np.log(output)) - ((1 - y).dot(np.log(1 - output)))
32
+ self.cost_.append(cost)
33
+ return self
34
+
35
+ def net_input(self, X):
36
+ return np.dot(X, self.w_[1:]) + self.w_[0]
37
+
38
+ def activation(self, z):
39
+ return 1.0 / (1.0 + np.exp(-np.clip(z, -250, 250)))
40
+
41
+ def predict(self, X):
42
+ return np.where(self.net_input(X) >= 0.0, 1, 0)
43
+
44
+
18
45
  def plot_decision_regions(x, y, classifier, test_idx=None, resolution=0.02):
19
46
  markers = ("s", "x", "o", "^", "v")
20
47
  colors = ("blue", "red", "lightgreen", "gray", "cyan")
@@ -46,8 +73,14 @@
46
73
  )
47
74
 
48
75
 
76
+ # データを作成する。
77
+ X, y = make_blobs(
78
+ random_state=0, n_samples=200, n_features=2, cluster_std=0.8, centers=2
79
+ )
80
+
81
+
49
82
  # ロジスティック回帰モデルで学習する。
50
- model = LogisticRegression(solver="lbfgs", multi_class="multinomial").fit(X, y)
83
+ model = LogisticRegressionGD(eta=0.05, n_iter=1000, random_state=1).fit(X, y)
51
84
  plot_decision_regions(X, y, model)
52
85
  ```
53
86
 

2

d

2020/07/07 16:22

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -6,8 +6,6 @@
6
6
  ```python
7
7
  import matplotlib.pyplot as plt
8
8
  import numpy as np
9
- from matplotlib.colors import ListedColormap
10
- from sklearn import datasets
11
9
  from sklearn.datasets import make_blobs
12
10
  from sklearn.linear_model import LogisticRegression
13
11
 

1

d

2020/07/07 16:00

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -53,4 +53,6 @@
53
53
  plot_decision_regions(X, y, model)
54
54
  ```
55
55
 
56
- ![イメージ説明](6b8febd777e2382cb6a353d0e82aebe7.jpeg)
56
+ ![イメージ説明](6b8febd777e2382cb6a353d0e82aebe7.jpeg)
57
+
58
+ もし、上記のようにならない場合は、現状の図を質問欄に貼ってください