<commit_before>require 'matrix'

module ML
  module Learner
    # Implementation of Perceptron Learning Algorithm
    class PerceptronLearner
      # Initialize a perceptron learner
      #
      # @param [Integer] dim the number of dimension
      def initialize dim
        @dim = dim
        @w = Matrix.column_vector(Array.new(dim + 1, 0))
      end

      # Train with supervised data
      #
      # @param [Hash] data supervised input data (mapping from array to integer)
      def train! data
        @update = 0
        while true
          misclassified = false

          for dat, result in data
            aug_data = Matrix.column_vector(dat)

            if wrongly_classify aug_data, result
              misclassified = true

              update_vector result, aug_data
              @update += 1
              break
            end
          end

          break unless misclassified
        end
      end

      # The final coefficient of the line
      #
      # @return [Array] [a,b,c] for ax+by+c=0
      def line
        @w.column(0).to_a
      end

      # The number for updates
      #
      # @return [Integer] update count
      def update_count
        @update<commit_msg>Adding threshold for perceptron learning<commit_after>require 'matrix'

module ML
  module Learner
    # Implementation of Perceptron Learning Algorithm
    class PerceptronLearner
      # Initialize a perceptron learner
      #
      # @param [Integer] dim the number of dimension
      def initialize dim, thres = 1.0/0
        @dim = dim
        @w = Matrix.column_vector(Array.new(dim + 1, 0))
      end

      # Train with supervised data
      #
      # @param [Hash] data supervised input data (mapping from array to integer)
      # @param [Numeric] threshold the upper bound of the traning iteration
      def train! data, thres = 1.0/0
        @update = 0
        @threshold = thres
        while true
          break if @update >= @threshold
          misclassified = false

          for dat, result in data
            aug_data = Matrix.column_vector(dat)

            if wrongly_classify aug_data, result
              misclassified = true

              update_vector result, aug_data
              @update += 1
              break
            end
          end

          break unless misclassified
        end
      end

      # The final coefficient of the line
      #
      # @return [Array] [a,b,c] for ax+by+c=0
      def line
        @w.column(0).to_a
      end

      # The number for updates
      #
      # @return [Integer] update count
      def update_count
        @update
{'fraction_non_alphanumeric': 0.0622358816749904, 'fraction_numerical': 0.006915097963887821, 'mean_word_length': 1.6900826446280992, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '1325627', 'n_tokens_mistral': 733, 'n_tokens_neox': 682, 'n_words': 243}