ai4ruby 1.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/README.rdoc +47 -0
  2. data/examples/classifiers/id3_data.csv +121 -0
  3. data/examples/classifiers/id3_example.rb +29 -0
  4. data/examples/classifiers/naive_bayes_data.csv +11 -0
  5. data/examples/classifiers/naive_bayes_example.rb +16 -0
  6. data/examples/classifiers/results.txt +31 -0
  7. data/examples/genetic_algorithm/genetic_algorithm_example.rb +37 -0
  8. data/examples/genetic_algorithm/travel_cost.csv +16 -0
  9. data/examples/neural_network/backpropagation_example.rb +67 -0
  10. data/examples/neural_network/patterns_with_base_noise.rb +68 -0
  11. data/examples/neural_network/patterns_with_noise.rb +66 -0
  12. data/examples/neural_network/training_patterns.rb +68 -0
  13. data/examples/neural_network/xor_example.rb +35 -0
  14. data/examples/som/som_data.rb +156 -0
  15. data/examples/som/som_multi_node_example.rb +22 -0
  16. data/examples/som/som_single_example.rb +24 -0
  17. data/lib/ai4r.rb +33 -0
  18. data/lib/ai4r/classifiers/classifier.rb +62 -0
  19. data/lib/ai4r/classifiers/hyperpipes.rb +118 -0
  20. data/lib/ai4r/classifiers/ib1.rb +121 -0
  21. data/lib/ai4r/classifiers/id3.rb +326 -0
  22. data/lib/ai4r/classifiers/multilayer_perceptron.rb +135 -0
  23. data/lib/ai4r/classifiers/naive_bayes.rb +259 -0
  24. data/lib/ai4r/classifiers/one_r.rb +110 -0
  25. data/lib/ai4r/classifiers/prism.rb +197 -0
  26. data/lib/ai4r/classifiers/zero_r.rb +73 -0
  27. data/lib/ai4r/clusterers/average_linkage.rb +59 -0
  28. data/lib/ai4r/clusterers/bisecting_k_means.rb +93 -0
  29. data/lib/ai4r/clusterers/centroid_linkage.rb +66 -0
  30. data/lib/ai4r/clusterers/clusterer.rb +61 -0
  31. data/lib/ai4r/clusterers/complete_linkage.rb +67 -0
  32. data/lib/ai4r/clusterers/diana.rb +139 -0
  33. data/lib/ai4r/clusterers/k_means.rb +126 -0
  34. data/lib/ai4r/clusterers/median_linkage.rb +61 -0
  35. data/lib/ai4r/clusterers/single_linkage.rb +194 -0
  36. data/lib/ai4r/clusterers/ward_linkage.rb +64 -0
  37. data/lib/ai4r/clusterers/ward_linkage_hierarchical.rb +31 -0
  38. data/lib/ai4r/clusterers/weighted_average_linkage.rb +61 -0
  39. data/lib/ai4r/data/data_set.rb +266 -0
  40. data/lib/ai4r/data/parameterizable.rb +64 -0
  41. data/lib/ai4r/data/proximity.rb +100 -0
  42. data/lib/ai4r/data/statistics.rb +77 -0
  43. data/lib/ai4r/experiment/classifier_evaluator.rb +95 -0
  44. data/lib/ai4r/genetic_algorithm/genetic_algorithm.rb +270 -0
  45. data/lib/ai4r/neural_network/backpropagation.rb +326 -0
  46. data/lib/ai4r/neural_network/hopfield.rb +149 -0
  47. data/lib/ai4r/som/layer.rb +68 -0
  48. data/lib/ai4r/som/node.rb +96 -0
  49. data/lib/ai4r/som/som.rb +155 -0
  50. data/lib/ai4r/som/two_phase_layer.rb +90 -0
  51. data/test/classifiers/hyperpipes_test.rb +84 -0
  52. data/test/classifiers/ib1_test.rb +78 -0
  53. data/test/classifiers/id3_test.rb +208 -0
  54. data/test/classifiers/multilayer_perceptron_test.rb +79 -0
  55. data/test/classifiers/naive_bayes_test.rb +43 -0
  56. data/test/classifiers/one_r_test.rb +62 -0
  57. data/test/classifiers/prism_test.rb +85 -0
  58. data/test/classifiers/zero_r_test.rb +49 -0
  59. data/test/clusterers/average_linkage_test.rb +51 -0
  60. data/test/clusterers/bisecting_k_means_test.rb +66 -0
  61. data/test/clusterers/centroid_linkage_test.rb +53 -0
  62. data/test/clusterers/complete_linkage_test.rb +57 -0
  63. data/test/clusterers/diana_test.rb +69 -0
  64. data/test/clusterers/k_means_test.rb +100 -0
  65. data/test/clusterers/median_linkage_test.rb +53 -0
  66. data/test/clusterers/single_linkage_test.rb +122 -0
  67. data/test/clusterers/ward_linkage_hierarchical_test.rb +61 -0
  68. data/test/clusterers/ward_linkage_test.rb +53 -0
  69. data/test/clusterers/weighted_average_linkage_test.rb +53 -0
  70. data/test/data/data_set_test.rb +96 -0
  71. data/test/data/proximity_test.rb +81 -0
  72. data/test/data/statistics_test.rb +65 -0
  73. data/test/experiment/classifier_evaluator_test.rb +76 -0
  74. data/test/genetic_algorithm/chromosome_test.rb +58 -0
  75. data/test/genetic_algorithm/genetic_algorithm_test.rb +81 -0
  76. data/test/neural_network/backpropagation_test.rb +82 -0
  77. data/test/neural_network/hopfield_test.rb +72 -0
  78. data/test/som/som_test.rb +97 -0
  79. metadata +168 -0
