kmeans 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,10 +4,59 @@ Kmeans Cluster
4
4
  **K-means clustering**
5
5
 
6
6
 
7
- What is This?
8
- -------------
9
-
10
- http://en.wikipedia.org/wiki/K-means_clustering
7
+ What is Kmeans?
8
+ ---------------
9
+
10
+ See also.
11
+
12
+ + http://en.wikipedia.org/wiki/K-means_clustering
13
+
14
+
15
+ Tutorial
16
+ --------
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
11
60
 
12
61
 
13
62
  Environment
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "kmeans"
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
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-07"
12
+ s.date = "2012-10-08"
13
13
  s.description = "K-means clustering"
14
14
  s.email = "idnanashi@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
  "lib/kmeans/cluster.rb",
32
32
  "lib/kmeans/pair.rb",
33
33
  "lib/kmeans/pearson.rb",
34
- "script/.gitkeep",
34
+ "script/build",
35
35
  "spec/lib/kmeans/cluster_spec.rb",
36
36
  "spec/lib/kmeans/pair_spec.rb",
37
37
  "spec/lib/kmeans/pearson_spec.rb",
@@ -2,11 +2,13 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  module Kmeans
5
- VERSION = "0.0.1"
5
+ VERSION = "0.0.2"
6
6
  ROOT_DIR = File.expand_path("..", File.dirname(__FILE__))
7
7
  $:.unshift ROOT_DIR
8
8
  $:.unshift ROOT_DIR + '/lib'
9
9
  $:.unshift ROOT_DIR + '/lib/kmeans'
10
10
 
11
+ require 'pair'
12
+ require 'pearson'
11
13
  require 'cluster'
12
14
  end
@@ -1,8 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require 'pair'
4
- require 'pearson'
5
-
6
3
  module Kmeans
7
4
  class Cluster
8
5
  def initialize(word_counts, user_options = {})
@@ -0,0 +1,27 @@
1
+ #!/bin/sh
2
+ #
3
+ ########################################################################
4
+ # Integration Build Script
5
+ #
6
+ # Maintainer: id774 <idnanashi@gmail.com>
7
+ #
8
+ # v1.0 3/16,2012
9
+ # First.
10
+ ########################################################################
11
+
12
+ kickstart() {
13
+ export RACK_ROOT="."
14
+ export RACK_ENV="test"
15
+ }
16
+
17
+ run_tests() {
18
+ rake spec
19
+ }
20
+
21
+ main() {
22
+ kickstart
23
+ run_tests
24
+ }
25
+
26
+ set -ex
27
+ main
@@ -22,7 +22,9 @@ describe Kmeans::Pair do
22
22
  result[0].should == expect[0]
23
23
  result[1].should == expect[1]
24
24
  end
25
+ end
25
26
 
27
+ describe '最小値と最大値と余分な値を渡す場合' do
26
28
  it "ペアが返却される" do
27
29
  min = 2
28
30
  max = 4
@@ -32,7 +34,9 @@ describe Kmeans::Pair do
32
34
  result[0].should == expect[0]
33
35
  result[1].should == expect[1]
34
36
  end
37
+ end
35
38
 
39
+ describe '同一の値を渡す場合' do
36
40
  it "ペアが返却される" do
37
41
  min = 5
38
42
  max = 5
@@ -4,8 +4,8 @@ require File.dirname(__FILE__) + '/../../spec_helper'
4
4
 
5
5
  describe Kmeans::Pearson do
6
6
  context 'の Pearson クラスにおいて' do
7
- describe '二つの変数を渡す場合' do
8
- it "ピアソン相関関数が返却される" do
7
+ describe '相関係数 0.0 となる集合を渡す場合' do
8
+ it "ピアソン相関係数が返却される" do
9
9
  expect = 0.0
10
10
  v1 = [4,2,3]
11
11
  v2 = [0,0,1]
@@ -13,87 +13,113 @@ describe Kmeans::Pearson do
13
13
  result.should == expect
