<commit_before>from collections import namedtuple
import numpy

class Perceptron:
  Sample = namedtuple('Sample', 'x y')

  def __init__(self, learning_rate=0.5):
    self.learning_rate = learning_rate

  def train(self, training_data): 
    # method accepts list of named tuples with sample.x (itself a list) and sample.y
    # e.g. namedtuple('Labeled_Sample', 'x y')
    self.dim = len(training_data[0].x)
    self.init_weights(self.dim)
    for sample in training_data:
      y_prime = numpy.dot(self.weight_vector, sample.x)
      if self.sign(y_prime) != self.sign(sample.y) or y_prime == 0:
        adjustment = [self.learning_rate * sample.y * x for x in sample.x]
        self.weight_vector = [a + b for (a,b) in zip(self.weight_vector, adjustment)]

  def classify(self, sampleX):
    # sampleX only includes the x component of a sample
    dotprod = numpy.dot(self.weight_vector, sampleX)
    if dotprod > 0:
      return 1
    else:
      return 0

  def init_weights(self, dim):
    self.weight_vector = [0] * dim

  def setLearningRate(self, number): 
    self.learning_rate = number
  
  def sign(self, number):<commit_msg>incprorated some code conventions<commit_after>from collections import namedtuple
import numpy

class Perceptron:
  Sample = namedtuple('Sample', 'x y')

  def __init__(self, learning_rate=0.5):
    self.learning_rate = learning_rate

  def train(self, training_data): 
    '''
    training_data example: namedtuple('Labeled_Sample', 'x y')
    for each sample in training_data: sample.x is data, sample.y is label
    '''
    self.dim = len(training_data[0].x)
    self.initWeights(self.dim)
    for sample in training_data:
      y_prime = numpy.dot(self.weight_vector, sample.x)
      if self.sign(y_prime) != self.sign(sample.y) or y_prime == 0:
        adjustment = [self.learning_rate * sample.y * x for x in sample.x]
        self.weight_vector = [a + b for (a,b) in zip(self.weight_vector, adjustment)]

  def classify(self, sampleX):
    '''
    sampleX includes ONLY the sample.x (data) component of a sample,
    and NOT the label which would be sample.y
    '''
    dotprod = numpy.dot(self.weight_vector, sampleX)
    if dotprod > 0:
      return 1
    else:
      return 0

  def initWeights(self, dim):
    self.weight_vector = [0] * dim

  def setLearningRate(self, number): 
    self.learning_rate = number
  
  def sign(self, number):
{'fraction_non_alphanumeric': 0.10836820083682008, 'fraction_numerical': 0.0066945606694560665, 'mean_word_length': 3.2926391382405744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1819564', 'n_tokens_mistral': 807, 'n_tokens_neox': 772, 'n_words': 222}