Class: SameSame::RockAlgorithm
- Inherits:
-
Object
- Object
- SameSame::RockAlgorithm
- Defined in:
- lib/same_same/rock_algorithm.rb
Instance Attribute Summary collapse
-
#datapoints ⇒ Object
Returns the value of attribute datapoints.
-
#k ⇒ Object
Returns the value of attribute k.
-
#link_matrix ⇒ Object
Returns the value of attribute link_matrix.
-
#similarity_matrix ⇒ Object
Returns the value of attribute similarity_matrix.
-
#similarity_measure ⇒ Object
Returns the value of attribute similarity_measure.
-
#th ⇒ Object
Returns the value of attribute th.
Instance Method Summary collapse
- #cluster ⇒ Object
-
#initialize(attrs = {}) ⇒ RockAlgorithm
constructor
A new instance of RockAlgorithm.
- #one_point_per_cluster ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ RockAlgorithm
Returns a new instance of RockAlgorithm.
11 12 13 14 15 16 17 18 19 |
# File 'lib/same_same/rock_algorithm.rb', line 11 def initialize( attrs = {} ) self.datapoints = attrs.fetch( :datapoints ) self.similarity_measure = attrs[ :similarity_measure ] || JaquardCoefficient.new self.k = attrs.fetch(:k) self.th = attrs.fetch(:th) self.similarity_matrix = SimilarityMatrix.new( similarity_measure, datapoints.map {|d| d.data} ) self.link_matrix = LinkMatrix.new( datapoints: datapoints, similarity_matrix: similarity_matrix, th: th) end |
Instance Attribute Details
#datapoints ⇒ Object
Returns the value of attribute datapoints.
9 10 11 |
# File 'lib/same_same/rock_algorithm.rb', line 9 def datapoints @datapoints end |
#k ⇒ Object
Returns the value of attribute k.
9 10 11 |
# File 'lib/same_same/rock_algorithm.rb', line 9 def k @k end |
#link_matrix ⇒ Object
Returns the value of attribute link_matrix.
9 10 11 |
# File 'lib/same_same/rock_algorithm.rb', line 9 def link_matrix @link_matrix end |
#similarity_matrix ⇒ Object
Returns the value of attribute similarity_matrix.
9 10 11 |
# File 'lib/same_same/rock_algorithm.rb', line 9 def similarity_matrix @similarity_matrix end |
#similarity_measure ⇒ Object
Returns the value of attribute similarity_measure.
9 10 11 |
# File 'lib/same_same/rock_algorithm.rb', line 9 def similarity_measure @similarity_measure end |
#th ⇒ Object
Returns the value of attribute th.
9 10 11 |
# File 'lib/same_same/rock_algorithm.rb', line 9 def th @th end |
Instance Method Details
#cluster ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/same_same/rock_algorithm.rb', line 21 def cluster Dendrogram.new( "Goodness" ).tap do |dnd| initial_clusters = one_point_per_cluster g = Float::INFINITY dnd.add_level(g.to_s, initial_clusters) goodness = MergeGoodnessMeasure.new( th ) rock_clusters = RockClusters.new( link_matrix: link_matrix, clusters: initial_clusters, goodness_measure: goodness) number_of_clusters = rock_clusters.size while number_of_clusters > k do number_of_clusters_before_merge = number_of_clusters g = rock_clusters.merge_best_candidates number_of_clusters = rock_clusters.size # finish if there are no linked clusters to merge break if number_of_clusters == number_of_clusters_before_merge dnd.add_level(g.to_s, rock_clusters.clusters) end end end |