ai4r 1.4 → 1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/README.rdoc +24 -3
  2. data/examples/decision_trees/id3_example.rb +1 -1
  3. data/examples/genetic_algorithm/genetic_algorithm_example.rb +1 -1
  4. data/lib/ai4r.rb +11 -0
  5. data/lib/ai4r/classifiers/classifier.rb +2 -0
  6. data/lib/ai4r/classifiers/id3.rb +3 -2
  7. data/lib/ai4r/classifiers/multilayer_perceptron.rb +135 -0
  8. data/lib/ai4r/classifiers/one_r.rb +2 -1
  9. data/lib/ai4r/classifiers/prism.rb +2 -1
  10. data/lib/ai4r/classifiers/zero_r.rb +2 -1
  11. data/lib/ai4r/clusterers/average_linkage.rb +60 -0
  12. data/lib/ai4r/clusterers/bisecting_k_means.rb +17 -39
  13. data/lib/ai4r/clusterers/clusterer.rb +25 -0
  14. data/lib/ai4r/clusterers/complete_linkage.rb +62 -0
  15. data/lib/ai4r/clusterers/k_means.rb +18 -25
  16. data/lib/ai4r/clusterers/single_linkage.rb +179 -0
  17. data/lib/ai4r/data/data_set.rb +33 -41
  18. data/lib/ai4r/data/proximity.rb +82 -0
  19. data/lib/ai4r/data/statistics.rb +77 -0
  20. data/lib/ai4r/experiment/classifier_evaluator.rb +95 -0
  21. data/lib/ai4r/genetic_algorithm/genetic_algorithm.rb +2 -4
  22. data/site/build/site/en/build/tmp/build-info.xml +5 -0
  23. data/site/build/site/en/build/tmp/plugins-1.xml +212 -0
  24. data/site/build/site/en/build/tmp/plugins-2.xml +252 -0
  25. data/site/build/site/en/build/tmp/projfilters.properties +41 -0
  26. data/site/build/site/en/downloads.html +1 -1
  27. data/site/build/site/en/geneticAlgorithms.html +1 -1
  28. data/site/build/site/en/index.html +44 -7
  29. data/site/build/site/en/index.pdf +278 -155
  30. data/site/build/site/en/linkmap.html +2 -2
  31. data/site/build/site/en/linkmap.pdf +12 -12
  32. data/site/build/site/en/machineLearning.html +1 -1
  33. data/site/build/site/en/neuralNetworks.html +1 -1
  34. data/site/build/site/en/sourceCode.html +244 -0
  35. data/site/build/site/en/sourceCode.pdf +278 -0
  36. data/site/build/site/en/svn.html +34 -42
  37. data/site/build/site/en/svn.pdf +86 -114
  38. data/site/build/tmp/cocoon-work/cache-dir/cocoon-ehcache-1.data +0 -0
  39. data/site/build/tmp/cocoon-work/cache-dir/cocoon-ehcache-1.index +0 -0
  40. data/site/build/tmp/projfilters.properties +1 -1
  41. data/site/build/webapp/WEB-INF/logs/core.log +628 -629
  42. data/site/build/webapp/WEB-INF/logs/error.log +213 -213
  43. data/site/src/documentation/content/xdocs/index.xml +20 -1
  44. data/site/src/documentation/content/xdocs/site.xml +1 -1
  45. data/site/src/documentation/content/xdocs/sourceCode.xml +43 -0
  46. data/site/src/documentation/resources/images/sigmoid.png +0 -0
  47. data/test/classifiers/id3_test.rb +0 -1
  48. data/test/classifiers/multilayer_perceptron_test.rb +79 -0
  49. data/test/classifiers/one_r_test.rb +0 -2
  50. data/test/classifiers/prism_test.rb +0 -2
  51. data/test/classifiers/zero_r_test.rb +0 -2
  52. data/test/clusterers/average_linkage_test.rb +45 -0
  53. data/test/clusterers/bisecting_k_means_test.rb +0 -2
  54. data/test/clusterers/complete_linkage_test.rb +45 -0
  55. data/test/clusterers/k_means_test.rb +0 -2
  56. data/test/clusterers/single_linkage_test.rb +113 -0
  57. data/test/data/data_set_test.rb +3 -15
  58. data/test/data/proximity_test.rb +71 -0
  59. data/test/data/statistics_test.rb +65 -0
  60. data/test/experiment/classifier_evaluator_test.rb +76 -0
  61. metadata +27 -6
  62. data/site/src/documentation/content/xdocs/svn.xml +0 -41
@@ -47,7 +47,7 @@ See http://forrest.apache.org/docs/linking.html for more info
47
47
  </practicalExamples>
48
48
  <download label="Downloads" href="downloads.html" description="ai4r - Download Files"/>
49
49
  <forum label="Forum" href="http://forum.jadeferret.com/viewforum.php?f=3" description="Ai4r Forum"/>
50
- <svn label="Source Code repository" href="svn.html" description="ai4r Subversion repository"/>
50
+ <svn label="Source Code repository" href="sourceCode.html" description="ai4r source code repository"/>
51
51
  <wiki label="AI4R Wiki" href="http://wiki.jadeferret.com/Category:AI4R" description="ai4r wiki with How-to guides and more documentation"/>
52
52
  <rdoc label="API doc (Rdoc)" href="http://ai4r.rubyforge.org/rdoc/index.html" description="Ai4 RDoc docs"/>
53
53
  </about>
@@ -0,0 +1,43 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
3
+ <document>
4
+ <header>
5
+ <title>Subversion repository for ai4r (Artificial Intelligence for Ruby)</title>
6
+ </header>
7
+ <body>
8
+ <section id="github">
9
+ <title>AI4R at GitHub</title>
10
+ <p>AI4R Source Code is stored in a git repository at GitHub:</p>
11
+ <p>
12
+ <a href="http://github.com/JadeFerret/ai4r" title="AI4R source code at GitHub">
13
+ http://github.com/JadeFerret/ai4r
14
+ </a>
15
+ </p>
16
+ <p>The public clone url is:</p>
17
+ <p>
18
+ <a href="git://github.com/JadeFerret/ai4r.git" title="AI4R git repository">
19
+ git://github.com/JadeFerret/ai4r.git
20
+ </a>
21
+ </p>
22
+
23
+ </section>
24
+
25
+ <section id="download">
26
+ <title>Download the source code</title>
27
+ <p>If you want to download ai4r code without using git,
28
+ visit
29
+ <a href="http://github.com/JadeFerret/ai4r/downloads" title="AI4R source code download">
30
+ AI4R source code download site</a>, and click on the "download" button.
31
+ </p>
32
+ </section>
33
+
34
+ <section id="moreinfor">
35
+ <title>More info on Git</title>
36
+ <p>If you need help on using git, the best documentation is available
37
+ <a href="http://git-scm.com/documentation" title="git documentation">here</a>.</p>
38
+ <p>If you work in SCM, you may find <a href="http://scm.jadeferret.com" title="Agile SCM blog">Jade Ferret’s Agile SCM blog</a>
39
+ interesting.</p>
40
+ </section>
41
+
42
+ </body>
43
+ </document>
@@ -1,4 +1,3 @@
1
- #
2
1
  # id3_test.rb
3
2
  #
4
3
  # This is a unit test file for the ID3 algorithm (Quinlan) implemented
@@ -0,0 +1,79 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/multilayer_perceptron'
3
+ require File.dirname(__FILE__) + '/../../lib/ai4r/data/data_set'
4
+
5
+ # Make all accessors and methods public
6
+ class Ai4r::Classifiers::MultilayerPerceptron
7
+ attr_accessor :data_set, :class_value, :network, :domains, :outputs
8
+ public :get_max_index
9
+ public :data_to_output
10
+ end
11
+
12
+ class MultilayerPerceptronTest < Test::Unit::TestCase
13
+
14
+ include Ai4r::Classifiers
15
+ include Ai4r::Data
16
+
17
+ @@data_set = DataSet.new(:data_items =>[ ['New York', '<30', 'M', 'Y'],
18
+ ['Chicago', '<30', 'M', 'Y'],
19
+ ['New York', '<30', 'M', 'Y'],
20
+ ['New York', '[30-50)', 'F', 'N'],
21
+ ['Chicago', '[30-50)', 'F', 'Y'],
22
+ ['New York', '[30-50)', 'F', 'N'],
23
+ ['Chicago', '[50-80]', 'M', 'N'],
24
+ ])
25
+
26
+ def test_initialize
27
+ classifier = MultilayerPerceptron.new
28
+ assert_equal 1, classifier.active_node_value
29
+ assert_equal 0, classifier.inactive_node_value
30
+ assert_equal Ai4r::NeuralNetwork::Backpropagation, classifier.network_class
31
+ assert_equal [], classifier.hidden_layers
32
+ assert classifier.network_parameters
33
+ assert classifier.network_parameters.empty?
34
+ assert classifier.training_iterations > 1
35
+ end
36
+
37
+ def test_build
38
+ assert_raise(ArgumentError) { MultilayerPerceptron.new.build(DataSet.new) }
39
+ classifier = MultilayerPerceptron.new
40
+ classifier.training_iterations = 1
41
+ classifier.build(@@data_set)
42
+ assert_equal [7,2], classifier.network.structure
43
+ classifier.hidden_layers = [6, 4]
44
+ classifier.build(@@data_set)
45
+ assert_equal [7,6,4,2], classifier.network.structure
46
+ end
47
+
48
+ def test_eval
49
+ classifier = MultilayerPerceptron.new.build(@@data_set)
50
+ assert classifier
51
+ assert_equal('N', classifier.eval(['Chicago', '[50-80]', 'M']))
52
+ assert_equal('N', classifier.eval(['New York', '[30-50)', 'F']))
53
+ assert_equal('Y', classifier.eval(['New York', '<30', 'M']))
54
+ assert_equal('Y', classifier.eval(['Chicago', '[30-50)', 'F']))
55
+ end
56
+
57
+ def test_get_rules
58
+ assert_match(/raise/, MultilayerPerceptron.new.get_rules)
59
+ end
60
+
61
+ def test_get_max_index
62
+ classifier = MultilayerPerceptron.new
63
+ assert_equal(0, classifier.get_max_index([3, 1, 0.2, -9, 0, 2.99]))
64
+ assert_equal(2, classifier.get_max_index([3, 1, 5, -9, 0, 2.99]))
65
+ assert_equal(5, classifier.get_max_index([3, 1, 5, -9, 0, 6]))
66
+ end
67
+
68
+ def test_data_to_output
69
+ classifier = MultilayerPerceptron.new
70
+ classifier.outputs = 4
71
+ classifier.outputs = 4
72
+ classifier.domains = [nil, nil, nil, ["A", "B", "C", "D"]]
73
+ assert_equal([1,0,0,0], classifier.data_to_output("A"))
74
+ assert_equal([0,0,1,0], classifier.data_to_output("C"))
75
+ assert_equal([0,0,0,1], classifier.data_to_output("D"))
76
+ end
77
+
78
+ end
79
+
@@ -1,5 +1,3 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
-
3
1
  require 'test/unit'
4
2
  require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/one_r'
5
3
 
@@ -1,5 +1,3 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
-
3
1
  require 'test/unit'
4
2
  require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/prism'
5
3
 
@@ -1,5 +1,3 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
-
3
1
  require 'test/unit'
4
2
  require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/zero_r'
5
3
  require File.dirname(__FILE__) + '/../../lib/ai4r/data/data_set'
@@ -0,0 +1,45 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../lib/ai4r/clusterers/average_linkage'
3
+
4
+ class Ai4r::Clusterers::AverageLinkage < Ai4r::Clusterers::SingleLinkage
5
+ attr_accessor :data_set, :number_of_clusters, :clusters, :distance_matrix
6
+ public :calc_index_clusters_distance
7
+ public :distance_between_item_and_cluster
8
+ end
9
+
10
+ class AverageLinkageTest < Test::Unit::TestCase
11
+
12
+ include Ai4r::Clusterers
13
+ include Ai4r::Data
14
+
15
+ @@data = [ [10, 3], [3, 10], [2, 8], [2, 5], [3, 8], [10, 3],
16
+ [1, 3], [8, 1], [2, 9], [2, 5], [3, 3], [9, 4]]
17
+
18
+ @@expected_distance_matrix = [
19
+ [98.0],
20
+ [89.0, 5.0],
21
+ [68.0, 26.0, 9.0],
22
+ [74.0, 4.0, 1.0, 10.0],
23
+ [0.0, 98.0, 89.0, 68.0, 74.0],
24
+ [81.0, 53.0, 26.0, 5.0, 29.0, 81.0],
25
+ [8.0, 106.0, 85.0, 52.0, 74.0, 8.0, 53.0],
26
+ [100.0, 2.0, 1.0, 16.0, 2.0, 100.0, 37.0, 100.0],
27
+ [68.0, 26.0, 9.0, 0.0, 10.0, 68.0, 5.0, 52.0, 16.0],
28
+ [49.0, 49.0, 26.0, 5.0, 25.0, 49.0, 4.0, 29.0, 37.0, 5.0],
29
+ [2.0, 72.0, 65.0, 50.0, 52.0, 2.0, 65.0, 10.0, 74.0, 50.0, 37.0]]
30
+
31
+ def test_calc_index_clusters_distance
32
+ clusterer = AverageLinkage.new
33
+ clusterer.distance_matrix = @@expected_distance_matrix
34
+ assert_equal 98.0, clusterer.calc_index_clusters_distance([0], [1])
35
+ assert_equal 43.0, clusterer.calc_index_clusters_distance([0, 1], [3, 4])
36
+ end
37
+
38
+ def test_distance_between_item_and_cluster
39
+ clusterer = AverageLinkage.new
40
+ assert_equal 20.0, clusterer.distance_between_item_and_cluster([1,2],
41
+ DataSet.new(:data_items => [[3,4],[5,6]]))
42
+ end
43
+
44
+ end
45
+
@@ -1,5 +1,3 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
-
3
1
  require 'test/unit'
4
2
  require File.dirname(__FILE__) + '/../../lib/ai4r/clusterers/bisecting_k_means'
5
3
 
@@ -0,0 +1,45 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../lib/ai4r/clusterers/complete_linkage'
3
+
4
+ class Ai4r::Clusterers::CompleteLinkage
5
+ attr_accessor :data_set, :number_of_clusters, :clusters, :distance_matrix
6
+ public :calc_index_clusters_distance
7
+ public :distance_between_item_and_cluster
8
+ end
9
+
10
+ class CompleteLinkageTest < Test::Unit::TestCase
11
+
12
+ include Ai4r::Clusterers
13
+ include Ai4r::Data
14
+
15
+ @@data = [ [10, 3], [3, 10], [2, 8], [2, 5], [3, 8], [10, 3],
16
+ [1, 3], [8, 1], [2, 9], [2, 5], [3, 3], [9, 4]]
17
+
18
+ @@expected_distance_matrix = [
19
+ [98.0],
20
+ [89.0, 5.0],
21
+ [68.0, 26.0, 9.0],
22
+ [74.0, 4.0, 1.0, 10.0],
23
+ [0.0, 98.0, 89.0, 68.0, 74.0],
24
+ [81.0, 53.0, 26.0, 5.0, 29.0, 81.0],
25
+ [8.0, 106.0, 85.0, 52.0, 74.0, 8.0, 53.0],
26
+ [100.0, 2.0, 1.0, 16.0, 2.0, 100.0, 37.0, 100.0],
27
+ [68.0, 26.0, 9.0, 0.0, 10.0, 68.0, 5.0, 52.0, 16.0],
28
+ [49.0, 49.0, 26.0, 5.0, 25.0, 49.0, 4.0, 29.0, 37.0, 5.0],
29
+ [2.0, 72.0, 65.0, 50.0, 52.0, 2.0, 65.0, 10.0, 74.0, 50.0, 37.0]]
30
+
31
+ def test_calc_index_clusters_distance
32
+ clusterer = CompleteLinkage.new
33
+ clusterer.distance_matrix = @@expected_distance_matrix
34
+ assert_equal 98.0, clusterer.calc_index_clusters_distance([0], [1])
35
+ assert_equal 74.0, clusterer.calc_index_clusters_distance([0, 1], [3, 4])
36
+ end
37
+
38
+ def test_distance_between_item_and_cluster
39
+ clusterer = CompleteLinkage.new
40
+ assert_equal 32.0, clusterer.distance_between_item_and_cluster([1,2],
41
+ DataSet.new(:data_items => [[3,4],[5,6]]))
42
+ end
43
+
44
+ end
45
+
@@ -1,5 +1,3 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
-
3
1
  require 'test/unit'
