ai4r 1.2 → 1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. data/README.rdoc +12 -25
  2. data/examples/decision_trees/id3_example.rb +6 -9
  3. data/examples/decision_trees/results.txt +2 -0
  4. data/examples/genetic_algorithm/genetic_algorithm_example.rb +11 -13
  5. data/examples/neural_network/xor_example.rb +25 -0
  6. data/lib/ai4r.rb +10 -0
  7. data/lib/ai4r/classifiers/classifier.rb +46 -0
  8. data/lib/ai4r/classifiers/id3.rb +27 -58
  9. data/lib/ai4r/classifiers/one_r.rb +19 -58
  10. data/lib/ai4r/classifiers/prism.rb +21 -57
  11. data/lib/ai4r/classifiers/zero_r.rb +16 -48
  12. data/lib/ai4r/clusterers/bisecting_k_means.rb +115 -0
  13. data/lib/ai4r/clusterers/clusterer.rb +55 -0
  14. data/lib/ai4r/clusterers/k_means.rb +164 -0
  15. data/lib/ai4r/data/data_set.rb +250 -0
  16. data/lib/ai4r/genetic_algorithm/genetic_algorithm.rb +19 -19
  17. data/lib/ai4r/neural_network/backpropagation.rb +23 -24
  18. data/site/build/site/en/broken-links.xml +2 -0
  19. data/site/build/site/en/downloads.html +200 -0
  20. data/site/build/site/en/downloads.pdf +151 -0
  21. data/site/build/site/en/forum.html +197 -0
  22. data/site/build/site/en/forum.pdf +151 -0
  23. data/site/build/site/en/geneticAlgorithms.html +591 -0
  24. data/site/build/site/en/geneticAlgorithms.pdf +934 -0
  25. data/site/build/site/en/images/ai4r-logo.png +0 -0
  26. data/site/build/site/en/images/built-with-forrest-button.png +0 -0
  27. data/site/build/site/en/images/c.png +0 -0
  28. data/site/build/site/en/images/c_wbn.png +0 -0
  29. data/site/build/site/en/images/c_wn.png +0 -0
  30. data/site/build/site/en/images/ero.gif +0 -0
  31. data/site/build/site/en/images/europe2.png +0 -0
  32. data/site/build/site/en/images/europe3.png +0 -0
  33. data/site/build/site/en/images/fitness.png +0 -0
  34. data/site/build/site/en/images/genetic_algorithms_example.png +0 -0
  35. data/site/build/site/en/images/instruction_arrow.png +0 -0
  36. data/site/build/site/en/images/jadeferret.png +0 -0
  37. data/site/build/site/en/images/my_email.png +0 -0
  38. data/site/build/site/en/images/neural_network_example.png +0 -0
  39. data/site/build/site/en/images/rubyforge.png +0 -0
  40. data/site/build/site/en/images/s.png +0 -0
  41. data/site/build/site/en/images/s_wbn.png +0 -0
  42. data/site/build/site/en/images/s_wn.png +0 -0
  43. data/site/build/site/en/images/sigmoid.png +0 -0
  44. data/site/build/site/en/images/t.png +0 -0
  45. data/site/build/site/en/images/t_wbn.png +0 -0
  46. data/site/build/site/en/images/t_wn.png +0 -0
  47. data/site/build/site/en/index.html +336 -0
  48. data/site/build/site/en/index.pdf +508 -0
  49. data/site/build/site/en/linkmap.html +263 -0
  50. data/site/build/site/en/linkmap.pdf +94 -0
  51. data/site/build/site/en/locationmap.xml +72 -0
  52. data/site/build/site/en/machineLearning.html +339 -0
  53. data/site/build/site/en/machineLearning.pdf +337 -0
  54. data/site/build/site/en/neuralNetworks.html +484 -0
  55. data/site/build/site/en/neuralNetworks.pdf +604 -0
  56. data/site/build/site/en/skin/CommonMessages_de.xml +23 -0
  57. data/site/build/site/en/skin/CommonMessages_en_US.xml +23 -0
  58. data/site/build/site/en/skin/CommonMessages_es.xml +23 -0
  59. data/site/build/site/en/skin/CommonMessages_fr.xml +23 -0
  60. data/site/build/site/en/skin/basic.css +166 -0
  61. data/site/build/site/en/skin/breadcrumbs-optimized.js +90 -0
  62. data/site/build/site/en/skin/breadcrumbs.js +237 -0
  63. data/site/build/site/en/skin/fontsize.js +166 -0
  64. data/site/build/site/en/skin/getBlank.js +40 -0
  65. data/site/build/site/en/skin/getMenu.js +45 -0
  66. data/site/build/site/en/skin/images/README.txt +1 -0
  67. data/site/build/site/en/skin/images/add.jpg +0 -0
  68. data/site/build/site/en/skin/images/built-with-forrest-button.png +0 -0
  69. data/site/build/site/en/skin/images/chapter.gif +0 -0
  70. data/site/build/site/en/skin/images/chapter_open.gif +0 -0
  71. data/site/build/site/en/skin/images/current.gif +0 -0
  72. data/site/build/site/en/skin/images/error.png +0 -0
  73. data/site/build/site/en/skin/images/external-link.gif +0 -0
  74. data/site/build/site/en/skin/images/fix.jpg +0 -0
  75. data/site/build/site/en/skin/images/forrest-credit-logo.png +0 -0
  76. data/site/build/site/en/skin/images/hack.jpg +0 -0
  77. data/site/build/site/en/skin/images/header_white_line.gif +0 -0
  78. data/site/build/site/en/skin/images/info.png +0 -0
  79. data/site/build/site/en/skin/images/instruction_arrow.png +0 -0
  80. data/site/build/site/en/skin/images/label.gif +0 -0
  81. data/site/build/site/en/skin/images/page.gif +0 -0
  82. data/site/build/site/en/skin/images/pdfdoc.gif +0 -0
  83. data/site/build/site/en/skin/images/poddoc.png +0 -0
  84. data/site/build/site/en/skin/images/printer.gif +0 -0
  85. data/site/build/site/en/skin/images/rc-b-l-15-1body-2menu-3menu.png +0 -0
  86. data/site/build/site/en/skin/images/rc-b-r-15-1body-2menu-3menu.png +0 -0
  87. data/site/build/site/en/skin/images/rc-b-r-5-1header-2tab-selected-3tab-selected.png +0 -0
  88. data/site/build/site/en/skin/images/rc-t-l-5-1header-2searchbox-3searchbox.png +0 -0
  89. data/site/build/site/en/skin/images/rc-t-l-5-1header-2tab-selected-3tab-selected.png +0 -0
  90. data/site/build/site/en/skin/images/rc-t-l-5-1header-2tab-unselected-3tab-unselected.png +0 -0
  91. data/site/build/site/en/skin/images/rc-t-r-15-1body-2menu-3menu.png +0 -0
  92. data/site/build/site/en/skin/images/rc-t-r-5-1header-2searchbox-3searchbox.png +0 -0
  93. data/site/build/site/en/skin/images/rc-t-r-5-1header-2tab-selected-3tab-selected.png +0 -0
  94. data/site/build/site/en/skin/images/rc-t-r-5-1header-2tab-unselected-3tab-unselected.png +0 -0
  95. data/site/build/site/en/skin/images/remove.jpg +0 -0
  96. data/site/build/site/en/skin/images/rss.png +0 -0
  97. data/site/build/site/en/skin/images/spacer.gif +0 -0
  98. data/site/build/site/en/skin/images/success.png +0 -0
  99. data/site/build/site/en/skin/images/txtdoc.png +0 -0
  100. data/site/build/site/en/skin/images/update.jpg +0 -0
  101. data/site/build/site/en/skin/images/valid-html401.png +0 -0
  102. data/site/build/site/en/skin/images/vcss.png +0 -0
  103. data/site/build/site/en/skin/images/warning.png +0 -0
  104. data/site/build/site/en/skin/images/xmldoc.gif +0 -0
  105. data/site/build/site/en/skin/menu.js +48 -0
  106. data/site/build/site/en/skin/note.txt +50 -0
  107. data/site/build/site/en/skin/print.css +54 -0
  108. data/site/build/site/en/skin/profile.css +163 -0
  109. data/site/build/site/en/skin/prototype.js +1257 -0
  110. data/site/build/site/en/skin/screen.css +587 -0
  111. data/site/build/site/en/svn.html +252 -0
  112. data/site/build/site/en/svn.pdf +306 -0
  113. data/site/build/site/en/wholesite.pdf +1915 -0
  114. data/site/build/tmp/brokenlinks.xml +2 -0
  115. data/site/build/tmp/cocoon-work/cache-dir/cocoon-ehcache-1.data +0 -0
  116. data/site/build/tmp/cocoon-work/cache-dir/cocoon-ehcache-1.index +0 -0
  117. data/site/build/tmp/locationmap.xml +14 -14
  118. data/site/build/tmp/output.xmap +23 -23
  119. data/site/build/tmp/pluginlist2fetchbuild.xml +144 -144
  120. data/site/build/tmp/projfilters.properties +41 -41
  121. data/site/build/webapp/WEB-INF/logs/core.log +593 -679
  122. data/site/build/webapp/WEB-INF/logs/error.log +362 -279
  123. data/site/build/webapp/WEB-INF/logs/sitemap.log +368 -1015
  124. data/site/src/documentation/content/xdocs/index.xml +18 -10
  125. data/site/src/documentation/content/xdocs/machineLearning.xml +4 -3
  126. data/site/src/documentation/content/xdocs/site.xml +2 -1
  127. data/site/src/documentation/resources/images/sigmoid.png +0 -0
  128. data/test/classifiers/id3_test.rb +45 -44
  129. data/test/classifiers/one_r_test.rb +19 -17
  130. data/test/classifiers/prism_test.rb +22 -20
  131. data/test/classifiers/zero_r_test.rb +15 -12
  132. data/test/clusterers/bisecting_k_means_test.rb +59 -0
  133. data/test/clusterers/k_means_test.rb +93 -0
  134. data/test/data/data_set_test.rb +92 -0
  135. metadata +252 -128
  136. data/lib/ai4r/classifiers/classifier_helper.rb +0 -54
  137. data/site/src/documentation/content/xdocs/forum.html +0 -9
  138. data/site/src/documentation/resources/images/Thumbs.db +0 -0
  139. data/site/src/documentation/resources/images/sub-dir/Thumbs.db +0 -0