14
14
  end
15
15
 
16
- it "ピアソン相関関数が返却される" do
17
- expect = 0.31622776601683794
18
- v1 = [3, 0, 1]
19
- v2 = [(2/1), (1/1), (3/1)]
16
+ it "ピアソン相関係数が返却される" do
17
+ expect = 0.0
18
+ v1 = [0, 2, 0]
19
+ v2 = [2, 1, 0]
20
20
  result = Kmeans::Pearson.calc(v1,v2)
21
21
  result.should == expect
22
22
  end
23
23
 
24
- it "ピアソン相関関数が返却される" do
25
- expect = 0.4472135954999579
26
- v1 = [3, 0, 1]
24
+ it "ピアソン相関係数が返却される" do
25
+ expect = 0.0
26
+ v1 = [0, 2, 0]
27
+ v2 = [(3/2), (1/2), (1/2)]
28
+ result = Kmeans::Pearson.calc(v1,v2)
29
+ result.should == expect
30
+ end
31
+
32
+ it "ピアソン相関係数が返却される" do
33
+ expect = 0.0
34
+ v1 = [4, 2, 3]
27
35
  v2 = [(3/2), (3/2), (1/2)]
28
36
  result = Kmeans::Pearson.calc(v1,v2)
29
37
  result.should == expect
30
38
  end
31
39
 
32
- it "ピアソン相関関数が返却される" do
33
- expect = 0.8944271909999159
34
- v1 = [3, 0, 1]
35
- v2 = [(3/2), (1/2), (1/2)]
40
+ it "ピアソン相関係数が返却される" do
41
+ expect = 0.0
42
+ v1 = [2, 1, 3]
43
+ v2 = [(10/3), (1/1), (5/3)]
36
44
  result = Kmeans::Pearson.calc(v1,v2)
37
45
  result.should == expect
38
46
  end
39
47
 
40
- it "ピアソン相関関数が返却される" do
41
- expect = -0.6666666666666666
42
- v1 = [0, 2, 0]
43
- v2 = [(3/1), (3/2), (3/1)]
48
+ it "ピアソン相関係数が返却される" do
49
+ expect = 0.0
50
+ v1 = [0, 1, 0]
51
+ v2 = [2, 1, 0]
52
+ result = Kmeans::Pearson.calc(v1,v2)
53
+ result.should == expect
54
+ end
55
+
56
+ it "ピアソン相関係数が返却される" do
57
+ expect = 0.0
58
+ v1 = [0, 1, 0]
59
+ v2 = [(10/3), (1/1), (5/3)]
44
60
  result = Kmeans::Pearson.calc(v1,v2)
45
61
  result.should == expect
46
62
  end
47
63
 
48
- it "ピアソン相関関数が返却される" do
64
+ it "ピアソン相関係数が返却される" do
49
65
  expect = 0.0
50
66
  v1 = [0, 2, 0]
51
67
  v2 = [2, 1, 0]
52
68
  result = Kmeans::Pearson.calc(v1,v2)
53
69
  result.should == expect
54
70
  end
71
+ end
55
72
 
56
- it "ピアソン相関関数が返却される" do
57
- expect = 0.5773502691896258
58
- v1 = [0, 2, 0]
73
+ describe '相関係数が正となる集合を渡す場合' do
74
+ it "ピアソン相関係数が返却される" do
75
+ expect = 0.31622776601683794
76
+ v1 = [3, 0, 1]
77
+ v2 = [(2/1), (1/1), (3/1)]
78
+ result = Kmeans::Pearson.calc(v1,v2)
79
+ result.should == expect
80
+ end
81
+
82
+ it "ピアソン相関係数が返却される" do
83
+ expect = 0.4472135954999579
84
+ v1 = [3, 0, 1]
59
85
  v2 = [(3/2), (3/2), (1/2)]
60
86
  result = Kmeans::Pearson.calc(v1,v2)
61
87
  result.should == expect
62
88
  end
63
89
 
