davidrichards-kmeans 0.0.3 → 0.0.4
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.
- data/VERSION.yml +1 -1
- data/lib/kmeans/agent.rb +10 -0
- data/spec/kmeans/agent_spec.rb +6 -0
- metadata +4 -3
data/VERSION.yml
CHANGED
data/lib/kmeans/agent.rb
CHANGED
|
@@ -21,6 +21,12 @@ module KMeans #:nodoc:
|
|
|
21
21
|
# The centroids used for the clustering
|
|
22
22
|
attr_reader :centroids
|
|
23
23
|
|
|
24
|
+
# The maximum number of iterations allowed
|
|
25
|
+
attr_reader :max
|
|
26
|
+
|
|
27
|
+
# The number of iterations used
|
|
28
|
+
attr_reader :num_iterations
|
|
29
|
+
|
|
24
30
|
# All the affectd nodes
|
|
25
31
|
def nodes
|
|
26
32
|
Node.nodes
|
|
@@ -44,6 +50,7 @@ module KMeans #:nodoc:
|
|
|
44
50
|
@k = opts[:k]
|
|
45
51
|
@centroids = opts.fetch(:centroids, false)
|
|
46
52
|
@online = opts.fetch(:online, false)
|
|
53
|
+
@max = opts.fetch(:max, 10_000)
|
|
47
54
|
else
|
|
48
55
|
@k = opts
|
|
49
56
|
end
|
|
@@ -67,9 +74,12 @@ module KMeans #:nodoc:
|
|
|
67
74
|
def stabilize_centroids
|
|
68
75
|
@centroids ||= infer_centroids(@k)
|
|
69
76
|
n = Node.cluster_to(@centroids)
|
|
77
|
+
@num_iterations = 0
|
|
70
78
|
while n > 0
|
|
71
79
|
@centroids.each { |c| c.rebalance }
|
|
72
80
|
n = Node.cluster_to(@centroids)
|
|
81
|
+
@num_iterations += 1
|
|
82
|
+
break if self.num_iterations >= self.max
|
|
73
83
|
end
|
|
74
84
|
end
|
|
75
85
|
|
data/spec/kmeans/agent_spec.rb
CHANGED
|
@@ -26,5 +26,11 @@ describe Agent do
|
|
|
26
26
|
c1.position.should eql([2,2,2])
|
|
27
27
|
c2.position.should eql([8,8,8])
|
|
28
28
|
end
|
|
29
|
+
|
|
30
|
+
it "should be able to set a maximum number of iterations" do
|
|
31
|
+
agent = Agent.process(:k => 2, :max => 1, *@node_list)
|
|
32
|
+
agent.num_iterations.should eql(1)
|
|
33
|
+
|
|
34
|
+
end
|
|
29
35
|
|
|
30
36
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: davidrichards-kmeans
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- David Richards
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2009-07-
|
|
12
|
+
date: 2009-07-30 00:00:00 -07:00
|
|
13
13
|
default_executable: kmeans
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
@@ -54,6 +54,7 @@ files:
|
|
|
54
54
|
- spec/spec_helper.rb
|
|
55
55
|
has_rdoc: true
|
|
56
56
|
homepage: http://github.com/davidrichards/kmeans
|
|
57
|
+
licenses:
|
|
57
58
|
post_install_message:
|
|
58
59
|
rdoc_options:
|
|
59
60
|
- --inline-source
|
|
@@ -75,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
75
76
|
requirements: []
|
|
76
77
|
|
|
77
78
|
rubyforge_project:
|
|
78
|
-
rubygems_version: 1.
|
|
79
|
+
rubygems_version: 1.3.5
|
|
79
80
|
signing_key:
|
|
80
81
|
specification_version: 2
|
|
81
82
|
summary: KMeans for clustering
|