Class: SClust::KMean::CosineDistance

Inherits:
Object
  • Object
show all
Defined in:
lib/sclust/kmean/cluster.rb

Class Method Summary collapse

Class Method Details

.distance(a, b) ⇒ Object

Given two vectors, compute the distance



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sclust/kmean/cluster.rb', line 34

def self.distance(a,b)
        
    acc1 = 0.0
    acc2 = 0.0
    acc3 = 0.0
    
    a.merge(b).keys.each do |i|
        acc1 += a[i]*b[i] 
        acc2 += a[i]*a[i]
        acc3 += b[i]*b[i]
    end
    
    v = 1 - ( acc1 / (Math.sqrt(acc2) * Math.sqrt(acc3)) )
    
    # Return nil if we detect no distance between documents.
    (v==1)? nil : v
end