64
- it "ピアソン相関関数が返却される" do
65
- expect = 0.0
66
- v1 = [0, 2, 0]
90
+ it "ピアソン相関係数が返却される" do
91
+ expect = 0.8944271909999159
92
+ v1 = [3, 0, 1]
67
93
  v2 = [(3/2), (1/2), (1/2)]
68
94
  result = Kmeans::Pearson.calc(v1,v2)
69
95
  result.should == expect
70
96
  end
71
97
 
72
- it "ピアソン相関関数が返却される" do
73
- expect = 0.8164965809277261
74
- v1 = [4, 2, 3]
75
- v2 = [(3/1), (3/2), (3/1)]
98
+ it "ピアソン相関係数が返却される" do
99
+ expect = 0.5773502691896258
100
+ v1 = [0, 2, 0]
101
+ v2 = [(3/2), (3/2), (1/2)]
76
102
  result = Kmeans::Pearson.calc(v1,v2)
77
103
  result.should == expect
78
104
  end
79
105
 
80
- it "ピアソン相関関数が返却される" do
81
- expect = 0.5
106
+ it "ピアソン相関係数が返却される" do
107
+ expect = 0.8164965809277261
82
108
  v1 = [4, 2, 3]
83
- v2 = [2, 1, 0]
109
+ v2 = [(3/1), (3/2), (3/1)]
84
110
  result = Kmeans::Pearson.calc(v1,v2)
85
111
  result.should == expect
86
112
  end
87
113
 
88
- it "ピアソン相関関数が返却される" do
89
- expect = 0.0
114
+ it "ピアソン相関係数が返却される" do
115
+ expect = 0.5
90
116
  v1 = [4, 2, 3]
91
- v2 = [(3/2), (3/2), (1/2)]
117
+ v2 = [2, 1, 0]
92
118
  result = Kmeans::Pearson.calc(v1,v2)
93
119
  result.should == expect
94
120
  end
95
121
 
96
- it "ピアソン相関関数が返却される" do
122
+ it "ピアソン相関係数が返却される" do
97
123
  expect = 0.7071067811865475
98
124
  v1 = [4, 2, 3]
99
125
  v2 = [(3/2), (1/2), (1/2)]
@@ -101,7 +127,7 @@ describe Kmeans::Pearson do
101
127
  result.should == expect
102
128
  end
103
129
 
104
- it "ピアソン相関関数が返却される" do
130
+ it "ピアソン相関係数が返却される" do
105
131
  expect = 0.6666666666666666
106
132
  v1 = [3, 1, 1]
107
133
  v2 = [(3/1), (3/2), (3/1)]
@@ -109,7 +135,7 @@ describe Kmeans::Pearson do
109
135
  result.should == expect
110
136
  end
111
137
 
112
- it "ピアソン相関関数が返却される" do
138
+ it "ピアソン相関係数が返却される" do
113
139
  expect = 0.8164965809277261
114
140
  v1 = [3, 1, 1]
115
141
  v2 = [2, 1, 0]
@@ -117,7 +143,7 @@ describe Kmeans::Pearson do
117
143
  result.should == expect
118
144
  end
119
145
 
120
- it "ピアソン相関関数が返却される" do
146
+ it "ピアソン相関係数が返却される" do
121
147
  expect = 0.5773502691896258
122
148
  v1 = [3, 1, 1]
123
149
  v2 = [(3/2), (3/2), (1/2)]
@@ -125,55 +151,57 @@ describe Kmeans::Pearson do
125
151
  result.should == expect
126
152
  end
127
153
 
128
- it "ピアソン相関関数が返却される" do
129
- expect = 1.1547005383792517
130
- v1 = [3, 1, 1]
131
- v2 = [(3/2), (1/2), (1/2)]
154
+ it "ピアソン相関係数が返却される" do
155
+ expect = 0.31622776601683794
156
+ v1 = [3, 0, 1]
157
+ v2 = [(2/1), (1/1), (3/1)]
132
158
  result = Kmeans::Pearson.calc(v1,v2)
