Class: RFModel

Inherits:
VectorModel show all
Defined in:
lib/rbbt/vector/model/random_forest.rb

Instance Attribute Summary

Attributes inherited from VectorModel

#balance, #bar, #directory, #eval_model, #extract_features, #factor_levels, #features, #labels, #model_file, #model_options, #names, #post_process, #train_model

Instance Method Summary collapse

Methods inherited from VectorModel

R_eval, R_run, R_train, #__load_method, #add, #add_list, #balance_labels, #clear, #cross_validation, #eval, #eval_list, f1_metrics, #run, #save_models, #train

Constructor Details

#initialize(dir) ⇒ RFModel

Returns a new instance of RFModel.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rbbt/vector/model/random_forest.rb', line 3

def initialize(dir)
  super(dir)

  @extract_features = Proc.new{|element|
    element
  }

  @train_model =<<-EOF
rbbt.require("randomForest");
model = randomForest(as.factor(label) ~ ., data = features);
  EOF
 
  @eval_model =<<-EOF
rbbt.require("randomForest");
pred = names(model$forest$xlevels)
for (p in pred) { 
if (is.factor(features[[p]])) { 
    features[[p]] = factor(features[[p]], levels=model$forest$xlevels[[p]])
  } 
}
label = predict(model, features);
  EOF
end

Instance Method Details

#importanceObject



27
28
29
30
31
32
33
34
35
# File 'lib/rbbt/vector/model/random_forest.rb', line 27

def importance
  TmpFile.with_file do |tmp|
    tsv = R.run <<-EOF
load(file="#{model_file}");
rbbt.tsv.write('#{tmp}', model$importance)
    EOF
    TSV.open(tmp)
  end
end