Class: DBSCAN::Clusterer
- Inherits:
-
Object
- Object
- DBSCAN::Clusterer
- Defined in:
- lib/dbscan.rb
Instance Attribute Summary collapse
-
#clusters ⇒ Object
Returns the value of attribute clusters.
-
#options ⇒ Object
Returns the value of attribute options.
-
#points ⇒ Object
Returns the value of attribute points.
Instance Method Summary collapse
- #add_connected(neighbors, current_cluster) ⇒ Object
- #clusterize! ⇒ Object
-
#initialize(points, options = {}) ⇒ Clusterer
constructor
A new instance of Clusterer.
- #inmediate_neighbors(point) ⇒ Object
- #results ⇒ Object
Constructor Details
#initialize(points, options = {}) ⇒ Clusterer
Returns a new instance of Clusterer.
18 19 20 21 22 23 |
# File 'lib/dbscan.rb', line 18 def initialize( points, = {} ) [:distance] = :euclidean_distance if ![:distance] @points, , @clusters = points.map { |e| Point.new(e) }, , {-1 => []} clusterize! end |
Instance Attribute Details
#clusters ⇒ Object
Returns the value of attribute clusters.
16 17 18 |
# File 'lib/dbscan.rb', line 16 def clusters @clusters end |
#options ⇒ Object
Returns the value of attribute options.
16 17 18 |
# File 'lib/dbscan.rb', line 16 def end |
#points ⇒ Object
Returns the value of attribute points.
16 17 18 |
# File 'lib/dbscan.rb', line 16 def points @points end |
Instance Method Details
#add_connected(neighbors, current_cluster) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dbscan.rb', line 61 def add_connected( neighbors, current_cluster ) cluster_points = [] neighbors.each do |point| if !point.visited? point.visit! new_points = inmediate_neighbors(point) if new_points.size >= [:min_points] new_points.each do |p| if !neighbors.include?(p) neighbors.push( p ) end end end end if !point.cluster cluster_points.push( point ) point.cluster = current_cluster end end cluster_points end |
#clusterize! ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/dbscan.rb', line 25 def clusterize! current_cluster = -1 @points.each do |point| if !point.visited? point.visit! neighbors = inmediate_neighbors( point ) if neighbors.size >= [:min_points] current_cluster += 1 point.cluster = current_cluster cluster = [point].push( add_connected( neighbors, current_cluster )) clusters[current_cluster] = cluster.flatten else clusters[-1].push( point ) end end end end |
#inmediate_neighbors(point) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dbscan.rb', line 50 def inmediate_neighbors( point ) neighbors = [] @points.each do |p| if p.items != point.items d = eval("point.items.#{options[:distance]}( p.items )") neighbors.push( p ) if d < [:epsilon] end end neighbors end |
#results ⇒ Object
44 45 46 47 48 |
# File 'lib/dbscan.rb', line 44 def results hash = {} @clusters.dup.each { |cluster_index, value| hash[cluster_index] = value.flatten.map(&:items) if !value.flatten.empty? } hash end |