View Single Post
Old 04-17-2012, 01:33 PM
rukacity rukacity is offline
Join Date: Apr 2012
Posts: 21
Default Re: Perceptron Learning Algorithm

Originally Posted by zsero View Post
I've written the algorithm just based on the lecture and it worked. Here is the "core" of the algorithm (written in Python / Numpy):

w = np.zeros( 3 )
done = False

while not done:
    wrongpoints = 0
    for p in points:
        if np.sign(, p) ) != targetFunction( p ):
            w = np.add( w, targetFunction( p ) * p )
            wrongpoints += 1
    if wrongpoints == 0:
        done = True
If anyone is interested in the Python implementation, here is my full code with plotting:

and the one for the experiments:
Argh! I am new to Python and did not know about numpy. It looks so much more concise than my code that has all the vector multiplication and what not. One thing I noticed is that you keep adding to weights for each wrongpoint whereas Yaser had mentioned to adjust it only once per iteration.

Correction: My bad! Missed the 'break' statement
Reply With Quote