ai4ruby 1.13 → 1.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,18 +13,33 @@ module Ai4r
13
13
 
14
14
  attr_reader :cluster_tree
15
15
 
16
- def initialize
16
+ def initialize(depth = nil)
17
17
  @cluster_tree = []
18
+ @depth = depth
19
+ @merges_so_far = 0
20
+ super()
21
+ end
22
+
23
+ def build(data_set, number_of_clusters)
24
+ data_len = data_set.data_items.length
25
+ @total_merges = data_len - number_of_clusters
18
26
  super
27
+ @cluster_tree << self.clusters
28
+ @cluster_tree.reverse!
29
+ return self
19
30
  end
20
31
 
21
32
  protected
22
33
 
23
34
  def merge_clusters(index_a, index_b, index_clusters)
24
- # store current clusters
25
- stored_distance_matrix = @distance_matrix.dup
26
- @cluster_tree << build_clusters_from_index_clusters(index_clusters)
27
- @distance_matrix = stored_distance_matrix
35
+ # only store if no or above depth
36
+ if @depth.nil? or @merges_so_far > @total_merges - @depth
37
+ # store current clusters
38
+ stored_distance_matrix = @distance_matrix.dup
39
+ @cluster_tree << build_clusters_from_index_clusters(index_clusters)
40
+ @distance_matrix = stored_distance_matrix
41
+ end
42
+ @merges_so_far += 1
28
43
  super
29
44
  end
30
45
  end
@@ -52,8 +52,28 @@ class Ai4r::Clusterers::WardLinkageHierarchicalTest < Test::Unit::TestCase
52
52
  def test_cluster_tree
53
53
  clusterer = Ai4r::Clusterers::WardLinkageHierarchical.new
54
54
  clusterer.build(DataSet.new(:data_items => @@data), 1)
55
-
56
- assert_equal clusterer.cluster_tree.length, @@data.length - 1
55
+ assert_equal @@data.length, clusterer.cluster_tree.length
56
+ end
57
+
58
+ def test_cluster_tree_limit
59
+ depth = 5
60
+ clusterer = Ai4r::Clusterers::WardLinkageHierarchical.new(5)
61
+ clusterer.build(DataSet.new(:data_items => @@data), 1)
62
+ assert_equal 5, clusterer.cluster_tree.length
63
+ end
64
+
65
+ def test_cluster_tree_first_length
66
+ depth = 5
67
+ clusterer = Ai4r::Clusterers::WardLinkageHierarchical.new(5)
68
+ clusterer.build(DataSet.new(:data_items => @@data), 1)
69
+ assert_equal 1, clusterer.cluster_tree.first.length
70
+ end
71
+
72
+ def test_cluster_tree_last_length
73
+ depth = 5
74
+ clusterer = Ai4r::Clusterers::WardLinkageHierarchical.new(5)
75
+ clusterer.build(DataSet.new(:data_items => @@data), 1)
76
+ assert_equal 5, clusterer.cluster_tree.last.length
57
77
  end
58
78
 
59
79
 
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai4ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 13
9
- version: "1.13"
8
+ - 14
9
+ version: "1.14"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Sergio Fierens
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-04 00:00:00 Z
18
+ date: 2012-02-05 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: