Module: KMeansClusterer::Distance

Defined in:
lib/kmeans-clusterer.rb

Class Method Summary collapse

Class Method Details

.euclidean(x, y, yy = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kmeans-clusterer.rb', line 44

def self.euclidean x, y, yy = nil
  if x.is_a?(NMatrix) && y.is_a?(NMatrix)
    xx = Scaler.row_norms(x)
    yy ||= Scaler.row_norms(y)
    xy = x * y.transpose
    distance = xy * -2
    distance += xx
    distance += yy.transpose
    NMath.sqrt distance
  else
    NMath.sqrt ((x - y)**2).sum(0)
  end
end