Class: DbClustering::Algorithms::Dbscan
- Inherits:
-
Object
- Object
- DbClustering::Algorithms::Dbscan
- Defined in:
- lib/algorithms/density_based/dbscan.rb
Instance Attribute Summary collapse
-
#clusters ⇒ Object
Returns the value of attribute clusters.
-
#datasource ⇒ Object
Returns the value of attribute datasource.
Instance Method Summary collapse
- #cluster(max_distance:, min_neighbors:, debug: false) ⇒ Object
-
#initialize(datasource:, distance_metric:) ⇒ Dbscan
constructor
A new instance of Dbscan.
Constructor Details
#initialize(datasource:, distance_metric:) ⇒ Dbscan
Returns a new instance of Dbscan.
9 10 11 12 13 |
# File 'lib/algorithms/density_based/dbscan.rb', line 9 def initialize(datasource:, distance_metric:) @datasource = datasource @distance_metric = distance_metric @clusters = [] end |
Instance Attribute Details
#clusters ⇒ Object
Returns the value of attribute clusters.
7 8 9 |
# File 'lib/algorithms/density_based/dbscan.rb', line 7 def clusters @clusters end |
#datasource ⇒ Object
Returns the value of attribute datasource.
7 8 9 |
# File 'lib/algorithms/density_based/dbscan.rb', line 7 def datasource @datasource end |
Instance Method Details
#cluster(max_distance:, min_neighbors:, debug: false) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/algorithms/density_based/dbscan.rb', line 15 def cluster(max_distance:, min_neighbors:, debug: false) @clusters = [] cluster = nil @datasource.iterate_all_points do |point, current_index, points_count| neighbors = @datasource.neighbors(point: point, distance_metric: @distance_metric, max_distance: max_distance) if neighbors.count < min_neighbors point.is_noise = true elsif point.cluster.nil? cluster = DbClustering::Models::Cluster.new @clusters << cluster neighbors.each do |neighbor| if neighbor.cluster.nil? cluster.add(neighbor) if debug print "+" end # add the neighbors of the neighbor to the neighbors to fully expand the cluster neighbors |= @datasource.neighbors(point: neighbor, distance_metric: @distance_metric, max_distance: max_distance) end end end yield(point, current_index, points_count) if block_given? end end |