Sha256: a8d481f3bd60e5869c3f7af12bee5f1bad4bbc827be2d3713091f8158dbd2af5

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# For more information see https://en.wikipedia.org/wiki/DBSCAN

module DbClustering
  module Algorithms
    class Dbscan

      attr_accessor :datasource, :clusters

      def initialize(datasource:, distance_metric:)
        @datasource = datasource
        @distance_metric = distance_metric
        @clusters = []
      end

      def cluster(max_distance:, min_neighbors:)
        @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
          else
            if point.cluster.nil?
              cluster = DbClustering::Models::Cluster.new
              @clusters << cluster
            else
              cluster = point.cluster
            end

            neighbors.each do |neighbor|
              if neighbor.cluster.nil?
                cluster.add(neighbor)

                # 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

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
db_clustering-0.1.11 lib/algorithms/density_based/dbscan.rb