2017-08-02 71 views
0
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 

class Perceptron(object): 
    def __init__(self, eta=0.01, n_iter=10): 
     self.eta = eta 
     self.n_iter = n_iter 

    def fit(self, X, y): 
     self.w_ = np.zeros(1 + X.shape[1]) 
     self.errors_ = [] 

     for _ in range(self.n_iter): 
      errors = 0 
      for xi, target in zip(X, y): 
       update = self.eta * (target - self.predict(xi)) 
       self.w_[1:] += update * xi 
       self.w_[0] += update 
       errors += int(update != 0.0) 
      self.errors_.append(errors) 
     return self 

    def net_input(self, X): 
     """Calculate net input""" 
     return np.dot(X, self.w_[1:]) + self.w_[0] 

    def predict(self, X): 
     """Return class label after unit step""" 
     return np.where(self.net_input(X) >= 0.0, 1, -1) 


df = pd.read_csv('D:\\TUT\\IRIS_DATA\\iris_data.csv', header=None) 
print(df.tail()) 
y = df.iloc[0:100, 4].values 
#print(y) 

y = np.where(y == 'Iris-setosa', -1, 1) 
#print(y) 

X = df.iloc[0:100,0:2].values 
print(X) 

plt.scatter(X[:50, 0], X[:50,1], label='setosa', color='red', marker='o') 
plt.scatter(X[50:100,0], X[50:100, 1], label='versicolor', color='blue',marker='x') 
plt.xlabel('petal length') 
plt.ylabel('sepal length') 
plt.legend() 
plt.show() 

ppn = Perceptron(0.01, 100) 
ppn.fit(X,y) 
plt.plot(range(1,len(ppn.errors_)+1), ppn.errors_, marker='o') 
plt.xlabel('epoch') 
plt.ylabel('Number of misclassification') 
plt.show() 

上面的代碼是從一本書複製的,但不幸的是,錯誤沒有收斂到0虹膜數據。錯誤在兩個值3.0和2.0之間跳動。 需要幫助來了解我要去哪裏錯了。Python中的感知器代碼,虹膜數據不收斂

請考慮我在機器學習領域的新手,任何有識之士將不勝感激。

回答

0

我剛剛查看過你的代碼,發現了一些問題。 別擔心我已經糾正它。從您的代碼 enter image description here

結果從修正代碼

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 

class Perceptron(object): 
    def __init__(self, eta=0.01, n_iter=10): 
     self.eta = eta 
     self.n_iter = n_iter 

    def fit(self, X, y): 
     self.w_ = np.zeros(1 + X.shape[1]) 
     self.errors_ = [] 

     for _ in range(self.n_iter): 
      errors = 0 
      for xi, target in zip(X, y): 
       update = self.eta * (target - self.predict(xi)) 
       self.w_[1:] += update * xi 
       self.w_[0] += update 
       errors += int(update != 0.0) 
      self.errors_.append(errors) 
     return self 

    def net_input(self, X): 
     """Calculate net input""" 
     return np.dot(X, self.w_[1:]) + self.w_[0] 

    def predict(self, X): 
     """Return class label after unit step""" 
     return np.where(self.net_input(X) >= 0.0, 1, -1) 


df = pd.read_csv('iris.csv', header=None) 
print(df.tail()) 
y = df.iloc[0:100, 4].values 
#print(y) 

y = np.where(y == 'Iris-setosa', -1, 1) 
#print(y) 

X = df.iloc[0:100,[0,2]].values 
print(X) 

plt.scatter(X[:50, 0], X[:50,1], label='setosa', color='red', marker='o') 
plt.scatter(X[50:100,0], X[50:100, 1], label='versicolor', color='blue',marker='x') 
plt.xlabel('petal length') 
plt.ylabel('sepal length') 
plt.legend() 
plt.show() 

ppn = Perceptron(0.1, 10) 
ppn.fit(X,y) 
plt.plot(range(1,len(ppn.errors_)+1), ppn.errors_, marker='o') 
plt.xlabel('epoch') 
plt.ylabel('Number of misclassification') 
plt.show() 

結果 enter image description here