similar 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/similar/version.rb +1 -1
- data/lib/similar.rb +3 -12
- metadata +1 -1
data/lib/similar/version.rb
CHANGED
data/lib/similar.rb
CHANGED
@@ -43,22 +43,13 @@ module Similar
|
|
43
43
|
num / den.to_f
|
44
44
|
end
|
45
45
|
|
46
|
+
# Calculate the distance between two Arrays of values.
|
47
|
+
#
|
48
|
+
# Distance is inverted, so higher values are better.
|
46
49
|
def self.euclidean_distance(a, b)
|
47
|
-
|
48
|
-
# Add up the squares of all the differences
|
49
|
-
# sum_of_squares = sum(
|
50
|
-
# [pow(prefs[person1][item]-prefs[person2][item],2)
|
51
|
-
|
52
|
-
# for item in prefs[person1] if item in prefs[person2]])
|
53
|
-
|
54
|
-
# return 1/(1+sum_of_squares)
|
55
|
-
|
56
50
|
# sum, of the squares of the differences...
|
57
51
|
sum = a.zip(b).map { |x, y| ( (x - y) ** 2) }.inject(:+)
|
58
|
-
|
59
52
|
1 / (1+ sum)
|
60
|
-
# sum_1_sq = a.inject(0) { |sum, c| sum + c ** 2 }
|
61
|
-
# sum_2_sq = b.inject(0) { |sum, c| sum + c ** 2 }
|
62
53
|
end
|
63
54
|
|
64
55
|
end
|