@@ -14,7 +14,6 @@
14
14
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
15
  See the License for the specific language governing permissions and
16
16
  limitations under the License.
17
-
18
17
  -->
19
18
  <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
20
19
  <document>
@@ -39,6 +38,12 @@
39
38
  <li><a href="http://en.wikipedia.org/wiki/Association_rule_learning#Zero-attribute-rule">ZeroR</a></li>
40
39
  </ul>
41
40
  </li>
41
+ <li><a href="machineLearning.html">Data clustering</a>
42
+ <ul>
43
+ <li><a href="http://en.wikipedia.org/wiki/k_means">K-means algorithm</a></li>
44
+ <li><a href="http://en.wikipedia.org/wiki/k_means">Bisecting k-means</a></li>
45
+ </ul>
46
+ </li>
42
47
  </ul>
43
48
  </section>
44
49
 
@@ -72,22 +77,25 @@
72
77
  <p>2. Include require statements in your code:</p>
73
78
  <source>
74
79
  require "rubygems"
75
- require "ai4r/classifiers/id3"en
76
- require "ai4r/classifiers/prism"
77
- require "ai4r/classifiers/one_r"
78
- require "ai4r/classifiers/zero_r"
79
- require "ai4r/neural_network/backpropagation"
80
- require "ai4r/genetic_algorithm/genetic_algorithm"
80
+ require "ai4r"
81
81
  </source>
