ai4r 1.12 → 1.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/README.rdoc +7 -12
  2. data/examples/classifiers/simple_linear_regression_example.csv +159 -0
  3. data/examples/classifiers/simple_linear_regression_example.rb +15 -0
  4. data/examples/clusterers/clusterer_example.rb +56 -0
  5. data/examples/neural_network/backpropagation_example.rb +2 -1
  6. data/lib/ai4r.rb +3 -1
  7. data/lib/ai4r/classifiers/id3.rb +6 -2
  8. data/lib/ai4r/classifiers/multilayer_perceptron.rb +1 -1
  9. data/lib/ai4r/classifiers/naive_bayes.rb +24 -21
  10. data/lib/ai4r/classifiers/simple_linear_regression.rb +118 -0
  11. data/lib/ai4r/clusterers/average_linkage.rb +3 -3
  12. data/lib/ai4r/clusterers/bisecting_k_means.rb +2 -2
  13. data/lib/ai4r/clusterers/centroid_linkage.rb +3 -3
  14. data/lib/ai4r/clusterers/clusterer.rb +0 -11
  15. data/lib/ai4r/clusterers/complete_linkage.rb +3 -3
  16. data/lib/ai4r/clusterers/diana.rb +2 -2
  17. data/lib/ai4r/clusterers/k_means.rb +123 -21
  18. data/lib/ai4r/clusterers/median_linkage.rb +3 -3
  19. data/lib/ai4r/clusterers/single_linkage.rb +4 -4
  20. data/lib/ai4r/clusterers/ward_linkage.rb +4 -4
  21. data/lib/ai4r/clusterers/ward_linkage_hierarchical.rb +48 -0
  22. data/lib/ai4r/clusterers/weighted_average_linkage.rb +3 -3
  23. data/lib/ai4r/data/data_set.rb +12 -3
  24. data/lib/ai4r/data/proximity.rb +22 -0
  25. data/lib/ai4r/neural_network/backpropagation.rb +26 -15
  26. data/test/classifiers/id3_test.rb +12 -0
  27. data/test/classifiers/multilayer_perceptron_test.rb +1 -1
  28. data/test/classifiers/naive_bayes_test.rb +18 -18
  29. data/test/classifiers/simple_linear_regression_test.rb +37 -0
  30. data/test/clusterers/k_means_test.rb +75 -8
  31. data/test/clusterers/ward_linkage_hierarchical_test.rb +81 -0
  32. data/test/data/data_set_test.rb +8 -0
  33. data/test/data/proximity_test.rb +7 -1
  34. metadata +96 -55