4
2
  require File.dirname(__FILE__) + '/../../lib/ai4r/clusterers/k_means'
5
3
 
@@ -0,0 +1,113 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../lib/ai4r/clusterers/single_linkage'
3
+
4
+ class Ai4r::Clusterers::SingleLinkage
5
+ attr_accessor :data_set, :number_of_clusters, :clusters, :distance_matrix
6
+ end
7
+
8
+ class Ai4r::Clusterers::SingleLinkageTest < Test::Unit::TestCase
9
+
10
+ include Ai4r::Clusterers
11
+ include Ai4r::Data
12
+
13
+ @@data = [ [10, 3], [3, 10], [2, 8], [2, 5], [3, 8], [10, 3],
14
+ [1, 3], [8, 1], [2, 9], [2, 5], [3, 3], [9, 4]]
15
+
16
+ @@expected_distance_matrix = [
17
+ [98.0],
18
+ [89.0, 5.0],
19
+ [68.0, 26.0, 9.0],
20
+ [74.0, 4.0, 1.0, 10.0],
21
+ [0.0, 98.0, 89.0, 68.0, 74.0],
22
+ [81.0, 53.0, 26.0, 5.0, 29.0, 81.0],
23
+ [8.0, 106.0, 85.0, 52.0, 74.0, 8.0, 53.0],
24
+ [100.0, 2.0, 1.0, 16.0, 2.0, 100.0, 37.0, 100.0],
25
+ [68.0, 26.0, 9.0, 0.0, 10.0, 68.0, 5.0, 52.0, 16.0],
26
+ [49.0, 49.0, 26.0, 5.0, 25.0, 49.0, 4.0, 29.0, 37.0, 5.0],
27
+ [2.0, 72.0, 65.0, 50.0, 52.0, 2.0, 65.0, 10.0, 74.0, 50.0, 37.0]]
28
+
29
+ def setup
30
+ SingleLinkage.send(:public, *SingleLinkage.protected_instance_methods)
31
+ end
32
+
33
+ def test_build
34
+ clusterer = Ai4r::Clusterers::SingleLinkage.new
35
+ clusterer.build(DataSet.new(:data_items => @@data), 4)
36
+ #draw_map(clusterer)
37
+ assert_equal 4, clusterer.clusters.length
38
+ end
39
+
40
+ def test_eval
41
+ clusterer = Ai4r::Clusterers::SingleLinkage.new
42
+ clusterer.build(DataSet.new(:data_items => @@data), 4)
43
+ assert_equal 2, clusterer.eval([0,8])
44
+ assert_equal 0, clusterer.eval([8,0])
45
+ end
46
+
47
+ def test_create_distance_matrix
48
+ clusterer = Ai4r::Clusterers::SingleLinkage.new
49
+ clusterer.create_distance_matrix(DataSet.new(:data_items => @@data))
50
+ assert clusterer.distance_matrix
51
+ clusterer.distance_matrix.each_with_index do |row, row_index|
52
+ assert_equal row_index+1, row.length
53
+ end
54
+ assert_equal @@expected_distance_matrix, clusterer.distance_matrix
55
+ end
56
+
57
+ def test_read_distance_matrix
58
+ clusterer = Ai4r::Clusterers::SingleLinkage.new
59
+ clusterer.distance_matrix = @@expected_distance_matrix
60
+ assert_equal 9.0, clusterer.read_distance_matrix(3, 2)
61
+ assert_equal 9.0, clusterer.read_distance_matrix(2, 3)
62
+ assert_equal 0, clusterer.read_distance_matrix(5, 5)
63
+ end
64
+
65
+ def test_calc_index_clusters_distance
66
+ clusterer = Ai4r::Clusterers::SingleLinkage.new
67
+ clusterer.distance_matrix = @@expected_distance_matrix
68
+ assert_equal 98.0, clusterer.calc_index_clusters_distance([0], [1])
69
+ assert_equal 4.0, clusterer.calc_index_clusters_distance([0, 1], [3, 4])
70
+ end
71
+
72
+ def test_get_closest_clusters
73
+ clusterer = Ai4r::Clusterers::SingleLinkage.new
74
+ clusterer.distance_matrix = @@expected_distance_matrix
75
+ assert_equal [1,0], clusterer.get_closest_clusters([[0,1], [3,4]])
76
+ assert_equal [2,1], clusterer.get_closest_clusters([[3,4], [0,1], [5,6]])
77
+ end
78
+
79
+ def test_create_initial_index_clusters
80
+ clusterer = Ai4r::Clusterers::SingleLinkage.new
81
+ clusterer.data_set = DataSet.new :data_items => @@data
82
+ index_clusters = clusterer.create_initial_index_clusters
83
+ assert_equal @@data.length, index_clusters.length
84
+ assert_equal 0, index_clusters.first.first
85
+ assert_equal @@data.length-1, index_clusters.last.first
86
+ end
87
+
88
+ def test_merge_clusters
89
+ clusterer = Ai4r::Clusterers::SingleLinkage.new
90
+ clusters = clusterer.merge_clusters([1,2], [[1,2],[3,4],[5,6]])
91
+ assert_equal [[1,2], [3,4,5,6]], clusters.collect {|x| x.sort}
92
+ clusters = clusterer.merge_clusters([2,1], [[1,2],[3,4],[5,6]])
93
+ assert_equal [[1,2], [3,4,5,6]], clusters.collect {|x| x.sort}
94
+ end
95
+
96
+ def test_distance_between_item_and_cluster
97
+ clusterer = Ai4r::Clusterers::SingleLinkage.new
98
+ assert_equal 8.0, clusterer.distance_between_item_and_cluster([1,2],
99
+ DataSet.new(:data_items => [[3,4],[5,6]]))
100
+ end
101
+
102
+ private
103
+ def draw_map(clusterer)
104
+ map = Array.new(11) {Array.new(11, 0)}
105
+ clusterer.clusters.each_index do |i|
106
+ clusterer.clusters[i].data_items.each do |point|
107
+ map[point.first][point.last]=(i+1)
108
+ end
109
+ end
110
+ map.each { |row| puts row.inspect}
111
+ end
112
+
113
+ end
@@ -14,8 +14,8 @@ module Ai4r
14
14
  module Data