133
159
  result.should == expect
134
160
  end
135
161
 
136
- it "ピアソン相関関数が返却される" do
137
- expect = -0.7071067811865475
138
- v1 = [0, 1, 0]
139
- v2 = [(2/1), (1/1), (3/1)]
162
+ it "ピアソン相関係数が返却される" do
163
+ expect = 0.6324555320336759
164
+ v1 = [3, 0, 1]
165
+ v2 = [2, 1, 0]
140
166
  result = Kmeans::Pearson.calc(v1,v2)
141
167
  result.should == expect
142
168
  end
169
+ end
143
170
 
144
- it "ピアソン相関関数が返却される" do
145
- expect = 0.0
146
- v1 = [0, 1, 0]
147
- v2 = [2, 1, 0]
171
+ describe '相関係数が負となる集合を渡す場合' do
172
+ it "ピアソン相関係数が返却される" do
173
+ expect = -0.6666666666666666
174
+ v1 = [0, 2, 0]
175
+ v2 = [(3/1), (3/2), (3/1)]
148
176
  result = Kmeans::Pearson.calc(v1,v2)
149
177
  result.should == expect
150
178
  end
151
179
 
152
- it "ピアソン相関関数が返却される" do
153
- expect = 1.0
154
- v1 = [0, 1, 0]
180
+ it "ピアソン相関係数が返却される" do
181
+ expect = -0.4472135954999579
182
+ v1 = [3, 0, 1]
155
183
  v2 = [(0/1), (3/2), (0/1)]
156
184
  result = Kmeans::Pearson.calc(v1,v2)
157
185
  result.should == expect
158
186
  end
159
187
 
160
- it "ピアソン相関関数が返却される" do
161
- expect = 0.0
162
- v1 = [0, 1, 0]
163
- v2 = [(10/3), (1/1), (5/3)]
188
+ it "ピアソン相関係数が返却される" do
189
+ expect = -0.8164965809277261
190
+ v1 = [0, 2, 0]
191
+ v2 = [(2/1), (1/1), (3/1)]
164
192
  result = Kmeans::Pearson.calc(v1,v2)
165
193
  result.should == expect
166
194
  end
167
195
 
168
- it "ピアソン相関関数が返却される" do
169
- expect = 1.0
170
- v1 = [2, 1, 3]
196
+ it "ピアソン相関係数が返却される" do
197
+ expect = -0.7071067811865475
198
+ v1 = [0, 1, 0]
171
199
  v2 = [(2/1), (1/1), (3/1)]
172
200
  result = Kmeans::Pearson.calc(v1,v2)
173
201
  result.should == expect
174
202
  end
175
203
 
176
- it "ピアソン相関関数が返却される" do
204
+ it "ピアソン相関係数が返却される" do
177
205
  expect = -0.5
178
206
  v1 = [2, 1, 3]
179
207
  v2 = [2, 1, 0]
@@ -181,47 +209,35 @@ describe Kmeans::Pearson do
181
209
  result.should == expect
182
210
  end
183
211
 
184
- it "ピアソン相関関数が返却される" do
212
+ it "ピアソン相関係数が返却される" do
185
213
  expect = -0.7071067811865475
186
214
  v1 = [2, 1, 3]
187
215
  v2 = [(0/1), (3/2), (0/1)]
188
216
  result = Kmeans::Pearson.calc(v1,v2)
189
217
  result.should == expect
190
218
  end
219
+ end
191
220
 
192
- it "ピアソン相関関数が返却される" do
193
- expect = 0.0
194
- v1 = [2, 1, 3]
195
- v2 = [(10/3), (1/1), (5/3)]
221
+ describe '相関係数が 1.0 の場合' do
222
+ it "ピアソン相関係数が返却される" do
223
+ expect = 1.0
224
+ v1 = [0, 1, 0]
225
+ v2 = [(0/1), (3/2), (0/1)]
196
226
  result = Kmeans::Pearson.calc(v1,v2)