@@ -0,0 +1,81 @@
1
+ # Author:: Sergio Fierens (implementation)
2
+ # License:: MPL 1.1
3
+ # Project:: ai4r
4
+ # Url:: http://ai4r.rubyforge.org/
5
+ #
6
+ # You can redistribute it and/or modify it under the terms of
7
+ # the Mozilla Public License version 1.1 as published by the
8
+ # Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
9
+
10
+ require 'test/unit'
11
+ require File.dirname(__FILE__) + '/../../lib/ai4r/clusterers/ward_linkage_hierarchical'
12
+
13
+ class Ai4r::Clusterers::WardLinkageHierarchical
14
+ attr_accessor :data_set, :number_of_clusters, :clusters, :distance_matrix, :index_clusters
15
+ end
16
+
17
+ class Ai4r::Clusterers::WardLinkageHierarchicalTest < Test::Unit::TestCase
18
+
19
+ include Ai4r::Clusterers
20
+ include Ai4r::Data
21
+
22
+ @@data = [ [10, 3], [3, 10], [2, 8], [2, 5], [3, 8], [10, 3],
23
+ [1, 3], [8, 1], [2, 9], [2, 5], [3, 3], [9, 4]]
24
+
25
+ @@expected_distance_matrix = [
26
+ [98.0],
27
+ [89.0, 5.0],
28
+ [68.0, 26.0, 9.0],
29
+ [74.0, 4.0, 1.0, 10.0],
30
+ [0.0, 98.0, 89.0, 68.0, 74.0],
31
+ [81.0, 53.0, 26.0, 5.0, 29.0, 81.0],
32
+ [8.0, 106.0, 85.0, 52.0, 74.0, 8.0, 53.0],
33
+ [100.0, 2.0, 1.0, 16.0, 2.0, 100.0, 37.0, 100.0],
34
+ [68.0, 26.0, 9.0, 0.0, 10.0, 68.0, 5.0, 52.0, 16.0],
35
+ [49.0, 49.0, 26.0, 5.0, 25.0, 49.0, 4.0, 29.0, 37.0, 5.0],
36
+ [2.0, 72.0, 65.0, 50.0, 52.0, 2.0, 65.0, 10.0, 74.0, 50.0, 37.0]]
37
+
38
+ def setup
39
+ Ai4r::Clusterers::WardLinkageHierarchical.send(:public,
40
+ *Ai4r::Clusterers::WardLinkageHierarchical.protected_instance_methods)
41
+ end
42
+
43
+ def test_linkage_distance
44
+ clusterer = Ai4r::Clusterers::WardLinkageHierarchical.new
45
+ clusterer.data_set = DataSet.new :data_items => @@data
46
+ clusterer.index_clusters = clusterer.create_initial_index_clusters
47
+ clusterer.distance_matrix = @@expected_distance_matrix
48
+ assert_in_delta 123.4166, clusterer.linkage_distance(0,1,2), 0.0001
49
+ assert_equal 27.75, clusterer.linkage_distance(4,2,5)
50
+ end
51
+
52
+ def test_cluster_tree
53
+ clusterer = Ai4r::Clusterers::WardLinkageHierarchical.new
54
+ clusterer.build(DataSet.new(:data_items => @@data), 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
77
+ end
78
+
79
+
80
+ end
81
+
@@ -18,6 +18,14 @@ module Ai4r
18
18
  set = DataSet.new.load_csv_with_labels("#{File.dirname(__FILE__)}/data_set.csv")
19
19
  assert_equal 120, set.data_items.length
20
20
  assert_equal ["zone", "rooms", "size", "price"], set.data_labels
21
+ assert_equal ["Moron Sur (GBA)","2","[28 m2 - 39 m2]","[29K-35K]"], set.data_items.first
22
+ end
23
+
24
+ def test_parse_csv_with_labels
25
+ set = DataSet.new.parse_csv_with_labels("#{File.dirname(__FILE__)}/data_set.csv")
26
+ assert_equal 120, set.data_items.length
27
+ assert_equal ["zone", "rooms", "size", "price"], set.data_labels
28
+ assert_equal ["Moron Sur (GBA)",2.0,"[28 m2 - 39 m2]","[29K-35K]"], set.data_items.first
21
29
  end
22
30
 
23
31
  def test_build_domains
@@ -14,7 +14,7 @@ module Ai4r
14
14
  module Data
15
15
  class ProximityTest < Test::Unit::TestCase
16
16
 
17
- @@delta = 0.0001
17
+ @@delta = 0.0000001
18
18
  @@data1 = [rand*10, rand*10, rand*-10]
19
19
  @@data2 = [rand*10, rand*-10, rand*10]
20
20
 
@@ -76,6 +76,12 @@ module Ai4r
76
76
  assert_equal 1.0/0, Proximity.simple_matching_distance([3], [0])
77
77
  end
78
78
 
79
+ def test_cosine_distance
80
+ assert_in_delta 0.0, Proximity.cosine_distance(@@data1, @@data1), @@delta
81
+ assert_equal Proximity.cosine_distance(@@data1, @@data2),
82
+ Proximity.cosine_distance(@@data2, @@data1)
83
+ assert_in_delta 0.0, Proximity.cosine_distance(@@data1, @@data1), @@delta
84
+ end
79
85
  end
80
86
  end
81
87
  end
metadata CHANGED
@@ -1,11 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai4r
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 21
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
- - 12
8
- version: "1.12"
8
+ - 13
9
+ version: "1.13"
9
10
  platform: ruby
10
11
  authors:
11
12
  - Sergio Fierens
@@ -13,7 +14,7 @@ autorequire:
13
14
  bindir: bin
14
15
  cert_chain: []
15
16
 
16
- date: 2012-09-07 00:00:00 -05:00
17
+ date: 2014-02-05 00:00:00 -06:00
17
18
  default_executable:
18
19
  dependencies: []
19
20
 
@@ -26,55 +27,89 @@ extensions: []
26
27
  extra_rdoc_files:
27
28
  - README.rdoc
28
29
  files:
30
+ - examples/classifiers/id3_data.csv
31
+ - examples/classifiers/id3_example.rb
32
+ - examples/classifiers/naive_bayes_data.csv
33
+ - examples/classifiers/naive_bayes_example.rb
34
+ - examples/classifiers/results.txt
35
+ - examples/classifiers/simple_linear_regression_example.csv
36
+ - examples/classifiers/simple_linear_regression_example.rb
37
+ - examples/clusterers/clusterer_example.rb
38
+ - examples/genetic_algorithm/genetic_algorithm_example.rb
39
+ - examples/genetic_algorithm/travel_cost.csv
40
+ - examples/neural_network/backpropagation_example.rb
29
41
  - examples/neural_network/patterns_with_base_noise.rb
42
+ - examples/neural_network/patterns_with_noise.rb
30
43
  - examples/neural_network/training_patterns.rb
31
44
  - examples/neural_network/xor_example.rb
32
- - examples/neural_network/patterns_with_noise.rb
33
- - examples/neural_network/backpropagation_example.rb
34
45
  - examples/som/som_data.rb
35
- - examples/som/som_single_example.rb
36
46
  - examples/som/som_multi_node_example.rb
37
- - examples/classifiers/id3_example.rb
38
- - examples/classifiers/results.txt
39
- - examples/classifiers/naive_bayes_example.rb
40
- - examples/classifiers/naive_bayes_data.csv
41
- - examples/classifiers/id3_data.csv
42
- - examples/genetic_algorithm/genetic_algorithm_example.rb
43
- - examples/genetic_algorithm/travel_cost.csv
47
+ - examples/som/som_single_example.rb
44
48
  - lib/ai4r.rb
45
- - lib/ai4r/data/proximity.rb
46
- - lib/ai4r/data/parameterizable.rb
47
- - lib/ai4r/data/data_set.rb
48
- - lib/ai4r/data/statistics.rb
49
- - lib/ai4r/neural_network/backpropagation.rb
50
- - lib/ai4r/neural_network/hopfield.rb
51
- - lib/ai4r/som/node.rb
52
- - lib/ai4r/som/som.rb
53
- - lib/ai4r/som/two_phase_layer.rb
54
- - lib/ai4r/som/layer.rb
49
+ - lib/ai4r/classifiers/classifier.rb
50
+ - lib/ai4r/classifiers/hyperpipes.rb
55
51
  - lib/ai4r/classifiers/ib1.rb
56
52
  - lib/ai4r/classifiers/id3.rb
57
- - lib/ai4r/classifiers/naive_bayes.rb
58
- - lib/ai4r/classifiers/hyperpipes.rb
59
- - lib/ai4r/classifiers/zero_r.rb
60
53
  - lib/ai4r/classifiers/multilayer_perceptron.rb
54
+ - lib/ai4r/classifiers/naive_bayes.rb
61
55
  - lib/ai4r/classifiers/one_r.rb
62
- - lib/ai4r/classifiers/classifier.rb
63
56
  - lib/ai4r/classifiers/prism.rb
64
- - lib/ai4r/experiment/classifier_evaluator.rb
65
- - lib/ai4r/genetic_algorithm/genetic_algorithm.rb
66
- - lib/ai4r/clusterers/complete_linkage.rb
67
- - lib/ai4r/clusterers/single_linkage.rb
68
- - lib/ai4r/clusterers/k_means.rb
69
- - lib/ai4r/clusterers/clusterer.rb
70
- - lib/ai4r/clusterers/centroid_linkage.rb
71
- - lib/ai4r/clusterers/diana.rb
57
+ - lib/ai4r/classifiers/simple_linear_regression.rb
58
+ - lib/ai4r/classifiers/zero_r.rb
72
59
  - lib/ai4r/clusterers/average_linkage.rb
73
60
  - lib/ai4r/clusterers/bisecting_k_means.rb
74
- - lib/ai4r/clusterers/ward_linkage.rb
61
+ - lib/ai4r/clusterers/centroid_linkage.rb
62
+ - lib/ai4r/clusterers/clusterer.rb
63
+ - lib/ai4r/clusterers/complete_linkage.rb
64
+ - lib/ai4r/clusterers/diana.rb
65
+ - lib/ai4r/clusterers/k_means.rb
75
66
  - lib/ai4r/clusterers/median_linkage.rb
67
+ - lib/ai4r/clusterers/single_linkage.rb
68
+ - lib/ai4r/clusterers/ward_linkage.rb
69
+ - lib/ai4r/clusterers/ward_linkage_hierarchical.rb
76
70
  - lib/ai4r/clusterers/weighted_average_linkage.rb
71
+ - lib/ai4r/data/data_set.rb
72
+ - lib/ai4r/data/parameterizable.rb
73
+ - lib/ai4r/data/proximity.rb
74
+ - lib/ai4r/data/statistics.rb
75
+ - lib/ai4r/experiment/classifier_evaluator.rb
76
+ - lib/ai4r/genetic_algorithm/genetic_algorithm.rb
77
+ - lib/ai4r/neural_network/backpropagation.rb
78
+ - lib/ai4r/neural_network/hopfield.rb
79
+ - lib/ai4r/som/layer.rb
80
+ - lib/ai4r/som/node.rb
81
+ - lib/ai4r/som/som.rb
82
+ - lib/ai4r/som/two_phase_layer.rb
77
83
  - README.rdoc
84
+ - test/classifiers/hyperpipes_test.rb
85
+ - test/classifiers/ib1_test.rb
86
+ - test/classifiers/id3_test.rb
87
+ - test/classifiers/multilayer_perceptron_test.rb
88
+ - test/classifiers/naive_bayes_test.rb
89
+ - test/classifiers/one_r_test.rb
90
+ - test/classifiers/prism_test.rb
91
+ - test/classifiers/simple_linear_regression_test.rb
92
+ - test/classifiers/zero_r_test.rb
93
+ - test/clusterers/average_linkage_test.rb
94
+ - test/clusterers/bisecting_k_means_test.rb
95
+ - test/clusterers/centroid_linkage_test.rb
96
+ - test/clusterers/complete_linkage_test.rb
97
+ - test/clusterers/diana_test.rb
98
+ - test/clusterers/k_means_test.rb
99
+ - test/clusterers/median_linkage_test.rb
100
+ - test/clusterers/single_linkage_test.rb
101
+ - test/clusterers/ward_linkage_hierarchical_test.rb
102
+ - test/clusterers/ward_linkage_test.rb
103
+ - test/clusterers/weighted_average_linkage_test.rb
104
+ - test/data/data_set_test.rb
105
+ - test/data/proximity_test.rb
106
+ - test/data/statistics_test.rb
107
+ - test/experiment/classifier_evaluator_test.rb
108
+ - test/genetic_algorithm/chromosome_test.rb
109
+ - test/genetic_algorithm/genetic_algorithm_test.rb
110
+ - test/neural_network/backpropagation_test.rb
111
+ - test/neural_network/hopfield_test.rb
112
+ - test/som/som_test.rb
78
113
  has_rdoc: true
79
114
  homepage: http://www.ai4r.org
80
115
  licenses: []
@@ -85,51 +120,57 @@ rdoc_options: []
85
120
  require_paths:
86
121
  - lib
87
122
  required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
88
124
  requirements:
89
125
  - - ">="
90
126
  - !ruby/object:Gem::Version
127
+ hash: 3
91
128
  segments:
92
129
  - 0
93
130
  version: "0"
94
131
  required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
95
133
  requirements:
96
134
  - - ">="
97
135
  - !ruby/object:Gem::Version
136
+ hash: 3
98
137
  segments:
99
138
  - 0
100
139
  version: "0"
101
140
  requirements: []
102
141
 
103
142
  rubyforge_project:
104
- rubygems_version: 1.3.6
143
+ rubygems_version: 1.3.7
105
144
  signing_key:
106
145
  specification_version: 3
107
146
  summary: Ruby algorithm implementations covering several Artificial intelligence fields, including Genetic algorithms, Neural Networks, machine learning, and clustering.
108
147
  test_files:
109
- - test/data/proximity_test.rb
110
- - test/data/data_set_test.rb
111
- - test/data/statistics_test.rb
112
- - test/neural_network/hopfield_test.rb
113
- - test/neural_network/backpropagation_test.rb
114
- - test/som/som_test.rb
115
- - test/classifiers/one_r_test.rb
116
- - test/classifiers/id3_test.rb
117
- - test/classifiers/ib1_test.rb
118
148
  - test/classifiers/hyperpipes_test.rb
119
- - test/classifiers/zero_r_test.rb
120
- - test/classifiers/prism_test.rb
149
+ - test/classifiers/ib1_test.rb
150
+ - test/classifiers/id3_test.rb
121
151
  - test/classifiers/multilayer_perceptron_test.rb
122
152
  - test/classifiers/naive_bayes_test.rb
123
- - test/experiment/classifier_evaluator_test.rb
124
- - test/genetic_algorithm/chromosome_test.rb
125
- - test/genetic_algorithm/genetic_algorithm_test.rb
126
- - test/clusterers/complete_linkage_test.rb
153
+ - test/classifiers/one_r_test.rb
154
+ - test/classifiers/prism_test.rb
155
+ - test/classifiers/simple_linear_regression_test.rb
156
+ - test/classifiers/zero_r_test.rb
127
157
  - test/clusterers/average_linkage_test.rb
128
- - test/clusterers/centroid_linkage_test.rb
129
158
  - test/clusterers/bisecting_k_means_test.rb
159
+ - test/clusterers/centroid_linkage_test.rb
160
+ - test/clusterers/complete_linkage_test.rb
130
161
  - test/clusterers/diana_test.rb
131
- - test/clusterers/single_linkage_test.rb
132
- - test/clusterers/weighted_average_linkage_test.rb
133
162
  - test/clusterers/k_means_test.rb
134
163
  - test/clusterers/median_linkage_test.rb
164
+ - test/clusterers/single_linkage_test.rb
165
+ - test/clusterers/ward_linkage_hierarchical_test.rb
135
166
  - test/clusterers/ward_linkage_test.rb
167
+ - test/clusterers/weighted_average_linkage_test.rb
168
+ - test/data/data_set_test.rb
169
+ - test/data/proximity_test.rb
170
+ - test/data/statistics_test.rb
171
+ - test/experiment/classifier_evaluator_test.rb
172
+ - test/genetic_algorithm/chromosome_test.rb
173
+ - test/genetic_algorithm/genetic_algorithm_test.rb
174
+ - test/neural_network/backpropagation_test.rb
175
+ - test/neural_network/hopfield_test.rb
176
+ - test/som/som_test.rb