kmeans 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/script/.gitkeep ADDED
File without changes
@@ -0,0 +1,64 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + '/../../spec_helper'
4
+
5
+ describe Kmeans::Cluster do
6
+ before :all do
7
+ @testdata = {
8
+ "test01"=>
9
+ {"hoge"=>0,
10
+ "fuga"=>1,
11
+ "piyo"=>0
12
+ },
13
+ "test02"=>
14
+ {"hoge"=>2,
15
+ "fuga"=>1,
16
+ "piyo"=>3
17
+ },
18
+ "test03"=>
19
+ {"hoge"=>3,
20
+ "fuga"=>0,
21
+ "piyo"=>1
22
+ },
23
+ "test04"=>
24
+ {"hoge"=>0,
25
+ "fuga"=>2,
26
+ "piyo"=>0
27
+ },
28
+ "test05"=>
29
+ {"hoge"=>4,
30
+ "fuga"=>2,
31
+ "piyo"=>3
32
+ },
33
+ "test06"=>
34
+ {"hoge"=>3,
35
+ "fuga"=>1,
36
+ "piyo"=>1
37
+ }
38
+ }
39
+ end
40
+
41
+ context 'の Cluster クラスにおいて' do
42
+ describe '二次元ハッシュを渡すと' do
43
+ it "Kmeans::Cluster クラスが返却される" do
44
+ result = Kmeans::Cluster.new(@testdata, {
45
+ :centroids => 4,
46
+ :loop_max => 10
47
+ })
48
+ result.class.should be_equal Kmeans::Cluster
49
+ end
50
+ end
51
+
52
+ describe 'make_cluster メソッドを呼ぶと' do
53
+ it "ハッシュの配列が返却される (結果は実行ごとに異なる)" do
54
+ result = Kmeans::Cluster.new(@testdata, {
55
+ :centroids => 4,
56
+ :loop_max => 10
57
+ })
58
+ result.make_cluster
59
+ result.cluster.class.should be_equal Hash
60
+ result.cluster.values.class.should be_equal Array
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,46 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + '/../../spec_helper'
4
+
5
+ describe Kmeans::Pair do
6
+ context 'の Pair クラスにおいて' do
7
+ describe '最小値と最大値を渡す場合' do
8
+ it "ペアが返却される" do
9
+ min = 0
10
+ max = 4
11
+ expect = [0, 4]
12
+ result = Kmeans::Pair.new([min, max])
13
+ result[0].should == expect[0]
14
+ result[1].should == expect[1]
15
+ end
16
+
17
+ it "ペアが返却される" do
18
+ min = 1
19
+ max = 3
20
+ expect = [1, 3]
21
+ result = Kmeans::Pair.new([min, max])
22
+ result[0].should == expect[0]
23
+ result[1].should == expect[1]
24
+ end
25
+
26
+ it "ペアが返却される" do
27
+ min = 2
28
+ max = 4
29
+ unexpected = 6
30
+ expect = [2, 4]
31
+ result = Kmeans::Pair.new([min, max, unexpected])
32
+ result[0].should == expect[0]
33
+ result[1].should == expect[1]
34
+ end
35
+
36
+ it "ペアが返却される" do
37
+ min = 5
38
+ max = 5
39
+ expect = [5, 5]
40
+ result = Kmeans::Pair.new([min, max])
41
+ result[0].should == expect[0]
42
+ result[1].should == expect[1]
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,257 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + '/../../spec_helper'
4
+
5
+ describe Kmeans::Pearson do
6
+ context 'の Pearson クラスにおいて' do
7
+ describe '二つの変数を渡す場合' do
8
+ it "ピアソン相関関数が返却される" do
9
+ expect = 0.0
10
+ v1 = [4,2,3]
11
+ v2 = [0,0,1]
12
+ result = Kmeans::Pearson.calc(v1,v2)
13
+ result.should == expect
14
+ end
15
+
16
+ it "ピアソン相関関数が返却される" do
17
+ expect = 0.31622776601683794
18
+ v1 = [3, 0, 1]
19
+ v2 = [(2/1), (1/1), (3/1)]
20
+ result = Kmeans::Pearson.calc(v1,v2)
21
+ result.should == expect
22
+ end
23
+
24
+ it "ピアソン相関関数が返却される" do
25
+ expect = 0.4472135954999579
26
+ v1 = [3, 0, 1]
27
+ v2 = [(3/2), (3/2), (1/2)]
28
+ result = Kmeans::Pearson.calc(v1,v2)
29
+ result.should == expect
30
+ end
31
+
32
+ it "ピアソン相関関数が返却される" do
33
+ expect = 0.8944271909999159
34
+ v1 = [3, 0, 1]
35
+ v2 = [(3/2), (1/2), (1/2)]
36
+ result = Kmeans::Pearson.calc(v1,v2)
37
+ result.should == expect
38
+ end
39
+
40
+ it "ピアソン相関関数が返却される" do
41
+ expect = -0.6666666666666666
42
+ v1 = [0, 2, 0]
43
+ v2 = [(3/1), (3/2), (3/1)]
44
+ result = Kmeans::Pearson.calc(v1,v2)
45
+ result.should == expect
46
+ end
47
+
48
+ it "ピアソン相関関数が返却される" do
49
+ expect = 0.0
50
+ v1 = [0, 2, 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.5773502691896258
58
+ v1 = [0, 2, 0]
59
+ v2 = [(3/2), (3/2), (1/2)]
60
+ result = Kmeans::Pearson.calc(v1,v2)
61
+ result.should == expect
62
+ end
63
+
64
+ it "ピアソン相関関数が返却される" do
65
+ expect = 0.0
66
+ v1 = [0, 2, 0]
67
+ v2 = [(3/2), (1/2), (1/2)]
68
+ result = Kmeans::Pearson.calc(v1,v2)
69
+ result.should == expect
70
+ end
71
+
72
+ it "ピアソン相関関数が返却される" do
73
+ expect = 0.8164965809277261
74
+ v1 = [4, 2, 3]
75
+ v2 = [(3/1), (3/2), (3/1)]
76
+ result = Kmeans::Pearson.calc(v1,v2)
77
+ result.should == expect
78
+ end
79
+
80
+ it "ピアソン相関関数が返却される" do
81
+ expect = 0.5
82
+ v1 = [4, 2, 3]
83
+ v2 = [2, 1, 0]
84
+ result = Kmeans::Pearson.calc(v1,v2)
85
+ result.should == expect
86
+ end
87
+
88
+ it "ピアソン相関関数が返却される" do
89
+ expect = 0.0
90
+ v1 = [4, 2, 3]
91
+ v2 = [(3/2), (3/2), (1/2)]
92
+ result = Kmeans::Pearson.calc(v1,v2)
93
+ result.should == expect
94
+ end
95
+
96
+ it "ピアソン相関関数が返却される" do
97
+ expect = 0.7071067811865475
98
+ v1 = [4, 2, 3]
99
+ v2 = [(3/2), (1/2), (1/2)]
100
+ result = Kmeans::Pearson.calc(v1,v2)
101
+ result.should == expect
102
+ end
103
+
104
+ it "ピアソン相関関数が返却される" do
105
+ expect = 0.6666666666666666
106
+ v1 = [3, 1, 1]
107
+ v2 = [(3/1), (3/2), (3/1)]
108
+ result = Kmeans::Pearson.calc(v1,v2)
109
+ result.should == expect
110
+ end
111
+
112
+ it "ピアソン相関関数が返却される" do
113
+ expect = 0.8164965809277261
114
+ v1 = [3, 1, 1]
115
+ v2 = [2, 1, 0]
116
+ result = Kmeans::Pearson.calc(v1,v2)
117
+ result.should == expect
118
+ end
119
+
120
+ it "ピアソン相関関数が返却される" do
121
+ expect = 0.5773502691896258
122
+ v1 = [3, 1, 1]
123
+ v2 = [(3/2), (3/2), (1/2)]
124
+ result = Kmeans::Pearson.calc(v1,v2)
125
+ result.should == expect
126
+ end
127
+
128
+ it "ピアソン相関関数が返却される" do
129
+ expect = 1.1547005383792517
130
+ v1 = [3, 1, 1]
131
+ v2 = [(3/2), (1/2), (1/2)]
132
+ result = Kmeans::Pearson.calc(v1,v2)
133
+ result.should == expect
134
+ end
135
+
136
+ it "ピアソン相関関数が返却される" do
137
+ expect = -0.7071067811865475
138
+ v1 = [0, 1, 0]
139
+ v2 = [(2/1), (1/1), (3/1)]
140
+ result = Kmeans::Pearson.calc(v1,v2)
141
+ result.should == expect
142
+ end
143
+
144
+ it "ピアソン相関関数が返却される" do
145
+ expect = 0.0
146
+ v1 = [0, 1, 0]
147
+ v2 = [2, 1, 0]
148
+ result = Kmeans::Pearson.calc(v1,v2)
149
+ result.should == expect
150
+ end
151
+
152
+ it "ピアソン相関関数が返却される" do
153
+ expect = 1.0
154
+ v1 = [0, 1, 0]
155
+ v2 = [(0/1), (3/2), (0/1)]
156
+ result = Kmeans::Pearson.calc(v1,v2)
157
+ result.should == expect
158
+ end
159
+
160
+ it "ピアソン相関関数が返却される" do
161
+ expect = 0.0
162
+ v1 = [0, 1, 0]
163
+ v2 = [(10/3), (1/1), (5/3)]
164
+ result = Kmeans::Pearson.calc(v1,v2)
165
+ result.should == expect
166
+ end
167
+
168
+ it "ピアソン相関関数が返却される" do
169
+ expect = 1.0
170
+ v1 = [2, 1, 3]
171
+ v2 = [(2/1), (1/1), (3/1)]
172
+ result = Kmeans::Pearson.calc(v1,v2)
173
+ result.should == expect
174
+ end
175
+
176
+ it "ピアソン相関関数が返却される" do
177
+ expect = -0.5
178
+ v1 = [2, 1, 3]
179
+ v2 = [2, 1, 0]
180
+ result = Kmeans::Pearson.calc(v1,v2)
181
+ result.should == expect
182
+ end
183
+
184
+ it "ピアソン相関関数が返却される" do
185
+ expect = -0.7071067811865475
186
+ v1 = [2, 1, 3]
187
+ v2 = [(0/1), (3/2), (0/1)]
188
+ result = Kmeans::Pearson.calc(v1,v2)
189
+ result.should == expect
190
+ end
191
+
192
+ it "ピアソン相関関数が返却される" do
193
+ expect = 0.0
194
+ v1 = [2, 1, 3]
195
+ v2 = [(10/3), (1/1), (5/3)]
196
+ result = Kmeans::Pearson.calc(v1,v2)
197
+ result.should == expect
198
+ end
199
+
200
+ it "ピアソン相関関数が返却される" do
201
+ expect = 0.31622776601683794
202
+ v1 = [3, 0, 1]
203
+ v2 = [(2/1), (1/1), (3/1)]
204
+ result = Kmeans::Pearson.calc(v1,v2)
205
+ result.should == expect
206
+ end
207
+
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
225
+ expect = 1.0327955589886444
226
+ v1 = [3, 0, 1]
227
+ v2 = [(10/3), (1/1), (5/3)]
228
+ result = Kmeans::Pearson.calc(v1,v2)
229
+ result.should == expect
230
+ end
231
+
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
242
+ v1 = [0, 2, 0]
243
+ v2 = [2, 1, 0]
244
+ result = Kmeans::Pearson.calc(v1,v2)
245
+ result.should == expect
246
+ end
247
+
248
+ it "ピアソン相関関数が返却される" do
249
+ expect = 1.1547005383792517
250
+ v1 = [0, 2, 0]
251
+ v2 = [(0/1), (3/2), (0/1)]
252
+ result = Kmeans::Pearson.calc(v1,v2)
253
+ result.should == expect
254
+ end
255
+ end
256
+ end
257
+ end
@@ -0,0 +1,13 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe Kmeans, 'kmeans' do
6
+ context 'のバージョンを参照した場合' do
7
+ it "バージョンが正しく表示される" do
8
+ expect = '0.0.1'
9
+ Kmeans.const_get(:VERSION).should be_true
10
+ Kmeans.const_get(:VERSION).should == expect
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), ".."))
4
+ $LOAD_PATH.unshift APP_ROOT
5
+ $LOAD_PATH.unshift File.join(APP_ROOT)
6
+ $LOAD_PATH.unshift File.join(APP_ROOT, 'lib')
7
+
8
+ require 'kmeans'
9
+
10
+ if ENV['COVERAGE'] == 'on'
11
+ require 'simplecov'
12
+ require 'simplecov-rcov'
13
+ SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
14
+
15
+ SimpleCov.start do
16
+ add_filter "spec"
17
+ add_filter "vendor"
18
+ end
19
+ end
20
+
21
+ unless /^1\.9\./ =~ RUBY_VERSION
22
+ require 'rspec'
23
+ end
24
+
25
+ RSpec.configure do |config|
26
+ end
data/vendor/.gitkeep ADDED
File without changes
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kmeans
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - id774
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cucumber
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: jeweler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: K-means clustering
63
+ email: idnanashi@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files:
67
+ - README.md
68
+ files:
69
+ - Gemfile
70
+ - README.md
71
+ - Rakefile
72
+ - VERSION
73
+ - doc/AUTHORS
74
+ - doc/COPYING
75
+ - doc/COPYING.LESSER
76
+ - doc/ChangeLog
77
+ - doc/LICENSE
78
+ - doc/README
79
+ - kmeans.gemspec
80
+ - lib/kmeans.rb
81
+ - lib/kmeans/cluster.rb
82
+ - lib/kmeans/pair.rb
83
+ - lib/kmeans/pearson.rb
84
+ - script/.gitkeep
85
+ - spec/lib/kmeans/cluster_spec.rb
86
+ - spec/lib/kmeans/pair_spec.rb
87
+ - spec/lib/kmeans/pearson_spec.rb
88
+ - spec/lib/kmeans_spec.rb
89
+ - spec/spec_helper.rb
90
+ - vendor/.gitkeep
91
+ homepage: http://github.com/id774/kmeans
92
+ licenses:
93
+ - GPL
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.24
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: kmeans
116
+ test_files: []