197
227
  result.should == expect
198
228
  end
199
229
 
200
- it "ピアソン相関関数が返却される" do
201
- expect = 0.31622776601683794
202
- v1 = [3, 0, 1]
230
+ it "ピアソン相関係数が返却される" do
231
+ expect = 1.0
232
+ v1 = [2, 1, 3]
203
233
  v2 = [(2/1), (1/1), (3/1)]
204
234
  result = Kmeans::Pearson.calc(v1,v2)
205
235
  result.should == expect
206
236
  end
237
+ end
207
238
 
208
- it "ピアソン相関関数が返却される" do
209
- expect = 0.6324555320336759
210
- v1 = [3, 0, 1]
211
- v2 = [2, 1, 0]
212
- result = Kmeans::Pearson.calc(v1,v2)
213
- result.should == expect
214
- end
215
-
216
- it "ピアソン相関関数が返却される" do
217
- expect = -0.4472135954999579
218
- v1 = [3, 0, 1]
219
- v2 = [(0/1), (3/2), (0/1)]
220
- result = Kmeans::Pearson.calc(v1,v2)
221
- result.should == expect
222
- end
223
-
224
- it "ピアソン相関関数が返却される" do
239
+ describe '相関係数が 1 を越える場合' do
240
+ it "ピアソン相関係数が返却される" do
225
241
  expect = 1.0327955589886444
226
242
  v1 = [3, 0, 1]
227
243
  v2 = [(10/3), (1/1), (5/3)]
@@ -229,26 +245,18 @@ describe Kmeans::Pearson do
229
245
  result.should == expect
230
246
  end
231
247
 
232
- it "ピアソン相関関数が返却される" do
233
- expect = -0.8164965809277261
234
- v1 = [0, 2, 0]
235
- v2 = [(2/1), (1/1), (3/1)]
236
- result = Kmeans::Pearson.calc(v1,v2)
237
- result.should == expect
238
- end
239
-
240
- it "ピアソン相関関数が返却される" do
241
- expect = 0.0
248
+ it "ピアソン相関係数が返却される" do
249
+ expect = 1.1547005383792517
242
250
  v1 = [0, 2, 0]
243
- v2 = [2, 1, 0]
251
+ v2 = [(0/1), (3/2), (0/1)]
244
252
  result = Kmeans::Pearson.calc(v1,v2)
245
253
  result.should == expect
246
254
  end
247
255
 
248
- it "ピアソン相関関数が返却される" do
256
+ it "ピアソン相関係数が返却される" do
249
257
  expect = 1.1547005383792517
250
- v1 = [0, 2, 0]
251
- v2 = [(0/1), (3/2), (0/1)]
258
+ v1 = [3, 1, 1]
259
+ v2 = [(3/2), (1/2), (1/2)]
252
260
  result = Kmeans::Pearson.calc(v1,v2)
253
261
  result.should == expect
254
262
  end
@@ -2,10 +2,10 @@
2
2
 
3
3
  require File.dirname(__FILE__) + '/../spec_helper'
4
4
 
5
- describe Kmeans, 'kmeans' do
5
+ describe Kmeans do
6
6
  context 'のバージョンを参照した場合' do
7
7
  it "バージョンが正しく表示される" do
8
- expect = '0.0.1'
8
+ expect = '0.0.2'
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.1
4
+ version: 0.0.2
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-07 00:00:00.000000000 Z
12
+ date: 2012-10-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cucumber
@@ -81,7 +81,7 @@ files:
81
81
  - lib/kmeans/cluster.rb
82
82
  - lib/kmeans/pair.rb
83
83
  - lib/kmeans/pearson.rb
84
- - script/.gitkeep
84
+ - script/build
85
85
  - spec/lib/kmeans/cluster_spec.rb
86
86
  - spec/lib/kmeans/pair_spec.rb
87
87
  - spec/lib/kmeans/pearson_spec.rb
File without changes