質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Q&A

解決済

1回答

1295閲覧

決定領域のコード

assa

総合スコア35

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

0グッド

0クリップ

投稿2020/07/07 15:27

編集2020/07/07 16:34

イメージ説明
質問は決定領域のコードでこの下のコードを実行すると、
データーはプロっとされるのですが、
決定境界を表す直線が出てきません。(グラフを二つに分ける)
どこを変えるといいのでしょうか。
間違えている箇所がわかりません。
教えてください。
plot_decision_regionsこの関数が決定領域の図です。

特徴量は2つです。
zのあたいは0か1です。

よろしくお願いします。

pytthon

1 2import numpy as np 3from sklearn import datasets 4from sklearn.model_selection import train_test_split 5import matplotlib.pyplot as plt 6%matplotlib inline 7from matplotlib.colors import ListedColormap 8 9iris=datasets.load_iris() 10x=iris.data[:,[2,3]] 11y=iris.target 12x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1,stratify=y) 13 14plot_decision_regions(x=x_train_01_subset, 15 y=y_train_01_subset, 16 classifier=lrgd) 17 18class LogisticRegressionGD: 19 def __init__(self,eta=0.05,n_iter=100,random_state=1): 20 self.eta=eta 21 self.n_iter=n_iter 22 self.random_state=random_state 23 24 def fit(self,x,y): 25 rgen=np.random.RandomState(self.random_state) #インスタンス 26 self.w_=rgen.normal(loc=0.0,scale=0.01,size=1+x.shape[1]) #特徴量+バイアス 27 self.cost_=[] 28 29 for i in range(self.n_iter): 30 net_input=self.net_input(x) 31 out_put=self.activation(net_input) 32 errors=(y-out_put) 33 self.w_[1:]+=self.eta*x.T.dot(errors) 34 self.w_[0]+=self.eta*errors.sum() 35 36 cost=-y.dot(np.log(out_put))-((1-y).dot(np.log(1-out_put))) 37 self.cost_.append(cost) 38 39 40 def net_input(self,x): 41 return np.dot(x,self.w_[1:])+self.w_[0] 42 43 def activation(self,z): 44 return 1./(1.+np.exp(np.clip(z,-250,250))) 45 46 def predict(self,x): 47 return np.where(self.activation(self.net_input(x))>=0.5,1,0) 48 49 50def plot_decision_regions(x,y,classifier,test_idx=None,resolution=0.02): 51 52 53 markers=('s','x','o','^','v') 54 colors=('blue','red','lightgreen','gray','cyan') 55 cmap=ListedColormap(colors[:len(np.unique(y))]) 56 57 58 x1_min,x1_max=x[:,0].min()-1,x[:,0].max()+1 59 x2_min,x2_max=x[:,1].min()-1,x[:,1].max()+1 60 61 xx1,xx2=np.meshgrid(np.arange(x1_min,x1_max,resolution), 62 np.arange(x2_min,x2_max,resolution)) 63 64 z=classifier.predict(np.array([xx1.ravel(),xx2.ravel()]).T) 65 66 z=z.reshape(xx1.shape) 67 plt.contourf(xx1,xx2,z,alpha=0.8,cmap=cmap) 68 plt.xlim(xx1.min(),xx1.max()) 69 plt.ylim(xx2.min(),xx2.max()) 70 71 for idx,c1 in enumerate(np.unique(y)): 72 plt.scatter(x=x[y==c1,0],y=x[y==c1,1], 73 alpha=0.8, 74 c=colors[idx], 75 marker=markers[idx], 76 label=c1, 77 edgecolor='black') 78 79 80x_train_01_subset=x_train[(y_train==0)|(y_train==1)] 81y_train_01_subset=y_train[(y_train==0)|(y_train==1)] 82lrgd=LogisticRegressionGD(eta=0.05,n_iter=1000,random_state=1) 83lrgd.fit(x_train_01_subset,y_train_01_subset) 84plot_decision_regions(x=x_train_01_subset, 85 y=y_train_01_subset, 86 classifier=lrgd) 87コード

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

コピペミスだと思いますが、シグモイド関数の分母の指数が e^{-x} でなく、e^x になっていて、間違っています。

diff

1- 1.0 / (1.0 + np.exp(np.clip(z, -250, 250))) 2+ 1.0 / (1.0 + np.exp(-np.clip(z, -250, 250)))

イメージ説明

書籍のコードは GitHub に上がっているようなので、動かない場合はこちらのものと比較するとよいかと思います。

python-machine-learning-book-2nd-edition/ch03.ipynb at master · rasbt/python-machine-learning-book-2nd-edition

投稿2020/07/07 15:58

編集2020/07/07 16:51
tiitoi

総合スコア21956

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

assa

2020/07/07 16:04

回答ありがとうございました。 tiitoiさんのコード実行させてもらいましたが自分もできました ではほかのこーどのみすということですね
assa

2020/07/07 16:04

iris=datasets.load_iris() x=iris.data[:,[2,3]] y=iris.target x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1,stratify=y) x_train_01_subset=x_train[(y_train==0)|(y_train==1)] y_train_01_subset=y_train[(y_train==0)|(y_train==1)] lrgd=LogisticRegressionGD(eta=0.05,n_iter=1000,random_state=1) lrgd.fit(x_train_01_subset,y_train_01_subset) plot_decision_regions(x=x_train_01_subset, y=y_train_01_subset, classifier=lrgd) データはこれです
tiitoi

2020/07/07 16:09

LogisticRegressionGD は自作したものでしょうか? その部分を sklearn の LogisticRegression に置き換えたらコメントのコードでも描画できました
assa

2020/07/07 16:18

python機械学習プログラミングという本の コードを書いたんです。 ありがとうございます。 ロジスティックがどこか間違っているというこがわかりました
tiitoi

2020/07/07 16:25 編集

ーーーー 訂正
assa

2020/07/07 16:26

画像はりました
tiitoi

2020/07/07 16:29

コピペで動作するコードを貼れますか? (import などすべて含む)
tiitoi

2020/07/07 16:31

再現したので、大丈夫です。ちょっと見てみます
assa

2020/07/07 16:32

はりました おそくてすみません
tiitoi

2020/07/07 16:50

原因がわかりました。self.activation() 内の式が間違っています。
assa

2020/07/07 16:53

本当にありがとうございました
assa

2020/07/07 16:54

実行したらできました。 ありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問