パーセプトロンの実装をしているのですが,二値出力を-1と1ではなく0と1にした場合収束しないのはなぜでしょうか.
二値出力とは任意に決めて良い値だと解釈していたのですが…
【追記】
#Assign 2行目に二値出力の設定がありますが,ここでは-1と1で予測する,期待通りに動くコードです
Python
1 2import numpy as np 3from pandas import DataFrame 4import pandas as pd 5import matplotlib.pyplot as plt 6 7 8#ImportData 9df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data') 10 11#Assign 12y= df.iloc[0:100,4].values 13y = np.where(y== 'Iris-setosa', -1,1) 14x = df.iloc[0:100,[0,2]].values 15 16#Perceptron 17class Perceptron(object): 18 def __init__(self, eta=0.01,n_iter=10): 19 self.eta = eta 20 self.n_iter = n_iter 21 22 def fit(self, x, y): 23 self.w_ = np.zeros(1+x.shape[1]) 24 self.errors_=[] 25 26 for _ in range(self.n_iter): 27 errors=0 28 for xi, target in zip(x, y): 29 update = self.eta * (target - self.predict(xi)) 30 self.w_[1:] += update * xi 31 self.w_[0] += update 32 errors += int(update != 0.0) 33 self.errors_.append(errors) 34 return self 35 36 def net_input(self, x): 37 return np.dot(x, self.w_[1:]) + self.w_[0] 38 39 def predict(self, x): 40 return np.where(self.net_input(x) >= 0.00, 1, -1) 41 42 43ppn = Perceptron(eta=0.1, n_iter=10) 44ppn.fit(x, y) 45plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker = 'o') 46plt.xlabel('Epochs') 47plt.ylabel('Number of misclassifications') 48plt.show() 49
Python機械学習プログラミング 達人データサイエンティストによる理論と実践
https://book.impress.co.jp/books/1115101122
p.30より
回答1件
あなたの回答
tips
プレビュー