Ejemplo n.º 1
0
 def train(self, data, num_iters):
     shouldContinue = True
     for iteration_count in xrange(num_iters):
         num_errors = 0
         for vector, label in data:
             activation = dot(self.weights, vector) + self.bias
             if label * activation <= 0:
                 # Update weights
                 for i in xrange(self.dimensions):
                     self.weights[i] += label * vector[i]
                     self.cached_weights[i] += label * vector[i] * self.counter
                 self.bias += label
                 self.cached_bias += label * self.counter
                 num_errors += 1
             self.counter += 1
         if self.onIteration is not None:
             shouldContinue = self.onIteration(iteration_count, self.weights, self.bias, num_errors, len(data))
         if shouldContinue is False:
             break
     return (
         array_subtract(self.weights, scale_array(self.cached_weights, 1 / self.counter)),
         self.bias - self.cached_bias / self.counter,
     )
Ejemplo n.º 2
0
 def get_weights(self):
     return array_subtract(self.weights, scale_array(self.cached_weights, 1 / self.counter))
Ejemplo n.º 3
0
 def classify(self, input_vector):
     weights = array_subtract(self.weights, scale_array(self.cached_weights, 1 / self.counter))
     bias = self.bias - self.cached_bias / self.counter
     return sign(dot(weights, input_vector) + bias)