Skip to content

Commit

Permalink
Merge e5d21fc into bdaa7c3
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 24, 2018
2 parents bdaa7c3 + e5d21fc commit ae41e2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
4 changes: 4 additions & 0 deletions lib/scoruby/models/random_forest/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def continuous_features
@continuous_features ||= fetch_continuous_features
end

def regression?
@xml.xpath("//MiningModel[@functionName='regression']").any?
end

private

def fetch_continuous_features
Expand Down
23 changes: 17 additions & 6 deletions lib/scoruby/models/random_forest/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@ module RandomForest
class Model
extend Forwardable
def_delegators :@data, :decision_trees, :categorical_features,
:continuous_features
:continuous_features, :regression?

def initialize(xml)
@data = Data.new(xml)
end

def score(features)
decisions_count = decisions_count(features)
decision = decisions_count.max_by { |_, v| v }
{
label: decision[0],
score: decision[1] / decisions_count.values.reduce(0, :+).to_f
}

if regression?
{
response: sum(decisions_count.map { |k, v| k.to_f * v }) / sum(decisions_count.values)
}
else
decision = decisions_count.max_by { |_, v| v }
{
label: decision[0],
score: decision[1] / sum(decisions_count.values).to_f
}
end
end

def decisions_count(features)
Expand All @@ -43,6 +50,10 @@ def aggregate_decisions(decisions)
counts[score] += 1
end
end

def sum(values)
values.reduce(0, :+)
end
end
end
end
Expand Down

0 comments on commit ae41e2e

Please sign in to comment.