82
82
  </section>
83
83
 
84
84
  <section id="ContactMe">
85
85
  <title>Contact me</title>
86
86
  <p>
87
- If you have some constructive comments about this project, please do send those to me:
87
+ If you have questions or constructive comments about this project,
88
+ please post them in the
89
+ <a href="http://forum.jadeferret.com/viewforum.php?f=3" title="Ai4r Forum">forum</a>.
90
+ I get an email notification when you post, and I do my best to answer as soon as possible.
91
+ </p>
92
+ <p>
93
+ If you do not want to make it public, send it to me:
88
94
  Sergio Fierens, email address: <img src="images/my_email.png" alt="(sergio (dot) fierens (at) gmail (dot) com)"
89
- title="Sergio Fierens contact email" />
90
- </p>
95
+ title="Sergio Fierens contact email" />. But please, try to post them in the
96
+ forum. I get tons of emails and it would be great to make them
97
+ public to help everyone.
98
+ </p>
91
99
  </section>
92
100
 
93
101
  <section id="warranty">
@@ -59,7 +59,7 @@ The only information that you have is a collection of examples, provided by a ma
59
59
  and identify new posible marketing targets:</p>
60
60
  <source>
61
61
  <![CDATA[
62
- id3.to_s
62
+ id3.get_rules
63
63
  # => if age_range=='<30' then marketing_target='Y'
64
64
  elsif age_range=='[30-50)' and city=='Chicago' then marketing_target='Y'
65
65
  elsif age_range=='[30-50)' and city=='New York' then marketing_target='N'
@@ -104,7 +104,8 @@ id3.eval(['New York', '<30', 'M'])
104
104
  # => 'Y'
105
105
  ]]></source>
106
106
  <p>
107
- But instead of going through the tree every time, you can take advantage of the fact that the method "to_s" generates proper ruby code!
107
+ But instead of going through the tree every time, you can take advantage of the
108
+ fact that the method "get_rules" generates proper ruby code!
108
109
  </p>
109
110
  <source>
