kmeans 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -15,54 +15,7 @@ See also.
15
15
  Tutorial
16
16
  --------
17
17
 
18
- require 'kmeans/pair'
19
- require 'kmeans/pearson'
20
- require 'kmeans/cluster'
21
- data = {
22
- "test01"=>
23
- {"hoge"=>0,
24
- "fuga"=>1,
25
- "piyo"=>0
26
- },
27
- "test02"=>
28
- {"hoge"=>2,
29
- "fuga"=>1,
30
- "piyo"=>3
31
- },
32
- "test03"=>
33
- {"hoge"=>3,
34
- "fuga"=>0,
35
- "piyo"=>1
36
- },
37
- "test04"=>
38
- {"hoge"=>0,
39
- "fuga"=>2,
40
- "piyo"=>0
41
- },
42
- "test05"=>
43
- {"hoge"=>4,
44
- "fuga"=>2,
45
- "piyo"=>3
46
- },
47
- "test06"=>
48
- {"hoge"=>3,
49
- "fuga"=>1,
50
- "piyo"=>1
51
- },
52
- } # 2-dimensional hash
53
- cluster = Kmeans::Cluster.new(data, {
54
- :centroids => 4,
55
- :loop_max => 10
56
- }) # Options can be omitted.
57
- cluster.make_cluster
58
- puts cluster.cluster # => {3=>["test01", "test04"], 1=>["test02"], 2=>["test03", "test05"], 0=>["test06"]}
59
- # Result changes for each execution
60
-
61
-
62
- Environment
63
- -----------
64
-
65
- After ruby 1.8.
18
+ See examples.
66
19
 
67
20
 
68
21
  Development
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/doc/ChangeLog CHANGED
@@ -1,3 +1,14 @@
1
+ === 0.0.3 / 2012-10-11
2
+
3
+ * Add hcluster.
4
+
5
+
6
+ === 0.0.2 / 2012-10-09
7
+
8
+ * Add tutorial.
9
+ * Fix test cases.
10
+
11
+
1
12
  === 0.0.1 / 2012-10-08
2
13
 
3
14
  * First release.
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'kmeans/pearson'
5
+ require 'kmeans/hcluster'
6
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0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 1, 2, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 11, 0, 0, 0, 0, 1, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 0, 0, 0, 3, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 4, 0, 0, 0, 0, 0, 0]]
9
+
10
+ cs = Kmeans::HCluster.new
11
+ clust = cs.hcluster(data)
12
+ print cs.printclust(clust, blognames)
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'kmeans/pair'
5
+ require 'kmeans/pearson'
6
+ require 'kmeans/cluster'
7
+
8
+ testdata = {
9
+ "test01"=>
10
+ {"hoge"=>0,
11
+ "fuga"=>1,
12
+ "piyo"=>0
13
+ },
14
+ "test02"=>
15
+ {"hoge"=>2,
16
+ "fuga"=>1,
17
+ "piyo"=>3
18
+ },
19
+ "test03"=>
20
+ {"hoge"=>3,
21
+ "fuga"=>0,
22
+ "piyo"=>1
23
+ },
24
+ "test04"=>
25
+ {"hoge"=>0,
26
+ "fuga"=>2,
27
+ "piyo"=>0
28
+ },
29
+ "test05"=>
30
+ {"hoge"=>4,
31
+ "fuga"=>2,
32
+ "piyo"=>3
33
+ },
34
+ "test06"=>
35
+ {"hoge"=>3,
36
+ "fuga"=>1,
37
+ "piyo"=>1
38
+ },
39
+ }
40
+
41
+ cluster = Kmeans::Cluster.new(testdata, { :centroids => 3, :loop_max => 10 })
42
+ cluster.make_cluster
43
+ p cluster.cluster
data/kmeans.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "kmeans"
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["id774"]
12
- s.date = "2012-10-08"
12
+ s.date = "2012-10-11"
13
13
  s.description = "K-means clustering"
14
14
  s.email = "idnanashi@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -26,13 +26,17 @@ Gem::Specification.new do |s|
26
26
  "doc/ChangeLog",
27
27
  "doc/LICENSE",
28
28
  "doc/README",
29
+ "examples/hcluster.rb",
30
+ "examples/kmeans.rb",
29
31
  "kmeans.gemspec",
30
32
  "lib/kmeans.rb",
31
33
  "lib/kmeans/cluster.rb",
34
+ "lib/kmeans/hcluster.rb",
32
35
  "lib/kmeans/pair.rb",
33
36
  "lib/kmeans/pearson.rb",
34
37
  "script/build",
35
38
  "spec/lib/kmeans/cluster_spec.rb",
39
+ "spec/lib/kmeans/hcluster_spec.rb",
36
40
  "spec/lib/kmeans/pair_spec.rb",
37
41
  "spec/lib/kmeans/pearson_spec.rb",
38
42
  "spec/lib/kmeans_spec.rb",
