lib/ai4r/classifiers/naive_bayes.rb in ai4r-1.9 vs lib/ai4r/classifiers/naive_bayes.rb in ai4r-1.11
- old
+ new
@@ -52,10 +52,11 @@
# b = NaiveBayes.new.
# set_parameters({:m=>3}).
# build data
# b.eval(["Red", "SUV", "Domestic"])
#
+
class NaiveBayes < Classifier
parameters_info :m => "Default value is set to 0. It may be set to a value greater than " +
"0 when the size of the dataset is relatively small"
@@ -148,11 +149,11 @@
array.inject(0.0){|b, i| b+i}
end
# returns the name of the class when the index is found
def index_to_klass(index)
- @klass_index.has_value?(index) ? @klass_index.index(index) : nil
+ @klass_index.has_value?(index) ? @klass_index.key(index) : nil
end
# initializes @values and @klass_index; maps a certain value to a uniq index
def initialize_klass_index
@klasses.each_with_index do |dl, index|
@@ -254,6 +255,11 @@
end
end
end
end
+end
+
+# Monkeypatch to support both ruby 1.8 and 1.9 (key vs index method)
+class Hash
+ alias_method(:key, :index) unless method_defined?(:key)
end