Created
          October 10, 2012 13:34 
        
      - 
      
 - 
        
Save chokkan/3865688 to your computer and use it in GitHub Desktop.  
    Perceptron
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | def update(w, x, y): | |
| a = 0. | |
| for i in range(len(w)): | |
| a += w[i] * x[i] | |
| if a * y <= 0: | |
| for i in range(len(w)): | |
| w[i] += y * x[i] | |
| def classify(w, x): | |
| a = 0. | |
| for i in range(len(w)): | |
| a += w[i] * x[i] | |
| return (0. < a) | |
| if __name__ == '__main__': | |
| w = [0.] * 9 | |
| D = ( | |
| ((1, 1, 1, 1, 1, 1, 0, 0, 0), +1), | |
| ((1, 0, 0, 0, 1, 1, 1, 1, 1), -1), | |
| ) | |
| update(w, D[0][0], D[0][1]) | |
| update(w, D[1][0], D[1][1]) | |
| print classify(w, D[0][0]) | |
| print classify(w, D[1][0]) | |
| print w | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment