Class: Clustering::Simple
- Inherits:
-
Object
- Object
- Clustering::Simple
- Defined in:
- lib/clustering/simple.rb
Instance Method Summary collapse
- #clusters(threshold = 2) ⇒ Object
-
#connections ⇒ Object
create a hash of hashes which contains the grade of linkage between elements connections[4] #=> 2.
-
#initialize(data) ⇒ Simple
constructor
input: “data” should be an array of arrays each array should contain elements that are connected to each other elements are counted as often as they occur in the arrays this class does not make sure to only have unique elements in an array [ [1,2,3], [2,3], [3,1] ].
- #relations ⇒ Object
- #strength?(a, b) ⇒ Boolean
Constructor Details
#initialize(data) ⇒ Simple
input: “data” should be an array of arrays each array should contain elements that are connected to each other elements are counted as often as they occur in the arrays this class does not make sure to only have unique elements in an array [
[1,2,3],
[2,3],
[3,1]
]
14 15 16 |
# File 'lib/clustering/simple.rb', line 14 def initialize(data) @original = data end |
Instance Method Details
#clusters(threshold = 2) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/clustering/simple.rb', line 40 def clusters(threshold = 2) relations.inject(Hash.new{|h,k| h[k] = Array.new }) do |akk, e| key, values = *e keys = values.keys.select{|s| s >= threshold}.sort.reverse keys.each do |k| akk[key].concat values[k] end akk end end |
#connections ⇒ Object
create a hash of hashes which contains the grade of linkage between elements connections[4] #=> 2
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/clustering/simple.rb', line 20 def connections @connections ||= @original.inject(Hash.new{|h,k| h[k] = Hash.new(0)}) do |akk, e| e.each do |key| e.each do |id| akk[key][id] += 1 unless key == id end end akk end end |
#relations ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/clustering/simple.rb', line 31 def relations @relations ||= connections.inject(Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = Array.new }}) do |akk, e| e.last.each do |k,v| akk[e.first][v].push k end akk end end |
#strength?(a, b) ⇒ Boolean
51 52 53 |
# File 'lib/clustering/simple.rb', line 51 def strength?(a, b) connections[a][b] end |