nimbus 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/nimbus/forest.rb CHANGED
@@ -78,7 +78,8 @@ module Nimbus
78
78
  trees.each do |t|
79
79
  individual_prediction << Nimbus::Tree.traverse(t, individual.snp_list)
80
80
  end
81
- @predictions[individual.id] = Nimbus::LossFunctions.majority_class_in_list(individual_prediction, @options.tree[:classes])
81
+ class_sizes = Nimbus::LossFunctions.class_sizes_in_list(individual_prediction, @options.tree[:classes]).map{|p| (p/individual_prediction.size.to_f).round(3)}
82
+ @predictions[individual.id] = Hash[@options.tree[:classes].zip class_sizes].to_s
82
83
  }
83
84
  end
84
85
 
@@ -45,7 +45,7 @@ module Nimbus
45
45
  # where Pj is the relative frequency of class j in T
46
46
  def gini_index(ids, value_table, classes)
47
47
  total_size = ids.size.to_f
48
- gini = 1 - class_sizes(ids, value_table, classes).inject(0.0){|sum, size|
48
+ gini = 1 - class_sizes(ids, value_table, classes).inject(0.0){|sum, size|
49
49
  sum + (size/total_size)**2}
50
50
  gini.round(5)
51
51
  end
@@ -70,6 +70,11 @@ module Nimbus
70
70
  def class_sizes(ids, value_table, classes)
71
71
  classes.map{|c| ids.count{|i| value_table[i] == c}}
72
72
  end
73
+
74
+ # Array with the list of sizes of each class in the given list of classes.
75
+ def class_sizes_in_list(list, classes)
76
+ classes.map{|c| list.count{|i| i == c}}
77
+ end
73
78
  end
74
79
 
75
80
  end
@@ -1,3 +1,3 @@
1
1
  module Nimbus
2
- VERSION = "2.1.0"
2
+ VERSION = "2.2.0"
3
3
  end
data/spec/forest_spec.rb CHANGED
@@ -116,7 +116,8 @@ describe Nimbus::Forest do
116
116
  tree_structure.each do |t|
117
117
  individual_prediction << Nimbus::Tree.traverse(t, individual.snp_list)
118
118
  end
119
- expected_predictions[individual.id] = Nimbus::LossFunctions.majority_class_in_list(individual_prediction, @config.tree[:classes])
119
+ class_sizes = Nimbus::LossFunctions.class_sizes_in_list(individual_prediction, @config.tree[:classes]).map{|p| (p/individual_prediction.size.to_f).round(3)}
120
+ expected_predictions[individual.id] = Hash[@config.tree[:classes].zip class_sizes].to_s
120
121
  }
121
122
 
122
123
  @forest.traverse
@@ -60,6 +60,19 @@ describe Nimbus::LossFunctions do
60
60
  Nimbus::LossFunctions.majority_class_in_list(list, classes).should == 'A'
61
61
  end
62
62
 
63
+ it "method for class sizes" do
64
+ ids = [1,2,3,4,5,7,85]
65
+ values = {1 => 'B', 2 => 'C', 3 => 'A', 4 => 'A', 5 => 'C', 7 => 'B', 85 => 'C'} #2A, 2B, 3C
66
+ classes = ['A', 'B', 'C']
67
+ Nimbus::LossFunctions.class_sizes(ids, values, classes).should == [2, 2, 3]
68
+ end
69
+
70
+ it "method for class sizes in list" do
71
+ list = %w(A A A B B B C A B C A B A) # 6A, 5B, 2C
72
+ classes = ['A', 'B', 'C']
73
+ Nimbus::LossFunctions.class_sizes_in_list(list, classes).should == [6, 5, 2]
74
+ end
75
+
63
76
  it "Gini index" do
64
77
  ids = [1,2,3,4,5,7]
65
78
  values = {1 => 'B', 2 => 'C', 3 => 'A', 4 => 'A', 5 => 'C', 7 => 'C'} #3C, 2A, 1B
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nimbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-28 00:00:00.000000000 Z
13
+ date: 2012-07-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &2152757020 !ruby/object:Gem::Requirement
17
+ requirement: &2153196200 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: 2.11.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2152757020
25
+ version_requirements: *2153196200
26
26
  description: Nimbus is a Ruby gem to implement Random Forest in a genomic selection
27
27
  context.
28
28
  email: