Warning: Declaration of action_plugin_tablewidth::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /s/bach/b/class/cs545/public_html/fall16/lib/plugins/tablewidth/action.php on line 93
code:perceptron [CS545 fall 2016]

User Tools

Site Tools


code:perceptron

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
code:perceptron [2015/08/28 10:35]
asa
code:perceptron [2016/08/09 10:25]
127.0.0.1 external edit
Line 5: Line 5:
  
 <file python perceptron.py>​ <file python perceptron.py>​
- 
  
 import numpy as np import numpy as np
Line 14: Line 13:
     """​An implementation of the perceptron algorithm.     """​An implementation of the perceptron algorithm.
     Note that this implementation does not include a bias term"""​     Note that this implementation does not include a bias term"""​
- 
-    attributes = {'​max_iterations':​ 50, 
-                  '​learning_rate'​ : 0.2} 
  
     def __init__(self,​ max_iterations=100,​ learning_rate=0.2) :     def __init__(self,​ max_iterations=100,​ learning_rate=0.2) :
Line 24: Line 20:
  
     def fit(self, X, y) :     def fit(self, X, y) :
 +        """​
 +        Train a classifier using the perceptron training algorithm.
 +        After training the attribute '​w'​ will contain the perceptron weight vector.
  
 +        Parameters
 +        ----------
 +
 +        X : ndarray, shape (num_examples,​ n_features)
 +        Training data.
 +
 +        y : ndarray, shape (n_examples,​)
 +        Array of labels.
 +        ​
 +        """​
         self.w = np.zeros(len(X[0]))         self.w = np.zeros(len(X[0]))
         converged = False         converged = False
Line 34: Line 43:
                     self.w = self.w + y[i] * self.learning_rate * X[i]                     self.w = self.w + y[i] * self.learning_rate * X[i]
                     converged = False                     converged = False
 +                    plot_data(X,​ y, self.w)
             iterations += 1             iterations += 1
-            plot_data(X,​ y, self.w) 
         self.converged = converged         self.converged = converged
         if converged :         if converged :
Line 44: Line 53:
             ​             ​
     def predict(self,​ X) :     def predict(self,​ X) :
 +        """​
 +        make predictions using a trained linear classifier
  
 +        Parameters
 +        ----------
 +
 +        X : ndarray, shape (num_examples,​ n_features)
 +        Training data.
 +        """​
 +        ​
         scores = np.dot(self.w,​ X)         scores = np.dot(self.w,​ X)
         return np.sign(scores)         return np.sign(scores)
- 
  
 def generate_separable_data(N) : def generate_separable_data(N) :
Line 71: Line 88:
  
 if __name__=='​__main__'​ : if __name__=='​__main__'​ :
-    X,y,w = generate_separable_data(20)+    X,y,w = generate_separable_data(40)
     p = Perceptron()     p = Perceptron()
     p.fit(X,y)     p.fit(X,y)
  
 </​file>​ </​file>​
code/perceptron.txt ยท Last modified: 2016/09/08 11:04 by asa