Sha256: 7aab9f2c793437046363c9a8408cd7b477e1e0887f47059deed97b0c06826a4c

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 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|
          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
            expand_cluster(cluster: cluster, neighbors: neighbors)
          end

        end
      end

      def expand_cluster(cluster:, neighbors:)
        # Important: If neighbors do not include point itself, then point must be added to cluster, too.
        neighbors.each do |neighbor|
          if !neighbor.visited?
            cluster.add(neighbor)
          end
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
db_clustering-0.1.3 lib/algorithms/density_based/dbscan.rb
db_clustering-0.1.2 lib/algorithms/density_based/dbscan.rb
db_clustering-0.1.1 lib/algorithms/density_based/dbscan.rb