15
15
  class DataSetTest < Test::Unit::TestCase
16
16
 
17
- def test_load_data_and_labels_from_csv
18
- set = DataSet.new.load_data_and_labels_from_csv("#{File.dirname(__FILE__)}/data_set.csv")
17
+ def test_load_csv_with_labels
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
21
  end
@@ -63,19 +63,7 @@ module Ai4r
63
63
  assert_raise(ArgumentError) { set.set_data_items(nil) }
64
64
  assert_raise(ArgumentError) { set.set_data_items([1]) }
65
65
  end
66
-
67
- def test_get_attribute_mean_or_mode
68
- items = [ [ "New York", 25, "Y"],
69
- [ "New York", 55, "Y"],
70
- [ "Chicago", 23, "Y"],
71
- [ "Boston", 23, "N"],
72
- [ "Chicago", 12, "N"],
73
- [ "Chicago", 87, "Y"] ]
74
- set = DataSet.new.set_data_items(items)
75
- assert_equal "Chicago", set.get_attribute_mean_or_mode(0)
76
- assert_equal 37.5, set.get_attribute_mean_or_mode(1)
77
- end
78
-
66
+
79
67
  def test_get_mean_or_mode
80
68
  items = [ [ "New York", 25, "Y"],
81
69
  [ "New York", 55, "Y"],
@@ -0,0 +1,71 @@
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 'test/unit'
11
+ require File.dirname(__FILE__) + '/../../lib/ai4r/data/proximity'
12
+
13
+ module Ai4r
14
+ module Data
15
+ class ProximityTest < Test::Unit::TestCase
16
+
17
+ @@delta = 0.0001
18
+ @@data1 = [rand*10, rand*10, rand*-10]
19
+ @@data2 = [rand*10, rand*-10, rand*10]
20
+
21
+ def test_squared_euclidean_distance
22
+ assert_equal 0, Proximity.squared_euclidean_distance(@@data1, @@data1)
23
+ assert_equal Proximity.squared_euclidean_distance(@@data1, @@data2),
24
+ Proximity.squared_euclidean_distance(@@data2, @@data1)
25
+ assert 0 <= Proximity.squared_euclidean_distance(@@data1, @@data1)
26
+ assert_equal 2, Proximity.squared_euclidean_distance([1,1], [2,2])
27
+ assert_equal 9, Proximity.squared_euclidean_distance([3], [0])
28
+ end
29
+
30
+ def test_euclidean_distance
31
+ assert_equal 0, Proximity.euclidean_distance(@@data1, @@data1)
32
+ assert_equal Proximity.euclidean_distance(@@data1, @@data2),
33
+ Proximity.euclidean_distance(@@data2, @@data1)
34
+ assert 0 <= Proximity.euclidean_distance(@@data1, @@data1)
35
+ assert_equal Math.sqrt(2), Proximity.euclidean_distance([1,1], [2,2])
36
+ assert_equal 3, Proximity.euclidean_distance([3], [0])
37
+ end
38
+
39
+ def test_manhattan_distance
40
+ assert_equal 0, Proximity.manhattan_distance(@@data1, @@data1)
41
+ assert_equal Proximity.manhattan_distance(@@data1, @@data2),
42
+ Proximity.manhattan_distance(@@data2, @@data1)
43
+ assert 0 <= Proximity.manhattan_distance(@@data1, @@data1)
44
+ assert_equal 2, Proximity.manhattan_distance([1,1], [2,2])
45
+ assert_equal 9, Proximity.manhattan_distance([1,10], [2,2])
46
+ assert_equal 3, Proximity.manhattan_distance([3], [0])
47
+ end
48
+
49
+ def test_sup_distance
50
+ assert_equal 0, Proximity.sup_distance(@@data1, @@data1)
51
+ assert_equal Proximity.sup_distance(@@data1, @@data2),
52
+ Proximity.sup_distance(@@data2, @@data1)
53
+ assert 0 <= Proximity.sup_distance(@@data1, @@data1)
54
+ assert_equal 1, Proximity.sup_distance([1,1], [2,2])
55
+ assert_equal 8, Proximity.sup_distance([1,10], [2,2])
56
+ assert_equal 3, Proximity.sup_distance([3], [0])
57
+ end
58
+
59
+ def test_hamming_distance
60
+ assert_equal 0, Proximity.hamming_distance(@@data1, @@data1)
61
+ assert_equal Proximity.hamming_distance(@@data1, @@data2),
62
+ Proximity.hamming_distance(@@data2, @@data1)
63
+ assert 0 <= Proximity.hamming_distance(@@data1, @@data1)
64
+ assert_equal 1, Proximity.hamming_distance([1,1], [0,1])
65
+ assert_equal 2, Proximity.hamming_distance([1,10], [2,2])
66
+ assert_equal 1, Proximity.hamming_distance([3], [0])
67
+ end
68
+
69
+ end
70
+ end
71
+ end