@@ -0,0 +1,82 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Kmeans
4
+ class Bicluster
5
+ def initialize(vec, left=nil, right=nil, distance=0.0, id=nil)
6
+ @left = left
7
+ @right = right
8
+ @vec = vec
9
+ @id = id
10
+ @distance = distance
11
+ end
12
+
13
+ attr_accessor :left, :right, :vec, :id, :distance
14
+ end
15
+
16
+ class HCluster
17
+ def initialize
18
+ @out = ""
19
+ end
20
+
21
+ def printclust(clust, labels=nil, n=0)
22
+ n.times do
23
+ @out << " "
24
+ end
25
+ if clust.id < 0
26
+ @out << "-\n"
27
+ else
28
+ if labels == nil
29
+ @out << clust.id + "\n"
30
+ else
31
+ @out << labels[clust.id] + "\n"
32
+ end
33
+ end
34
+ printclust(clust.left, labels, n+1) if clust.left != nil
35
+ printclust(clust.right, labels, n+1) if clust.right != nil
36
+ return @out
37
+ end
38
+
39
+ def hcluster(rows)
40
+ distances = Hash.new
41
+ currentclustid = -1
42
+
43
+ clust = Array.new
44
+ for i in 0...rows.length
45
+ c = Bicluster.new(rows[i])
46
+ c.id = i
47
+ clust.push(c)
48
+ end
49
+
50
+ while clust.length > 1
51
+ lowestpair = [0,1]
52
+ closest = Pearson.calc(clust[0].vec, clust[1].vec)
53
+ for i in 0...clust.length
54
+ for j in i+1...clust.length
55
+ if !distances.key?([clust[i].id, clust[j].id])
56
+ distances[[clust[i].id, clust[j].id]] = Pearson.calc(clust[i].vec, clust[j].vec)
57
+ end
58
+ d = distances[[clust[i].id, clust[j].id]]
59
+ if d < closest
60
+ closest = d
61
+ lowestpair = [i,j]
62
+ end
63
+ end
64
+ end
65
+
66
+ mergevec = Array.new
67
+ for i in 0...clust[0].vec.length
68
+ m = (clust[lowestpair[0]].vec[i] + clust[lowestpair[1]].vec[i])/2.0
69
+ mergevec.push(m)
70
+ end
71
+
72
+ newcluster = Bicluster.new(mergevec, clust[lowestpair[0]], clust[lowestpair[1]], closest, currentclustid)
73
+
74
+ currentclustid -= 1
75
+ clust.delete_at(lowestpair[1])
76
+ clust.delete_at(lowestpair[0])
77
+ clust.push(newcluster)
78
+ end
79
+ return clust[0]
80
+ end
81
+ end
82
+ end
data/lib/kmeans.rb CHANGED
@@ -2,13 +2,9 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  module Kmeans
5
- VERSION = "0.0.2"
6
- ROOT_DIR = File.expand_path("..", File.dirname(__FILE__))
7
- $:.unshift ROOT_DIR
8
- $:.unshift ROOT_DIR + '/lib'
9
- $:.unshift ROOT_DIR + '/lib/kmeans'
10
-
11
- require 'pair'
12
- require 'pearson'
13
- require 'cluster'
5
+ VERSION = "0.0.3"
6
+ require File.dirname(__FILE__) + "/kmeans/pair"
7
+ require File.dirname(__FILE__) + "/kmeans/pearson"
8
+ require File.dirname(__FILE__) + "/kmeans/cluster"
9
+ require File.dirname(__FILE__) + "/kmeans/hcluster"
14
10
  end
@@ -0,0 +1,37 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) +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0, 0, 0, 1, 0, 0, 0, 1, 0, 4, 0, 4, 2, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 1, 2, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 11, 0, 0, 0, 0, 1, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 0, 0, 0, 3, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 4, 0, 0, 0, 0, 0, 0]]
9
+ end
10
+
11
+ context 'の HCluster クラスにおいて' do
12
+ describe '二次元配列を渡すと' do
13
+ it "Kmeans::Bicluster クラスが返却される" do
14
+ cs = Kmeans::HCluster.new
15
+ clust = cs.hcluster(@data)
16
+ clust.class.should == Kmeans::Bicluster
17
+ end
18
+ end
19
+ end
20
+
21
+ context 'の HCluster クラスにおいて' do
22
+ describe 'printclust メソッドを呼ぶと' do
23
+ it "クラスタの中身が返却される" do
24
+ cs = Kmeans::HCluster.new
25
+ clust = cs.hcluster(@data)
26
+ out = cs.printclust(clust, @blognames)
27
+ out.should ==
28
+ "-
29
+ Wonkette
30
+ -
31
+ The Superficial - Because You're Ugly
32
+ Publishing 2.0
33
+ "
34
+ end
35
+ end
36
+ end
37
+ end
@@ -5,7 +5,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
5
5
  describe Kmeans do
6
6
  context 'のバージョンを参照した場合' do
7
7
  it "バージョンが正しく表示される" do
8
- expect = '0.0.2'
8
+ expect = '0.0.3'
9
9
  Kmeans.const_get(:VERSION).should be_true
10
10
  Kmeans.const_get(:VERSION).should == expect
11
11
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kmeans
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-08 00:00:00.000000000 Z
12
+ date: 2012-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cucumber
@@ -76,13 +76,17 @@ files:
76
76
  - doc/ChangeLog
77
77
  - doc/LICENSE
78
78
  - doc/README
79
+ - examples/hcluster.rb
80
+ - examples/kmeans.rb
79
81
  - kmeans.gemspec
80
82
  - lib/kmeans.rb
81
83
  - lib/kmeans/cluster.rb
84
+ - lib/kmeans/hcluster.rb
82
85
  - lib/kmeans/pair.rb
83
86
  - lib/kmeans/pearson.rb
84
87
  - script/build
85
88
  - spec/lib/kmeans/cluster_spec.rb
89
+ - spec/lib/kmeans/hcluster_spec.rb
86
90
  - spec/lib/kmeans/pair_spec.rb
87
91
  - spec/lib/kmeans/pearson_spec.rb
88
92
  - spec/lib/kmeans_spec.rb