110
111
  <![CDATA[
@@ -113,7 +114,7 @@ But instead of going through the tree every time, you can take advantage of the
113
114
  city = 'New York'
114
115
  gender = 'M'
115
116
  marketing_target = nil
116
- eval id3.to_s
117
+ eval id3.get_rules
117
118
  puts marketing_target
118
119
  # => 'Y'
119
120
  ]]></source>
@@ -46,7 +46,8 @@ See http://forrest.apache.org/docs/linking.html for more info
46
46
  <neuralNetworks label="Neural Networks" href="neuralNetworks.html" description="Backpropagation Neural Network in ruby" />
47
47
  </practicalExamples>
48
48
  <download label="Downloads" href="downloads.html" description="ai4r - Download Files"/>
49
- <download label="Forum" href="forum.html" description="ai4r - Forum"/>
49
+ <forum label="Forum" href="http://forum.jadeferret.com/viewforum.php?f=3" description="Ai4r Forum"/>
50
+ <rdoc label="API doc (Rdoc)" href="http://ai4r.rubyforge.org/rdoc/index.html" description="Ai4 RDoc docs"/>
50
51
  <svn label="Source Code repository" href="svn.html" description="ai4r Subversion repository"/>
51
52
  <ai4r_site_pdf label="ai4r project PDF" href="wholesite.pdf"/>
52
53
  </about>
@@ -18,7 +18,7 @@ require 'test/unit'
18
18
 
19
19
  DATA_LABELS = [ 'city', 'age_range', 'gender', 'marketing_target' ]
20
20
 
