similar 0.0.1 → 0.0.2
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 +18 -0
- data/similar.gemspec +1 -1
- data/spec/similar_spec.rb +9 -0
- metadata +2 -2
data/lib/similar/version.rb
CHANGED
data/lib/similar.rb
CHANGED
@@ -43,4 +43,22 @@ module Similar
|
|
43
43
|
num / den.to_f
|
44
44
|
end
|
45
45
|
|
46
|
+
def self.euclidian_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
|
+
# sum, of the squares of the differences...
|
57
|
+
sum = a.zip(b).map { |x, y| ( (x - y) ** 2) }.inject(:+)
|
58
|
+
|
59
|
+
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
|
+
end
|
63
|
+
|
46
64
|
end
|
data/similar.gemspec
CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
|
|
13
13
|
if arbitrary sets of data.
|
14
14
|
EOF
|
15
15
|
gem.summary = %q{Determine similarity of arbitrary sets of data}
|
16
|
-
gem.homepage = "http://
|
16
|
+
gem.homepage = "http://github.com/scottbarr/similar"
|
17
17
|
|
18
18
|
gem.files = `git ls-files`.split($/)
|
19
19
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/spec/similar_spec.rb
CHANGED
@@ -23,4 +23,13 @@ describe Similar do
|
|
23
23
|
lambda { Similar.pearson_score(a, b) }.should raise_exception(ArgumentError, "Arrays not of equal length")
|
24
24
|
end
|
25
25
|
|
26
|
+
it "should calculate euclidian distance score" do
|
27
|
+
a = 5.0, 5.0, 5.0, 5.0, 5.0
|
28
|
+
b = 4.0, 4.0, 4.0, 4.0, 4.0
|
29
|
+
|
30
|
+
score = Similar.euclidian_distance(a, b)
|
31
|
+
|
32
|
+
score.should be_within(0.00001).of(0.16666666666666666)
|
33
|
+
end
|
34
|
+
|
26
35
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: similar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -159,7 +159,7 @@ files:
|
|
159
159
|
- similar.gemspec
|
160
160
|
- spec/similar_spec.rb
|
161
161
|
- spec/spec_helper.rb
|
162
|
-
homepage: http://
|
162
|
+
homepage: http://github.com/scottbarr/similar
|
163
163
|
licenses: []
|
164
164
|
post_install_message:
|
165
165
|
rdoc_options: []
|