lite 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lite/cluster.rb +15 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5be315e570f8fa335d6059e3e1c03b168618d6fc
|
4
|
+
data.tar.gz: 65d024a05f399a503c094f994b4515651a0cfd86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0eabf693fbfbc5233bd63cf0eaa1b502c4ae1eafa47170fd00a5c87201d1b168ba713f5ccbe3e49e3bcb7abdb22a9ab94caec8edb291a4d45c3cd37a43fb3d8f
|
7
|
+
data.tar.gz: 3ada7a12747ae9cc62d9326ab75cfcadec2a297f86fae466e6cfbc24e48d186e50b1b44e55a5ce527adf311df3bad37e8e3807a062bb6418e75aa5d94f59beaa
|
data/lib/lite/cluster.rb
CHANGED
@@ -8,15 +8,16 @@ module Cluster
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def observe!( instance )
|
11
|
-
|
11
|
+
u = SparseVector.new instance
|
12
|
+
|
12
13
|
if @centroids.size == 0
|
13
|
-
@centroids <<
|
14
|
+
@centroids << Centroid.new( u )
|
14
15
|
return self
|
15
16
|
end
|
16
17
|
|
17
|
-
@centroids.sort! {|c1, c2|
|
18
|
+
@centroids.sort! {|c1, c2| u.dist(c1.x) <=> u.dist(c2.x) }
|
18
19
|
closest_centroid = @centroids.first
|
19
|
-
closest_centroid.update!(
|
20
|
+
closest_centroid.update!( u )
|
20
21
|
|
21
22
|
if( @centroids.size >= @k_max )
|
22
23
|
pairs = []
|
@@ -36,15 +37,14 @@ module Cluster
|
|
36
37
|
@centroids[merge_info[1]].merge!( @centroids[merge_info[2]] )
|
37
38
|
@centroids = @centroids - [ @centroids[merge_info[2]] ]
|
38
39
|
end
|
39
|
-
@centroids << Centroid.new(
|
40
|
+
@centroids << Centroid.new( u )
|
40
41
|
|
41
42
|
[]
|
42
43
|
end
|
43
44
|
|
44
|
-
def
|
45
|
+
def centroids( min_num_instances_in_cluster = 2 )
|
45
46
|
@centroids.each do |c|
|
46
47
|
next if c.n >= min_num_instances_in_cluster
|
47
|
-
p c
|
48
48
|
@centroids = @centroids - [ c ]
|
49
49
|
next if c.n == 0
|
50
50
|
aux = @centroids.inject( {:min_c => @centroids.first, :d => @centroids.first.x.dist( c.x )} ) {|a,cc| cc.nil? || cc.x.dist(c.x) > a[:d] ? a : { :min_c=>cc, :d=>cc.x.dist(c.x)} }
|
@@ -54,15 +54,15 @@ module Cluster
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
class Centroid
|
58
|
-
attr_accessor :x
|
57
|
+
class Centroid
|
58
|
+
attr_accessor :n,:x
|
59
59
|
def initialize( x )
|
60
60
|
@x = x
|
61
|
-
@n =
|
61
|
+
@n = 1
|
62
62
|
end
|
63
63
|
|
64
64
|
def update!( newX )
|
65
|
-
@x += ( @x
|
65
|
+
@x += ( @x.mult_scalar(@n) + newX ).mult_scalar( 1.0/(@n+1) )
|
66
66
|
@n += 1
|
67
67
|
self
|
68
68
|
end
|
@@ -72,6 +72,10 @@ module Cluster
|
|
72
72
|
@n += centroid.n
|
73
73
|
self
|
74
74
|
end
|
75
|
+
|
76
|
+
def to_s
|
77
|
+
"Centroid #{x.attr} of #{n} instances"
|
78
|
+
end
|
75
79
|
end
|
76
80
|
|
77
81
|
end
|