caymans 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kmeans.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alejandro Ciniglio
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Kmeans
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'kmeans'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install kmeans
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/kmeans/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Alejandro Ciniglio"]
6
+ gem.email = ["mail@alejandrociniglio.com"]
7
+ gem.description = %q{A Kmeans classifier written in ruby, good for fast classification}
8
+ gem.summary = %q{A Kmeans classifier written in ruby}
9
+ gem.homepage = "http://github.com/ciniglio/kmeans"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "caymans"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Kmeans::VERSION
17
+ end
@@ -0,0 +1,8 @@
1
+ require "kmeans/version"
2
+ require "kmeans/initial"
3
+ require "kmeans/util"
4
+ require "kmeans/steps"
5
+ require "kmeans/default_classifier"
6
+ module Kmeans
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,9 @@
1
+ module Kmeans
2
+ class DefaultClassifier < Classifier
3
+ include RandomInitializer
4
+ end
5
+
6
+ class ForgyClassifier < Classifier
7
+ include ForgyInitializer
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module Kmeans
2
+ class LengthMismatch < ArgumentError
3
+ end
4
+ end
@@ -0,0 +1,28 @@
1
+ module Kmeans
2
+ class Classifier
3
+ def initialize(k, data)
4
+ @k = k
5
+ @data = data
6
+ @clusters = new_clusters(@k)
7
+ end
8
+ end
9
+
10
+ module ForgyInitializer
11
+ def initialize(*args)
12
+ super *args
13
+ @means = random_selection(@k)
14
+ assignment_step
15
+ end
16
+ end
17
+
18
+ module RandomInitializer
19
+ def initialize(*args)
20
+ super *args
21
+ r = Random.new
22
+ @data.each do |v|
23
+ @clusters[r.rand(@k)] << v
24
+ end
25
+ update_step
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ module Kmeans
2
+ class Classifier
3
+ def assignment_step
4
+ clusters = new_clusters(@k)
5
+ @data.each do |d|
6
+ min_dist = Float::INFINITY
7
+ nearest = nil
8
+ @means.each_with_index do |m, i|
9
+ dist = distance(d, m)
10
+ if dist < min_dist
11
+ min_dist = dist
12
+ nearest = i
13
+ end
14
+ end
15
+ clusters[nearest] << d
16
+ end
17
+ @clusters = clusters
18
+ end
19
+
20
+ def update_step
21
+ means = Array.new(@k)
22
+ @clusters.each_with_index do |c, i|
23
+ means[i] = centroid(c)
24
+ end
25
+ @means = means
26
+ assignment_step
27
+ end
28
+
29
+ def run(n = 100)
30
+ n.times { update_step }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'set'
2
+
3
+ module Kmeans
4
+ class Classifier
5
+ def random_selection(n = 1)
6
+ @data.to_a.sample n
7
+ end
8
+
9
+ def distance(u, v)
10
+ raise LengthMismatch, "||#{u}|| != ||#{v}||" unless (u.length == v.length)
11
+ sum = 0.to_f
12
+ u.each_with_index do |p, i|
13
+ q = v[i]
14
+ sum += (p - q)**(2.to_f)
15
+ end
16
+ Math.sqrt(sum)
17
+ end
18
+
19
+ def new_clusters(n)
20
+ clusters = []
21
+ n.times { clusters << Set.new }
22
+ clusters
23
+ end
24
+
25
+ def centroid(points)
26
+ x = Array.new(points.first.length, 0)
27
+ points.each do |p|
28
+ x = x.zip(p).map { |xi| xi.inject(:+) }
29
+ end
30
+ x.map { |xi| xi / points.length.to_f }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Kmeans
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: caymans
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alejandro Ciniglio
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-13 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A Kmeans classifier written in ruby, good for fast classification
15
+ email:
16
+ - mail@alejandrociniglio.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - kmeans.gemspec
27
+ - lib/kmeans.rb
28
+ - lib/kmeans/default_classifier.rb
29
+ - lib/kmeans/errors.rb
30
+ - lib/kmeans/initial.rb
31
+ - lib/kmeans/steps.rb
32
+ - lib/kmeans/util.rb
33
+ - lib/kmeans/version.rb
34
+ homepage: http://github.com/ciniglio/kmeans
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.24
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: A Kmeans classifier written in ruby
58
+ test_files: []