knn 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +31 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/examples/example_one.rb +10 -0
- data/lib/ext/array.rb +9 -0
- data/lib/knn.rb +24 -0
- data/spec/ext/array_spec.rb +13 -0
- data/spec/knn_spec.rb +19 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +80 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 reddavis
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
= K Nearest Neighbours
|
2
|
+
|
3
|
+
Simple KNN Ruby implementation
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
gem sources -a -http://gemcutter.org
|
8
|
+
sudo gem install naive_bayes
|
9
|
+
|
10
|
+
== How To Use
|
11
|
+
|
12
|
+
require 'rubygems'
|
13
|
+
require 'knn'
|
14
|
+
|
15
|
+
data = Array.new(100000) { Array.new(4) { rand } }
|
16
|
+
|
17
|
+
knn = KNN.new(data)
|
18
|
+
|
19
|
+
knn.nearest_neighbours([1,2,3,4], 4) # ([data], k's)
|
20
|
+
#=> [[4837, 7.43033158269445, [0.966558570073977, 0.903158898673566, 0.954567901514261, 0.988114355901207]], ...
|
21
|
+
|
22
|
+
# Data is returned in the format
|
23
|
+
# [data index, distance to the input, [data points]]
|
24
|
+
|
25
|
+
# So if we called queried the data array for 4837...
|
26
|
+
data[4837]
|
27
|
+
#=> [0.966558570073977, 0.903158898673566, 0.954567901514261, 0.988114355901207]
|
28
|
+
|
29
|
+
== Copyright
|
30
|
+
|
31
|
+
Copyright (c) 2009 Red Davis. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "knn"
|
8
|
+
gem.summary = %Q{Simple K Nearest Neighbour algorithm}
|
9
|
+
gem.description = %Q{Simple K Nearest Neighbour algorithm}
|
10
|
+
gem.email = "reddavis@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/reddavis/knn"
|
12
|
+
gem.authors = ["reddavis"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "k_nearest_neighbours #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/lib/ext/array.rb
ADDED
data/lib/knn.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/ext/array')
|
2
|
+
|
3
|
+
class KNN
|
4
|
+
def initialize(data)
|
5
|
+
@data = data
|
6
|
+
end
|
7
|
+
|
8
|
+
def nearest_neighbours(input, k=4)
|
9
|
+
find_closest_data(input, k)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def find_closest_data(input, k)
|
15
|
+
calculated_distances = []
|
16
|
+
|
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
|
21
|
+
|
22
|
+
calculated_distances.sort {|x, y| x[1] <=> y[1]}.first(k)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,13 @@
|
|
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
|
data/spec/knn_spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "KNN" do
|
4
|
+
before do
|
5
|
+
@knn = KNN.new(data)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return 2 nearest neighbours (50,52 10,11)" do
|
9
|
+
neighbours = @knn.nearest_neighbours([60,60], 2)
|
10
|
+
neighbours.size.should == 2
|
11
|
+
neighbours.map {|x| x[2]}.should include([50,52], [10,11])
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def data
|
17
|
+
[[1,2], [5,6], [10,11], [50,52]]
|
18
|
+
end
|
19
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: knn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- reddavis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-07 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
description: Simple K Nearest Neighbour algorithm
|
26
|
+
email: reddavis@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- examples/example_one.rb
|
42
|
+
- lib/ext/array.rb
|
43
|
+
- lib/knn.rb
|
44
|
+
- spec/ext/array_spec.rb
|
45
|
+
- spec/knn_spec.rb
|
46
|
+
- spec/spec.opts
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/reddavis/knn
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.3.5
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Simple K Nearest Neighbour algorithm
|
76
|
+
test_files:
|
77
|
+
- spec/ext/array_spec.rb
|
78
|
+
- spec/knn_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
- examples/example_one.rb
|