@@ -0,0 +1,66 @@
1
+ # Author:: Sergio Fierens
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
+ TRIANGLE_WITH_NOISE = [
11
+ [ 1, 0, 0, 0, 0, 0, 0, 1, 5, 0, 0, 1, 0, 0, 0, 0],
12
+ [ 0, 0, 0, 0, 3, 0, 1, 9, 9, 1, 0, 0, 0, 0, 3, 0],
13
+ [ 0, 3, 0, 0, 0, 0, 5, 1, 5, 3, 0, 0, 0, 0, 0, 7],
14
+ [ 0, 0, 0, 7, 0, 1, 9, 1, 1, 9, 1, 0, 0, 0, 3, 0],
15
+ [ 0, 0, 0, 0, 0, 3, 5, 0, 3, 5, 5, 0, 0, 0, 0, 0],
16
+ [ 0, 1, 0, 0, 1, 9, 1, 0, 1, 1, 9, 1, 0, 0, 0, 0],
17
+ [ 1, 0, 0, 0, 5, 5, 0, 0, 0, 0, 5, 5, 7, 0, 0, 3],
18
+ [ 0, 0, 3, 3, 9, 1, 0, 0, 1, 0, 1, 9, 1, 0, 0, 0],
19
+ [ 0, 0, 0, 5, 5, 0, 3, 7, 0, 0, 0, 5, 5, 0, 0, 0],
20
+ [ 0, 0, 1, 9, 1, 0, 0, 0, 0, 0, 0, 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, 0, 0, 5, 5, 0, 0, 0, 10, 0, 0, 0],
65
+ [ 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0]
66
+ ]
@@ -0,0 +1,68 @@
1
+ # Author:: Sergio Fierens
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
+
11
+ TRIANGLE = [
12
+ [ 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0],
13
+ [ 0, 0, 0, 0, 0, 0, 1, 9, 9, 1, 0, 0, 0, 0, 0, 0],
14
+ [ 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 0, 0, 0, 0, 0, 0],
15
+ [ 0, 0, 0, 0, 0, 1, 9, 1, 1, 9, 1, 0, 0, 0, 0, 0],
16
+ [ 0, 0, 0, 0, 0, 5, 5, 0, 0, 5, 5, 0, 0, 0, 0, 0],
17
+ [ 0, 0, 0, 0, 1, 9, 1, 0, 0, 1, 9, 1, 0, 0, 0, 0],
18
+ [ 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0],
19
+ [ 0, 0, 0, 1, 9, 1, 0, 0, 0, 0, 1, 9, 1, 0, 0, 0],
20
+ [ 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0],
21
+ [ 0, 0, 1, 9, 1, 0, 0, 0, 0, 0, 0, 1, 9, 1, 0, 0],
22
+ [ 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0],
23
+ [ 0, 1, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 9, 1, 0],
24
+ [ 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 0],
25
+ [ 1, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 9, 1],
26
+ [ 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5],
27
+ [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
28
+ ]
29
+
30
+ SQUARE = [
31
+ [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
32
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
33
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
34
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
35
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
36
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
37
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
38
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
39
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
40
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
41
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
42
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
43
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
44
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
45
+ [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10],
46
+ [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10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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 File.dirname(__FILE__) + '/../../lib/ai4r/neural_network/backpropagation'
11
+ require 'benchmark'
12
+
13
+ times = Benchmark.measure do
14
+
15
+ srand 1
16
+
17
+ net = Ai4r::NeuralNetwork::Backpropagation.new([2, 2, 1])
18
+
19
+ puts "Training the network, please wait."
20
+ 2001.times do |i|
21
+ net.train([0,0], [0])
22
+ net.train([0,1], [1])
23
+ net.train([1,0], [1])
24
+ error = net.train([1,1], [0])
25
+ puts "Error after iteration #{i}:\t#{error}" if i%200 == 0
26
+ end
27
+
28
+ puts "Test data"
29
+ puts "[0,0] = > #{net.eval([0,0]).inspect}"
30
+ puts "[0,1] = > #{net.eval([0,1]).inspect}"
31
+ puts "[1,0] = > #{net.eval([1,0]).inspect}"
32
+ puts "[1,1] = > #{net.eval([1,1]).inspect}"
33
+ end
34
+
35
+ puts "Elapsed time: #{times}"
@@ -0,0 +1,156 @@
1
+ # data is from the iris dataset (http://archive.ics.uci.edu/ml/datasets/Iris)
2
+ # it is the full dataset, removing the last column
3
+ # website provides additional information on the dataset itself (attributes, class distribution, etc)
4
+
5
+ SOM_DATA = [
6
+ [5.1, 3.5, 1.4, 0.2],
7
+ [4.9, 3.0, 1.4, 0.2],
8
+ [4.7, 3.2, 1.3, 0.2],
9
+ [4.6, 3.1, 1.5, 0.2],
10
+ [5.0, 3.6, 1.4, 0.2],
11
+ [5.4, 3.9, 1.7, 0.4],
12
+ [4.6, 3.4, 1.4, 0.3],
13
+ [5.0, 3.4, 1.5, 0.2],
14
+ [4.4, 2.9, 1.4, 0.2],
15
+ [4.9, 3.1, 1.5, 0.1],
16
+ [5.4, 3.7, 1.5, 0.2],
17
+ [4.8, 3.4, 1.6, 0.2],
18
+ [4.8, 3.0, 1.4, 0.1],
19
+ [4.3, 3.0, 1.1, 0.1],
20
+ [5.8, 4.0, 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, 4.2, 1.3],
103
+ [6.2, 2.9, 4.3, 1.3],
104
+ [5.1, 2.5, 3.0, 1.1],
105
+ [5.7, 2.8, 4.1, 1.3],
106
+ [6.3, 3.3, 6.0, 2.5],
107
+ [5.8, 2.7, 5.1, 1.9],
108
+ [7.1, 3.0, 5.9, 2.1],
109
+ [6.3, 2.9, 5.6, 1.8],
110
+ [6.5, 3.0, 5.8, 2.2],
111
+ [7.6, 3.0, 6.6, 2.1],
112
+ [4.9, 2.5, 4.5, 1.7],
113
+ [7.3, 2.9, 6.3, 1.8],
114
+ [6.7, 2.5, 5.8, 1.8],
115
+ [7.2, 3.6, 6.1, 2.5],
116
+ [6.5, 3.2, 5.1, 2.0],
117
+ [6.4, 2.7, 5.3, 1.9],
118
+ [6.8, 3.0, 5.5, 2.1],
119
+ [5.7, 2.5, 5.0, 2.0],
120
+ [5.8, 2.8, 5.1, 2.4],
121
+ [6.4, 3.2, 5.3, 2.3],
122
+ [6.5, 3.0, 5.5, 1.8],
123
+ [7.7, 3.8, 6.7, 2.2],
124
+ [7.7, 2.6, 6.9, 2.3],
125
+ [6.0, 2.2, 5.0, 1.5],
126
+ [6.9, 3.2, 5.7, 2.3],
127
+ [5.6, 2.8, 4.9, 2.0],
128
+ [7.7, 2.8, 6.7, 2.0],
129
+ [6.3, 2.7, 4.9, 1.8],
130
+ [6.7, 3.3, 5.7, 2.1],
131
+ [7.2, 3.2, 6.0, 1.8],
132
+ [6.2, 2.8, 4.8, 1.8],
133
+ [6.1, 3.0, 4.9, 1.8],
134
+ [6.4, 2.8, 5.6, 2.1],
135
+ [7.2, 3.0, 5.8, 1.6],
136
+ [7.4, 2.8, 6.1, 1.9],
137
+ [7.9, 3.8, 6.4, 2.0],
138
+ [6.4, 2.8, 5.6, 2.2],
139
+ [6.3, 2.8, 5.1, 1.5],
140
+ [6.1, 2.6, 5.6, 1.4],
141
+ [7.7, 3.0, 6.1, 2.3],
142
+ [6.3, 3.4, 5.6, 2.4],
143
+ [6.4, 3.1, 5.5, 1.8],
144
+ [6.0, 3.0, 4.8, 1.8],
145
+ [6.9, 3.1, 5.4, 2.1],
146
+ [6.7, 3.1, 5.6, 2.4],
147
+ [6.9, 3.1, 5.1, 2.3],
148
+ [5.8, 2.7, 5.1, 1.9],
149
+ [6.8, 3.2, 5.9, 2.3],
150
+ [6.7, 3.3, 5.7, 2.5],
151
+ [6.7, 3.0, 5.2, 2.3],
152
+ [6.3, 2.5, 5.0, 1.9],
153
+ [6.5, 3.0, 5.2, 2.0],
154
+ [6.2, 3.4, 5.4, 2.3],
155
+ [5.9, 3.0, 5.1, 1.8],
156
+ ]
@@ -0,0 +1,22 @@
1
+ # this example shows the impact of the size of a som on the global error distance
2
+ require File.dirname(__FILE__) + '/../../lib/ai4r/som/som'
3
+ require File.dirname(__FILE__) + '/som_data'
4
+ require 'benchmark'
5
+
6
+ 10.times do |t|
7
+ t += 3 # minimum number of nodes
8
+
9
+ puts "Nodes: #{t}"
10
+ som = Ai4r::Som::Som.new 4, 8, Ai4r::Som::TwoPhaseLayer.new(t)
11
+ som.initiate_map
12
+
13
+ puts "global error distance: #{som.global_error(SOM_DATA)}"
14
+ puts "\ntraining the som\n"
15
+
16
+ times = Benchmark.measure do
17
+ som.train SOM_DATA
18
+ end
19
+
20
+ puts "Elapsed time for training: #{times}"
21
+ puts "global error distance: #{som.global_error(SOM_DATA)}\n\n"
22
+ end
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '/../../lib/ai4r/som/som'
2
+ require File.dirname(__FILE__) + '/som_data'
3
+ require 'benchmark'
4
+
5
+ som = Ai4r::Som::Som.new 4, 8, Ai4r::Som::TwoPhaseLayer.new(10)
6
+ som.initiate_map
7
+
8
+ som.nodes.each do |node|
9
+ p node.weights
10
+ end
11
+
12
+ puts "global error distance: #{som.global_error(SOM_DATA)}"
13
+ puts "\ntraining the som\n"
14
+
15
+ times = Benchmark.measure do
16
+ som.train SOM_DATA
17
+ end
18
+
19
+ som.nodes.each do |node|
20
+ p node.weights
21
+ end
22
+
23
+ puts "Elapsed time for training: #{times}"
24
+ puts "global error distance: #{som.global_error(SOM_DATA)}\n\n"
@@ -0,0 +1,33 @@
1
+ # Data
2
+ require File.dirname(__FILE__) + "/ai4r/data/data_set"
3
+ require File.dirname(__FILE__) + "/ai4r/data/statistics"
4
+ require File.dirname(__FILE__) + "/ai4r/data/proximity"
5
+ require File.dirname(__FILE__) + "/ai4r/data/parameterizable"
6
+ # Clusterers
7
+ require File.dirname(__FILE__) + "/ai4r/clusterers/clusterer"
8
+ require File.dirname(__FILE__) + "/ai4r/clusterers/k_means"
9
+ require File.dirname(__FILE__) + "/ai4r/clusterers/bisecting_k_means"
10
+ require File.dirname(__FILE__) + "/ai4r/clusterers/single_linkage"
11
+ require File.dirname(__FILE__) + "/ai4r/clusterers/complete_linkage"
12
+ require File.dirname(__FILE__) + "/ai4r/clusterers/average_linkage"
13
+ require File.dirname(__FILE__) + "/ai4r/clusterers/weighted_average_linkage"
14
+ require File.dirname(__FILE__) + "/ai4r/clusterers/centroid_linkage"
15
+ require File.dirname(__FILE__) + "/ai4r/clusterers/median_linkage"
16
+ require File.dirname(__FILE__) + "/ai4r/clusterers/ward_linkage"
17
+ require File.dirname(__FILE__) + "/ai4r/clusterers/diana"
18
+ # Classifiers
19
+ require File.dirname(__FILE__) + "/ai4r/classifiers/classifier"
20
+ require File.dirname(__FILE__) + "/ai4r/classifiers/id3"
21
+ require File.dirname(__FILE__) + "/ai4r/classifiers/prism"
22
+ require File.dirname(__FILE__) + "/ai4r/classifiers/one_r"
23
+ require File.dirname(__FILE__) + "/ai4r/classifiers/zero_r"
24
+ require File.dirname(__FILE__) + "/ai4r/classifiers/hyperpipes"
25
+ require File.dirname(__FILE__) + "/ai4r/classifiers/naive_bayes"
26
+ require File.dirname(__FILE__) + "/ai4r/classifiers/ib1"
27
+ # Neural networks
28
+ require File.dirname(__FILE__) + "/ai4r/neural_network/backpropagation"
29
+ require File.dirname(__FILE__) + "/ai4r/neural_network/hopfield"
30
+ # Genetic Algorithms
31
+ require File.dirname(__FILE__) + "/ai4r/genetic_algorithm/genetic_algorithm"
32
+ # SOM
33
+ require File.dirname(__FILE__) + "/ai4r/som/som"
@@ -0,0 +1,62 @@
1
+ # Author:: Sergio Fierens
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 File.dirname(__FILE__) + '/../data/parameterizable'
11
+
12
+ module Ai4r
13
+ module Classifiers
14
+
15
+ # This class defines a common API for classifiers.
16
+ # All methods in this class must be implemented in subclasses.
17
+ class Classifier
18
+
19
+ include Ai4r::Data::Parameterizable
20
+
21
+ # Build a new classifier, using data examples found in data_set.
22
+ # The last attribute of each item is considered as the
23
+ # item class.
24
+ def build(data_set)
25
+ raise NotImplementedError
26
+ end
27
+
28
+ # You can evaluate new data, predicting its class.
29
+ # e.g.
30
+ # classifier.eval(['New York', '<30', 'F']) # => 'Y'
31
+ def eval(data)
32
+ raise NotImplementedError
33
+ end
34
+
35
+ # This method returns the generated rules in ruby code.
36
+ # e.g.
37
+ #
38
+ # classifier.get_rules
39
+ # # => if age_range=='<30' then marketing_target='Y'
40
+ # elsif age_range=='[30-50)' and city=='Chicago' then marketing_target='Y'
41
+ # elsif age_range=='[30-50)' and city=='New York' then marketing_target='N'
42
+ # elsif age_range=='[50-80]' then marketing_target='N'
43
+ # elsif age_range=='>80' then marketing_target='Y'
44
+ # else raise 'There was not enough information during training to do a proper induction for this data element' end
45
+ #
46
+ # It is a nice way to inspect induction results, and also to execute them:
47
+ # age_range = '<30'
48
+ # city='New York'
49
+ # marketing_target = nil
50
+ # eval classifier.get_rules
51
+ # puts marketing_target
52
+ # # => 'Y'
53
+ #
54
+ # Note, however, that not all classifiers are able to produce rules.
55
+ # This method is not implemented in such classifiers.
56
+ def get_rules
57
+ raise NotImplementedError
58
+ end
59
+
60
+ end
61
+ end
62
+ end