Class: Coletivo::Similarity::EuclideanDistanceStrategy

Inherits:
BaseStrategy
  • Object
show all
Defined in:
lib/coletivo/similarity/euclidean_distance_strategy.rb

Instance Attribute Summary

Attributes inherited from BaseStrategy

#preferences

Instance Method Summary collapse

Methods inherited from BaseStrategy

#train_with

Instance Method Details

#similarity_between(one, other) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/coletivo/similarity/euclidean_distance_strategy.rb', line 4

def similarity_between(one, other)
  shared = shared_items_between(one, other)

  return 0 if shared.empty?

  sum_of_squares = shared.inject(0.0) { |sum, item|
    sum + (preferences[one][item] - preferences[other][item]) ** 2
  }

  1 / (1 + sum_of_squares)
end