import sys
import numpy as np
import lxmls.classifiers.linear_classifier as lc

class Perceptron(lc.LinearClassifier):

    def __init__(self,nr_epochs = 10,learning_rate = 1, averaged = True):
        lc.LinearClassifier.__init__(self)
        self.trained = False
        self.nr_epochs = nr_epochs
        self.learning_rate = learning_rate
        self.averaged = averaged
        self.params_per_round = []

    def train(self,x,y):
        self.params_per_round = []
        x_orig = x[:,:]
        x = self.add_intercept_term(x)
        nr_x,nr_f = x.shape
        nr_c = np.unique(y).shape[0]
        w = np.zeros((nr_f,nr_c))
        ## Randomize the examples
        perm = np.random.permutation(nr_x)
        for epoch_nr in xrange(self.nr_epochs):
             for nr in xrange(nr_x):
                #print "iter %i" %( epoch_nr*nr_x + nr)
                inst = perm[nr]
                y_hat = self.get_label(x[inst:inst+1,:],w)
                if(y[inst:inst+1,0] != y_hat):
                    #Increase features of th e truth
                    w[:,y[inst:inst+1,0]] += self.learning_rate*x[inst:inst+1,:].transpose()
                    #Decrease features of the prediction
                    w[:,y_hat] += -1*self.learning_rate*x[inst:inst+1,:].transpose()
             self.params_per_round.append(w.copy())
             self.trained = True
             y_pred = self.test(x_orig,w)
             acc = self.evaluate(y,y_pred)
             self.trained = False
             print "Rounds: %i Accuracy: %f" %( epoch_nr,acc) 
        self.trained = True
        
        if(self.averaged == True):
            new_w = 0
            for old_w in self.params_per_round:
                new_w += old_w
            new_w = new_w / len(self.params_per_round)
            return new_w
        return w



@@ -73,16 +73,17 @@
  as lc%0A%0A
+%0A
 class Pe
@@ -113,17 +113,16 @@
 ifier):%0A
-%0A
     def 
@@ -135,16 +135,17 @@
 __(self,
+ 
 nr_epoch
@@ -149,14 +149,13 @@
 ochs
- = 
+=
 10,
+ 
 lear
@@ -163,19 +163,17 @@
 ing_rate
- = 
+=
 1, avera
@@ -175,19 +175,17 @@
 averaged
- = 
+=
 True):%0A 
@@ -425,11 +425,21 @@
 elf,
+ 
 x,
-y
+ y, seed=1
 ):%0A 
@@ -493,16 +493,17 @@
 g = x%5B:,
+ 
 :%5D%0A     
@@ -549,16 +549,17 @@
    nr_x,
+ 
 nr_f = x
@@ -629,16 +629,17 @@
 s((nr_f,
+ 
 nr_c))%0A 
@@ -649,34 +649,133 @@
     
-## Randomize the examples%0A
+for epoch_nr in xrange(self.nr_epochs):%0A%0A            # use seed to generate permutation%0A            np.random.seed(seed)%0A    
     
@@ -813,24 +813,25 @@
 n(nr_x)%0A
+%0A
         
 for epoc
@@ -826,49 +826,107 @@
     
-for epoch_nr in xrange(self.nr_epochs):%0A 
+    # change the seed so next epoch we don't get the same permutation%0A            seed = seed + 1%0A%0A
     
@@ -1099,17 +1099,22 @@
 inst
-+
+ + 
 1,
+ 
 :%5D,
+ 
 w)%0A
+%0A
     
@@ -1127,16 +1127,17 @@
       if
+ 
 (y%5Binst:
@@ -1140,19 +1140,22 @@
 nst:inst
-+
+ + 
 1,
+ 
 0%5D != y_
@@ -1160,16 +1160,17 @@
 y_hat):%0A
+%0A
         
@@ -1238,16 +1238,17 @@
     w%5B:,
+ 
 y%5Binst:i
@@ -1250,19 +1250,22 @@
 nst:inst
-+
+ + 
 1,
+ 
 0%5D%5D += s
@@ -1273,33 +1273,35 @@
 lf.learning_rate
-*
+ * 
 x%5Binst:inst+1,:%5D
@@ -1287,35 +1287,38 @@
 te * x%5Binst:inst
-+
+ + 
 1,
+ 
 :%5D.transpose()%0A 
@@ -1308,32 +1308,33 @@
  :%5D.transpose()%0A
+%0A
                 
@@ -1398,16 +1398,17 @@
     w%5B:,
+ 
 y_hat%5D +
@@ -1411,17 +1411,19 @@
 t%5D += -1
-*
+ * 
 self.lea
@@ -1432,17 +1432,19 @@
 ing_rate
-*
+ * 
 x%5Binst:i
@@ -1450,11 +1450,14 @@
 inst
-+
+ + 
 1,
+ 
 :%5D.t
@@ -1467,17 +1467,17 @@
 spose()%0A
- 
+%0A
         
@@ -1523,33 +1523,32 @@
 ())%0A            
- 
 self.trained = T
@@ -1547,25 +1547,24 @@
 ined = True%0A
- 
             
@@ -1593,12 +1593,12 @@
 rig,
+ 
 w)%0A
- 
     
@@ -1627,16 +1627,17 @@
 luate(y,
+ 
 y_pred)%0A
@@ -1640,33 +1640,32 @@
 ed)%0A            
- 
 self.trained = F
@@ -1665,25 +1665,24 @@
 ned = False%0A
- 
             
@@ -1714,16 +1714,17 @@
 y: %25f%22 %25
+ 
 ( epoch_
@@ -1730,13 +1730,13 @@
 _nr,
+ 
 acc)
- 
 %0A   
@@ -1760,24 +1760,16 @@
  = True%0A
-        
 %0A       
@@ -1771,16 +1771,17 @@
       if
+ 
 (self.av
@@ -1997,8 +1997,10 @@
 urn w%0A%0A%0A
+%0A%0A

Update perceptron.py