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
24
25
26
# File 'lib/dbscan.rb', line 18

def initialize( points, options = {} )
  options[:distance] = :euclidean_distance if !options[:distance]
  options[:labels] = [] if !options[:labels]

  c = 0
  @points, @options, @clusters = points.map { |e| po = Point.new(e, options[:labels][c] ); c +=1; po }, 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



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 >= 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



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 >= 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



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.#{options[:distance]}( p.items )")
      neighbors.push( p ) if d < options[:epsilon]
    end
  end
  neighbors
end

#labeled_resultsObject



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

#resultsObject



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