21
- DATA_SET = [ ['New York', '<30', 'M', 'Y'],
21
+ DATA_ITEMS = [ ['New York', '<30', 'M', 'Y'],
22
22
  ['Chicago', '<30', 'M', 'Y'],
23
23
  ['Chicago', '<30', 'F', 'Y'],
24
24
  ['New York', '<30', 'M', 'Y'],
@@ -35,7 +35,7 @@ DATA_SET = [ ['New York', '<30', 'M', 'Y'],
35
35
  ['Chicago', '>80', 'F', 'Y']
36
36
  ]
37
37
 
38
- SPLIT_DATA_SET_BY_CITY = [ [
38
+ SPLIT_DATA_ITEMS_BY_CITY = [ [
39
39
  ["New York", "<30", "M", "Y"],
40
40
  ["New York", "<30", "M", "Y"],
41
41
  ["New York", "<30", "M", "Y"],
@@ -54,7 +54,7 @@ SPLIT_DATA_SET_BY_CITY = [ [
54
54
  ["Chicago", ">80", "F", "Y"]]
55
55
  ]
56
56
 
57
- SPLIT_DATA_SET_BY_AGE = [ [
57
+ SPLIT_DATA_ITEMS_BY_AGE = [ [
58
58
  ["New York", "<30", "M", "Y"],
59
59
  ["Chicago", "<30", "M", "Y"],
60
60
  ["Chicago", "<30", "F", "Y"],
@@ -87,6 +87,7 @@ Ai4r::Classifiers::ID3.send(:public, *Ai4r::Classifiers::ID3.protected_instance_
87
87
  Ai4r::Classifiers::ID3.send(:public, *Ai4r::Classifiers::ID3.private_instance_methods)
88
88
 
89
89
  include Ai4r::Classifiers
90
+ include Ai4r::Data
90
91
 
91
92
  class ID3Test < Test::Unit::TestCase
92
93
 
@@ -102,75 +103,75 @@ class ID3Test < Test::Unit::TestCase
102
103
  end
103
104
 
104
105
  def test_data_labels
105
- id3 = ID3.new.build(DATA_SET)
106
+ id3 = ID3.new.build(DataSet.new(:data_items =>DATA_ITEMS))
106
107
  expected_default = [ 'attribute_1', 'attribute_2', 'attribute_3', 'class_value' ]
107
- assert_equal(expected_default, id3.data_labels)
108
- id3 = ID3.new.build(DATA_SET, DATA_LABELS)
109
- assert_equal(DATA_LABELS, id3.data_labels)
108
+ assert_equal(expected_default, id3.data_set.data_labels)
109
+ id3 = ID3.new.build(DataSet.new(:data_items =>DATA_ITEMS, :data_labels => DATA_LABELS))
110
+ assert_equal(DATA_LABELS, id3.data_set.data_labels)
110
111
  end
111
112
 
112
113
  def test_domain
113
- id3 = ID3.new.build(DATA_SET, DATA_LABELS)
114
+ id3 = ID3.new.build(DataSet.new(:data_items =>DATA_ITEMS, :data_labels => DATA_LABELS))
114
115
  expected_domain = [["New York", "Chicago"], ["<30", "[30-50)", "[50-80]", ">80"], ["M", "F"], ["Y", "N"]]
115
- assert_equal expected_domain, id3.domain(DATA_SET)
116
+ assert_equal expected_domain, id3.domain(DATA_ITEMS)
116
117
  end
117
118
 
118
119
  def test_grid
119
- id3 = ID3.new.build(DATA_SET, DATA_LABELS)
120
+ id3 = ID3.new.build(DataSet.new(:data_items =>DATA_ITEMS, :data_labels => DATA_LABELS))
120
121
  expected_grid = [[3, 5], [5, 2]]
121
- domain = id3.domain(DATA_SET)
122
- assert_equal expected_grid, id3.freq_grid(0, DATA_SET, domain)
122
+ domain = id3.domain(DATA_ITEMS)
123
+ assert_equal expected_grid, id3.freq_grid(0, DATA_ITEMS, domain)
123
124
  expected_grid = [[5, 0], [2, 2], [0, 5], [1, 0]]
124
- assert_equal expected_grid, id3.freq_grid(1, DATA_SET, domain)
125
+ assert_equal expected_grid, id3.freq_grid(1, DATA_ITEMS, domain)
125
126
  end
126
127
 
127
128
  def test_entropy
128
- id3 = ID3.new.build(DATA_SET, DATA_LABELS)
129
+ id3 = ID3.new.build(DataSet.new(:data_items =>DATA_ITEMS, :data_labels => DATA_LABELS))
129
130
  expected_entropy = 0.9118
130
- domain = id3.domain(DATA_SET)
131
- freq_grid = id3.freq_grid(0, DATA_SET, domain)
132
- assert expected_entropy - id3.entropy(freq_grid, DATA_SET.length) < 0.0001
131
+ domain = id3.domain(DATA_ITEMS)
132
+ freq_grid = id3.freq_grid(0, DATA_ITEMS, domain)
133
+ assert expected_entropy - id3.entropy(freq_grid, DATA_ITEMS.length) < 0.0001
133
134
  expected_entropy = 0.2667
134
- freq_grid = id3.freq_grid(1, DATA_SET, domain)
135
- assert expected_entropy - id3.entropy(freq_grid, DATA_SET.length) < 0.0001
135
+ freq_grid = id3.freq_grid(1, DATA_ITEMS, domain)
136
+ assert expected_entropy - id3.entropy(freq_grid, DATA_ITEMS.length) < 0.0001
136
137
  expected_entropy = 0.9688
137
- freq_grid = id3.freq_grid(2, DATA_SET, domain)
138
- assert expected_entropy - id3.entropy(freq_grid, DATA_SET.length) < 0.0001
138
+ freq_grid = id3.freq_grid(2, DATA_ITEMS, domain)
139
+ assert expected_entropy - id3.entropy(freq_grid, DATA_ITEMS.length) < 0.0001
139
140
  end
140
141
 
141
142
  def test_min_entropy_index
142
- id3 = ID3.new.build(DATA_SET, DATA_LABELS)
143
- domain = id3.domain(DATA_SET)
144
- assert_equal 1, id3.min_entropy_index(DATA_SET, domain)
145
- assert_equal 0, id3.min_entropy_index(DATA_SET, domain, [1])
146
- assert_equal 2, id3.min_entropy_index(DATA_SET, domain, [1, 0])
143
+ id3 = ID3.new.build(DataSet.new(:data_items =>DATA_ITEMS, :data_labels => DATA_LABELS))
144
+ domain = id3.domain(DATA_ITEMS)
145
+ assert_equal 1, id3.min_entropy_index(DATA_ITEMS, domain)
146
+ assert_equal 0, id3.min_entropy_index(DATA_ITEMS, domain, [1])
147
+ assert_equal 2, id3.min_entropy_index(DATA_ITEMS, domain, [1, 0])
147
148
  end
148
149
 
149
150
  def test_split_data_examples
150
- id3 = ID3.new.build(DATA_SET, DATA_LABELS)
151
- domain = id3.domain(DATA_SET)
152
- res = id3.split_data_examples(DATA_SET, domain, 0)
153
- assert_equal(SPLIT_DATA_SET_BY_CITY, res)
154
- res = id3.split_data_examples(DATA_SET, domain, 1)
155
- assert_equal(SPLIT_DATA_SET_BY_AGE, res)
151
+ id3 = ID3.new.build(DataSet.new(:data_items =>DATA_ITEMS, :data_labels => DATA_LABELS))
152
+ domain = id3.domain(DATA_ITEMS)
153
+ res = id3.split_data_examples(DATA_ITEMS, domain, 0)
154
+ assert_equal(SPLIT_DATA_ITEMS_BY_CITY, res)
155
+ res = id3.split_data_examples(DATA_ITEMS, domain, 1)
156
+ assert_equal(SPLIT_DATA_ITEMS_BY_AGE, res)
156
157
  end
157
158
 
158
159
  def test_most_freq
159
- id3 = ID3.new.build(DATA_SET, DATA_LABELS)
160
- domain = id3.domain(DATA_SET)
161
- assert_equal 'Y', id3.most_freq(DATA_SET, domain)
162
- assert_equal 'Y', id3.most_freq(SPLIT_DATA_SET_BY_AGE[3], domain)
163
- assert_equal 'N', id3.most_freq(SPLIT_DATA_SET_BY_AGE[2], domain)
160
+ id3 = ID3.new.build(DataSet.new(:data_items =>DATA_ITEMS, :data_labels => DATA_LABELS))
161
+ domain = id3.domain(DATA_ITEMS)
162
+ assert_equal 'Y', id3.most_freq(DATA_ITEMS, domain)
163
+ assert_equal 'Y', id3.most_freq(SPLIT_DATA_ITEMS_BY_AGE[3], domain)
164
+ assert_equal 'N', id3.most_freq(SPLIT_DATA_ITEMS_BY_AGE[2], domain)
164
165
  end
165
166
 
166
- def test_to_s
167
+ def test_get_rules
167
168
  assert_equal [["marketing_target='N'"]], CategoryNode.new('marketing_target', 'N').get_rules
168
- id3 = ID3.new.build(DATA_SET, DATA_LABELS)
169
- assert_equal EXPECTED_RULES_STRING, id3.to_s
169
+ id3 = ID3.new.build(DataSet.new(:data_items =>DATA_ITEMS, :data_labels => DATA_LABELS))
170
+ assert_equal EXPECTED_RULES_STRING, id3.get_rules
170
171
  end
171
172
 
172
173
  def test_eval
173
- id3 = ID3.new.build(DATA_SET, DATA_LABELS)
174
+ id3 = ID3.new.build(DataSet.new(:data_items =>DATA_ITEMS, :data_labels => DATA_LABELS))
174
175
  #if age_range='<30' then marketing_target='Y'
175
176
  assert_equal 'Y', id3.eval(['New York', '<30', 'F'])
176
177
  assert_equal 'Y', id3.eval(['Chicago', '<30', 'M'])
@@ -189,16 +190,16 @@ class ID3Test < Test::Unit::TestCase
189
190
  end
190
191
 
191
192
  def test_rules_eval
192
- id3 = ID3.new.build(DATA_SET, DATA_LABELS)
193
+ id3 = ID3.new.build(DataSet.new(:data_items =>DATA_ITEMS, :data_labels => DATA_LABELS))
193
194
  #if age_range='<30' then marketing_target='Y'
194
195
  age_range = '<30'
195
196
  marketing_target = nil
196
- eval id3.to_s
197
+ eval id3.get_rules
197
198
  assert_equal 'Y', marketing_target
198
199
  #if age_range='[30-50)' and city='New York' then marketing_target='N'
199
200
  age_range='[30-50)'
200
201
  city='New York'
201
- eval id3.to_s
202
+ eval id3.get_rules
202
203
  assert_equal 'N', marketing_target
203
204
  end
204
205
  end
@@ -6,6 +6,7 @@ require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/one_r'
6
6
  class OneRTest < Test::Unit::TestCase
7
7
 
8
8
  include Ai4r::Classifiers
9
+ include Ai4r::Data
9
10
 
10
11
  @@data_examples = [ ['New York', '<30', 'M', 'Y'],
11
12
  ['Chicago', '<30', 'M', 'Y'],
@@ -19,42 +20,43 @@ class OneRTest < Test::Unit::TestCase
19
20
  @@data_labels = [ 'city', 'age_range', 'gender', 'marketing_target' ]
20
21
 
21
22
  def test_build
22
- assert_raise(ArgumentError) { OneR.new.build(nil) }
23
- assert_raise(ArgumentError) { OneR.new.build([]) }
24
- classifier = OneR.new.build(@@data_examples)
25
- assert_not_nil(classifier.data_labels)
23
+ assert_raise(ArgumentError) { OneR.new.build(DataSet.new) }
24
+ classifier = OneR.new.build(DataSet.new(:data_items => @@data_examples))
25
+ assert_not_nil(classifier.data_set.data_labels)
26
26
  assert_not_nil(classifier.rule)
27
- assert_equal("attribute_1", classifier.data_labels.first)
28
- assert_equal("class_value", classifier.data_labels.last)
29
- classifier = OneR.new.build(@@data_examples, @@data_labels)
30
- assert_not_nil(classifier.data_labels)
27
+ assert_equal("attribute_1", classifier.data_set.data_labels.first)
28
+ assert_equal("class_value", classifier.data_set.data_labels.last)
29
+ classifier = OneR.new.build(DataSet.new(:data_items => @@data_examples,
30
+ :data_labels => @@data_labels))
31
+ assert_not_nil(classifier.data_set.data_labels)
31
32
  assert_not_nil(classifier.rule)
32
- assert_equal("city", classifier.data_labels.first)
33
- assert_equal("marketing_target", classifier.data_labels.last)
33
+ assert_equal("city", classifier.data_set.data_labels.first)
34
+ assert_equal("marketing_target", classifier.data_set.data_labels.last)
34
35
  assert_equal(1, classifier.rule[:attr_index])
35
36
  end
36
37
 
37
38
  def test_eval
38
- classifier = OneR.new.build(@@data_examples)
39
+ classifier = OneR.new.build(DataSet.new(:data_items => @@data_examples))
39
40
  assert_equal("Y", classifier.eval(['New York', '<30', 'M']))
40
41
  assert_equal("N", classifier.eval(['New York', '[30-50)', 'M']))
41
42
  assert_equal("N", classifier.eval(['Chicago', '[50-80]', 'M']))
42
43
  end
43
44
 
44
- def test_to_s
45
- classifier = OneR.new.build(@@data_examples, @@data_labels)
45
+ def test_get_rules
46
+ classifier = OneR.new.build(DataSet.new(:data_items => @@data_examples,
47
+ :data_labels => @@data_labels))
46
48
  marketing_target = nil
47
49
  age_range = nil
48
- eval(classifier.to_s)
50
+ eval(classifier.get_rules)
49
51
  assert_nil(marketing_target)
50
52
  age_range = '<30'
51
- eval(classifier.to_s)
53
+ eval(classifier.get_rules)
52
54
  assert_equal("Y", marketing_target)
53
55
  age_range = '[30-50)'
54
- eval(classifier.to_s)
56
+ eval(classifier.get_rules)
55
57
  assert_equal("N", marketing_target)
56
58
  age_range = '[50-80]'
57
- eval(classifier.to_s)
59
+ eval(classifier.get_rules)
58
60
  assert_equal("N", marketing_target)
59
61
  end
60
62
 
@@ -9,6 +9,7 @@ Ai4r::Classifiers::Prism.send(:public, *Ai4r::Classifiers::Prism.private_instanc
9
9
  class PrismTest < Test::Unit::TestCase
10
10
 
11
11
  include Ai4r::Classifiers
12
+ include Ai4r::Data
12
13
 
13
14
  @@data_examples = [ ['New York', '<30', 'M', 'Y'],
14
15
  ['Chicago', '<30', 'M', 'Y'],
@@ -30,52 +31,53 @@ class PrismTest < Test::Unit::TestCase
30
31
  @@data_labels = [ 'city', 'age_range', 'gender', 'marketing_target' ]
31
32
 
32
33
  def test_build
33
- assert_raise(ArgumentError) { Prism.new.build(nil) }
34
- assert_raise(ArgumentError) { Prism.new.build([]) }
35
- classifier = Prism.new.build(@@data_examples)
36
- assert_not_nil(classifier.data_labels)
34
+ assert_raise(ArgumentError) { Prism.new.build(DataSet.new) }
35
+ classifier = Prism.new.build(DataSet.new(:data_items=>@@data_examples))
36
+ assert_not_nil(classifier.data_set.data_labels)
37
37
  assert_not_nil(classifier.rules)
38
- assert_equal("attribute_1", classifier.data_labels.first)
39
- assert_equal("class_value", classifier.data_labels.last)
40
- classifier = Prism.new.build(@@data_examples, @@data_labels)
41
- assert_not_nil(classifier.data_labels)
38
+ assert_equal("attribute_1", classifier.data_set.data_labels.first)
39
+ assert_equal("class_value", classifier.data_set.data_labels.last)
40
+ classifier = Prism.new.build(DataSet.new(:data_items => @@data_examples,
41
+ :data_labels => @@data_labels))
42
+ assert_not_nil(classifier.data_set.data_labels)
42
43
  assert_not_nil(classifier.rules)
43
- assert_equal("city", classifier.data_labels.first)
44
- assert_equal("marketing_target", classifier.data_labels.last)
44
+ assert_equal("city", classifier.data_set.data_labels.first)
45
+ assert_equal("marketing_target", classifier.data_set.data_labels.last)
45
46
  assert !classifier.rules.empty?
46
47
  end
47
48
 
48
49
  def test_eval
49
- classifier = Prism.new.build(@@data_examples)
50
+ classifier = Prism.new.build(DataSet.new(:data_items=>@@data_examples))
50
51
  @@data_examples.each do |data|
51
52
  assert_equal(data.last, classifier.eval(data[0...-1]))
52
53
  end
53
54
  end
54
55
 
55
- def test_to_s
56
- classifier = Prism.new.build(@@data_examples, @@data_labels)
56
+ def test_get_rules
57
+ classifier = Prism.new.build(DataSet.new(:data_items => @@data_examples,
58
+ :data_labels => @@data_labels))
57
59
  marketing_target = nil
58
60
  age_range = nil
59
61
  city = 'Chicago'
60
- eval(classifier.to_s)
62
+ eval(classifier.get_rules)
61
63
  age_range = '<30'
62
- eval(classifier.to_s)
64
+ eval(classifier.get_rules)
63
65
  assert_equal("Y", marketing_target)
64
66
  age_range = '[30-50)'
65
- eval(classifier.to_s)
67
+ eval(classifier.get_rules)
66
68
  assert_equal("Y", marketing_target)
67
69
  age_range = '[30-50)'
68
70
  city = 'New York'
69
- eval(classifier.to_s)
71
+ eval(classifier.get_rules)
70
72
  assert_equal("N", marketing_target)
71
73
  age_range = '[50-80]'
72
- eval(classifier.to_s)
74
+ eval(classifier.get_rules)
73
75
  assert_equal("N", marketing_target)
74
76
  end
75
77
 
76
78
  def test_matches_conditions
77
- classifier = Prism.new
78
- classifier.data_labels = @@data_labels
79
+ classifier = Prism.new.build(DataSet.new(:data_labels => @@data_labels,
80
+ :data_items => @@data_examples))
79
81
  assert classifier.matches_conditions(['New York', '<30', 'M', 'Y'], {"age_range" => "<30"})
80
82
  assert !classifier.matches_conditions(['New York', '<30', 'M', 'Y'], {"age_range" => "[50-80]"})
81
83
  end
@@ -2,10 +2,12 @@ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
2
 
3
3
  require 'test/unit'
4
4
  require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/zero_r'
5
+ require File.dirname(__FILE__) + '/../../lib/ai4r/data/data_set'
5
6
 
6
7
  class ZeroRTest < Test::Unit::TestCase
7
8
 
8
9
  include Ai4r::Classifiers
10
+ include Ai4r::Data
9
11
 
10
12
  @@data_examples = [ ['New York', '<30', 'M', 'Y'],
11
13
  ['Chicago', '<30', 'M', 'Y'],
@@ -19,28 +21,29 @@ class ZeroRTest < Test::Unit::TestCase
19
21
  @@data_labels = [ 'city', 'age_range', 'gender', 'marketing_target' ]
20
22
 
21
23
  def test_build
22
- assert_raise(ArgumentError) { ZeroR.new.build(nil) }
23
- assert_raise(ArgumentError) { ZeroR.new.build([]) }
24
- classifier = ZeroR.new.build(@@data_examples)
24
+ assert_raise(ArgumentError) { ZeroR.new.build(DataSet.new) }
25
+ classifier = ZeroR.new.build(DataSet.new(:data_items => @@data_examples))
25
26
  assert_equal("Y", classifier.class_value)
26
- assert_equal("attribute_1", classifier.data_labels.first)
27
- assert_equal("class_value", classifier.data_labels.last)
28
- classifier = ZeroR.new.build(@@data_examples, @@data_labels)
27
+ assert_equal("attribute_1", classifier.data_set.data_labels.first)
28
+ assert_equal("class_value", classifier.data_set.data_labels.last)
29
+ classifier = ZeroR.new.build(DataSet.new(:data_items => @@data_examples,
30
+ :data_labels => @@data_labels))
29
31
  assert_equal("Y", classifier.class_value)
30
- assert_equal("city", classifier.data_labels.first)
31
- assert_equal("marketing_target", classifier.data_labels.last)
32
+ assert_equal("city", classifier.data_set.data_labels.first)
33
+ assert_equal("marketing_target", classifier.data_set.data_labels.last)
32
34
  end
33
35
 
34
36
  def test_eval
35
- classifier = ZeroR.new.build(@@data_examples)
37
+ classifier = ZeroR.new.build(DataSet.new(:data_items => @@data_examples))
36
38
  assert_equal('Y', classifier.eval(@@data_examples.first) )
37
39
  assert_equal('Y', classifier.eval(@@data_examples.last) )
38
40
  end
39
41
 
40
- def test_to_s
41
- classifier = ZeroR.new.build(@@data_examples, @@data_labels)
42
+ def test_get_rules
43
+ classifier = ZeroR.new.build(DataSet.new(:data_items => @@data_examples,
44
+ :data_labels => @@data_labels))
42
45
  marketing_target = nil
43
- eval(classifier.to_s)
46
+ eval(classifier.get_rules)
44
47
  assert_equal('Y', marketing_target)
45
48
  end
46
49