Class: Ai4r::Classifiers::RandomForest
- Inherits:
-
Classifier
- Object
- Classifier
- Ai4r::Classifiers::RandomForest
- Defined in:
- lib/ai4r/classifiers/random_forest.rb
Overview
RandomForest ensemble classifier built from decision trees.
Instance Attribute Summary collapse
-
#features ⇒ Object
readonly
Returns the value of attribute features.
-
#trees ⇒ Object
readonly
Returns the value of attribute trees.
Instance Method Summary collapse
- #build(data_set) ⇒ Object
- #eval(data) ⇒ Object
- #get_rules ⇒ Object
-
#initialize ⇒ RandomForest
constructor
A new instance of RandomForest.
Methods included from Data::Parameterizable
#get_parameters, included, #set_parameters
Constructor Details
#initialize ⇒ RandomForest
Returns a new instance of RandomForest.
26 27 28 29 30 31 32 |
# File 'lib/ai4r/classifiers/random_forest.rb', line 26 def initialize super() @n_trees = 10 @sample_size = nil @feature_fraction = nil @random_seed = nil end |
Instance Attribute Details
#features ⇒ Object (readonly)
Returns the value of attribute features.
24 25 26 |
# File 'lib/ai4r/classifiers/random_forest.rb', line 24 def features @features end |
#trees ⇒ Object (readonly)
Returns the value of attribute trees.
24 25 26 |
# File 'lib/ai4r/classifiers/random_forest.rb', line 24 def trees @trees end |
Instance Method Details
#build(data_set) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ai4r/classifiers/random_forest.rb', line 34 def build(data_set) data_set.check_not_empty rng = @random_seed ? Random.new(@random_seed) : Random.new num_attributes = data_set.data_labels.length - 1 frac = @feature_fraction || (Math.sqrt(num_attributes) / num_attributes) feature_count = [1, (num_attributes * frac).round].max @sample_size ||= data_set.data_items.length @trees = [] @features = [] @n_trees.times do sampled = Array.new(@sample_size) { data_set.data_items.sample(random: rng) } feature_idx = (0...num_attributes).to_a.sample(feature_count, random: rng) tree_items = sampled.map do |item| values = feature_idx.map { |i| item[i] } values + [item.last] end labels = feature_idx.map { |i| data_set.data_labels[i] } + [data_set.data_labels.last] ds = Ai4r::Data::DataSet.new(data_items: tree_items, data_labels: labels) @trees << ID3.new.build(ds) @features << feature_idx end self end |
#eval(data) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/ai4r/classifiers/random_forest.rb', line 58 def eval(data) votes = Votes.new @trees.each_with_index do |tree, idx| sub_data = @features[idx].map { |i| data[i] } votes.increment_category(tree.eval(sub_data)) end votes.get_winner end |
#get_rules ⇒ Object
67 68 69 |
# File 'lib/ai4r/classifiers/random_forest.rb', line 67 def get_rules 'RandomForest does not support rule extraction.' end |