knn 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,6 +26,32 @@ Simple KNN Ruby implementation
26
26
  data[4837]
27
27
  #=> [0.966558570073977, 0.903158898673566, 0.954567901514261, 0.988114355901207]
28
28
 
29
+ == Distance Measurements
30
+
31
+ KNN uses the Distance Measures Gem (http://github.com/reddavis/Distance-Measures) so we get quite a range of distance measurements.
32
+
33
+ The measurements currently available are:
34
+
35
+ euclidean_distance
36
+
37
+ cosine_similarity
38
+
39
+ jaccard_index
40
+
41
+ jaccard_distance
42
+
43
+ binary_jaccard_index
44
+
45
+ binary_jaccard_distance
46
+
47
+ tanimoto_coefficient
48
+
49
+ To specify a particular one to use in the KNN algorithm, just provide it as an option:
50
+
51
+ KNN.new(@data, :distance_measure => :jaccard_index)
52
+ KNN.new(@data, :distance_measure => :cosine_similarity)
53
+ KNN.new(@data, :distance_measure => :tanimoto_coefficient)
54
+
29
55
  == Copyright
30
56
 
31
57
  Copyright (c) 2009 Red Davis. See LICENSE for details.
data/Rakefile CHANGED
@@ -11,6 +11,7 @@ begin
11
11
  gem.homepage = "http://github.com/reddavis/knn"
12
12
  gem.authors = ["reddavis"]
13
13
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency('distance_measures', '>= 0.0.0')
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
16
17
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{knn}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["reddavis"]
12
+ s.date = %q{2010-01-26}
13
+ s.description = %q{Simple K Nearest Neighbour algorithm}
14
+ s.email = %q{reddavis@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "examples/example_one.rb",
27
+ "knn.gemspec",
28
+ "lib/knn.rb",
29
+ "spec/knn_spec.rb",
30
+ "spec/spec.opts",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/reddavis/knn}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{Simple K Nearest Neighbour algorithm}
38
+ s.test_files = [
39
+ "spec/knn_spec.rb",
40
+ "spec/spec_helper.rb",
41
+ "examples/example_one.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
50
+ s.add_runtime_dependency(%q<distance_measures>, [">= 0.0.0"])
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ s.add_dependency(%q<distance_measures>, [">= 0.0.0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ s.add_dependency(%q<distance_measures>, [">= 0.0.0"])
58
+ end
59
+ end
60
+
data/lib/knn.rb CHANGED
@@ -1,8 +1,9 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/ext/array')
1
+ require 'distance_measures'
2
2
 
3
3
  class KNN
4
- def initialize(data)
4
+ def initialize(data, options={})
5
5
  @data = data
6
+ @distance_measure = options[:distance_measure] || :euclidean_distance
6
7
  end
7
8
 
8
9
  def nearest_neighbours(input, k=4)
@@ -12,13 +13,17 @@ class KNN
12
13
  private
13
14
 
14
15
  def find_closest_data(input, k)
15
- calculated_distances = []
16
+ begin
17
+ calculated_distances = []
16
18
 
17
- @data.each_with_index do |datum, index| #Ye olde english
18
- distance = input.euclidean_distance_from(datum)
19
- calculated_distances << [index, distance, datum]
20
- end
19
+ @data.each_with_index do |datum, index| #Ye olde english
20
+ distance = input.send(@distance_measure, datum)
21
+ calculated_distances << [index, distance, datum]
22
+ end
21
23
 
22
- calculated_distances.sort {|x, y| x[1] <=> y[1]}.first(k)
24
+ calculated_distances.sort {|x, y| x[1] <=> y[1]}.first(k)
25
+ rescue NoMethodError
26
+ raise "Hey, that's not a measurement. Read the README for available measurements"
27
+ end
23
28
  end
24
29
  end
@@ -11,6 +11,14 @@ describe "KNN" do
11
11
  neighbours.map {|x| x[2]}.should include([50,52], [10,11])
12
12
  end
13
13
 
14
+ describe "Providing a wrong distance measure" do
15
+ it "should raise an error" do
16
+ lambda do
17
+ KNN.new(data, :distance_measure => :wrong).nearest_neighbours([2,2])
18
+ end.should raise_error
19
+ end
20
+ end
21
+
14
22
  private
15
23
 
16
24
  def data
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - reddavis
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-07 00:00:00 +00:00
12
+ date: 2010-01-26 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,6 +22,16 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 1.2.9
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: distance_measures
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.0
34
+ version:
25
35
  description: Simple K Nearest Neighbour algorithm
26
36
  email: reddavis@gmail.com
27
37
  executables: []
@@ -39,9 +49,8 @@ files:
39
49
  - Rakefile
40
50
  - VERSION
41
51
  - examples/example_one.rb
42
- - lib/ext/array.rb
52
+ - knn.gemspec
43
53
  - lib/knn.rb
44
- - spec/ext/array_spec.rb
45
54
  - spec/knn_spec.rb
46
55
  - spec/spec.opts
47
56
  - spec/spec_helper.rb
@@ -74,7 +83,6 @@ signing_key:
74
83
  specification_version: 3
75
84
  summary: Simple K Nearest Neighbour algorithm
76
85
  test_files:
77
- - spec/ext/array_spec.rb
78
86
  - spec/knn_spec.rb
79
87
  - spec/spec_helper.rb
80
88
  - examples/example_one.rb
@@ -1,9 +0,0 @@
1
- class Array
2
- def euclidean_distance_from(other)
3
- sum = 0.0
4
- self.each_with_index do |datum, index|
5
- sum += (datum - other[index]) ** 2
6
- end
7
- Math.sqrt(sum)
8
- end
9
- end
@@ -1,13 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Array" do
4
- describe "Euclidean Distance" do
5
- it "should return 0" do
6
- [0,0].euclidean_distance_from([0,0]).should == 0
7
- end
8
-
9
- it "should return 1" do
10
- [0,0].euclidean_distance_from([0,1]).should == 1
11
- end
12
- end
13
- end