lite 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lite/cluster.rb +15 -11
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d79082f5ca6160a1ef4b0356e6708b22fe0d67a
4
- data.tar.gz: 0a05a1776c11c33497a1f9afcbc29d5eca1f5912
3
+ metadata.gz: 5be315e570f8fa335d6059e3e1c03b168618d6fc
4
+ data.tar.gz: 65d024a05f399a503c094f994b4515651a0cfd86
5
5
  SHA512:
6
- metadata.gz: 4445ce29ed39fe1da5cfed1b9825c4d8a2ced615939c27d4f02c77e89219c2f240e72b6a8c5202246a2317352228faffefb80d3729664038ba8cbf134b92181f
7
- data.tar.gz: 6cbf5e1f884c1ff9a7d52f4a10d4e5aacf0bdc54b1b12e7dac3e5f8b2bf07129664dd84ed7ecd0ceb0e4afa00178979c89a5ec5e0fa20f9359b6f22966024529
6
+ metadata.gz: 0eabf693fbfbc5233bd63cf0eaa1b502c4ae1eafa47170fd00a5c87201d1b168ba713f5ccbe3e49e3bcb7abdb22a9ab94caec8edb291a4d45c3cd37a43fb3d8f
7
+ data.tar.gz: 3ada7a12747ae9cc62d9326ab75cfcadec2a297f86fae466e6cfbc24e48d186e50b1b44e55a5ce527adf311df3bad37e8e3807a062bb6418e75aa5d94f59beaa
@@ -8,15 +8,16 @@ module Cluster
8
8
  end
9
9
 
10
10
  def observe!( instance )
11
- cinstance = Centroid.new( instance )
11
+ u = SparseVector.new instance
12
+
12
13
  if @centroids.size == 0
13
- @centroids << cinstance
14
+ @centroids << Centroid.new( u )
14
15
  return self
15
16
  end
16
17
 
17
- @centroids.sort! {|c1, c2| instance.dist(c1.x) <=> instance.dist(c2.x) }
18
+ @centroids.sort! {|c1, c2| u.dist(c1.x) <=> u.dist(c2.x) }
18
19
  closest_centroid = @centroids.first
19
- closest_centroid.update!( cinstance )
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( cinstance )
40
+ @centroids << Centroid.new( u )
40
41
 
41
42
  []
42
43
  end
43
44
 
44
- def getCentroids( min_num_instances_in_cluster = 2 )
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 < SparseVector
58
- attr_accessor :x,:n
57
+ class Centroid
58
+ attr_accessor :n,:x
59
59
  def initialize( x )
60
60
  @x = x
61
- @n = 0
61
+ @n = 1
62
62
  end
63
63
 
64
64
  def update!( newX )
65
- @x += ( @x - newX ).mult_scalar( 1.0/(@n+1) )
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - ronbee