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
- #labeled_results ⇒ Object
- #results ⇒ Object
Constructor Details
#initialize(points, options = {}) ⇒ Clusterer
Returns a new instance of Clusterer.
18 19 20 21 22 23 24 25 26 |
# File 'lib/dbscan.rb', line 18 def initialize( points, = {} ) [:distance] = :euclidean_distance if ![:distance] [:labels] = [] if ![:labels] c = 0 @points, @options, @clusters = points.map { |e| po = Point.new(e, [:labels][c] ); c +=1; po }, , {-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 @options 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dbscan.rb', line 75 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
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dbscan.rb', line 28 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
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dbscan.rb', line 64 def inmediate_neighbors( point ) neighbors = [] @points.each do |p| if p.items != point.items d = eval("point.items.#{[:distance]}( p.items )") neighbors.push( p ) if d < [:epsilon] end end neighbors end |
#labeled_results ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dbscan.rb', line 53 def labeled_results hash = {} @clusters.each do |cluster_index, elements| hash.store( cluster_index, [] ) elements.each do |e| hash[cluster_index].push( e.label ) end end hash end |
#results ⇒ Object
47 48 49 50 51 |
# File 'lib/dbscan.rb', line 47 def results hash = {} @clusters.dup.each { |cluster_index, value| hash[cluster_index] = value.flatten.map(&:items) if !value.flatten.empty? } hash end |