Sha256: b19a45df0cb0f8a28f8e9c33d85d5633a5a6d5c36daa80c83d8056a81b2dbe5b

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

require 'set'

module Ai4r
  
  module Classifiers

    NUMERIC_CLASS_TYPE = 1
    NOMINAL_CLASS_TYPE = 2
    
    module ClassifierHelper
      
      def default_data_labels(data_examples)
        data_labels = []
        data_examples[0][0..-2].each_index do |i|
          data_labels[i] = "attribute_#{i+1}" 
        end
        data_labels[data_labels.length]="class_value"
        return data_labels
      end
      
      def check_data_examples(data_examples)
        if !data_examples || data_examples.empty?
          raise ArgumentError,"Examples data set must not be empty." 
        elsif !data_examples.first.is_a?(Array)
          raise ArgumentError,"Unkown format for example data." 
        end
      end
      
      # Returns attributes number, including class attribute
      def num_attributes(data_examples)
        return 0 if !data_examples || data_examples.empty? || !data_examples.first.is_a?(Array)
        return data_examples.first.size
      end

      # Returns an array with the domain of each attribute (Set instance 
      # containing all possible values)
      # Return example:
      # => [#<Set: {"New York", "Chicago"}>, 
      #     #<Set: {"<30", "[30-50)", "[50-80]", ">80"}>, 
      #     #<Set: {"M", "F"}>, 
      #     #<Set: {"Y", "N"}>]
      def build_domains(data_examples)
        domains = Array.new(num_attributes(data_examples)) { Set.new }
        data_examples.each do |data|
          data.each_index {|attr_index| domains[attr_index] << data[attr_index]}
        end
        return domains
      end
      
    end
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ai4r-1.2 lib/ai4r/classifiers/classifier_helper.rb