Class: DBSCAN::Clusterer

Inherits:
Object
  • Object
show all
Defined in:
lib/dbscan.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {} )
  options[:distance] = :euclidean_distance if !options[:distance]
  @points, @options, @clusters = points.map { |e| Point.new(e) }, options, {-1 => []}

  clusterize!
end

Instance Attribute Details

#clustersObject

Returns the value of attribute clusters.



16
17
18
# File 'lib/dbscan.rb', line 16

def clusters
  @clusters
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/dbscan.rb', line 16

def options
  @options
end

#pointsObject

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 >= options[: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 >= options[: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 < options[:epsilon]
    end
  end
  neighbors
end

#resultsObject



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