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
@@ -1,7 +1,6 @@
1
1
  = Introduction
2
2
 
3
- This project aims to produce ruby implementations of
4
- algorithms covering several Artificial intelligence fields.
3
+ This project aims to produce ruby implementations of algorithms covering several Artificial intelligence fields.
5
4
 
6
5
  = Where can I find the lastest code and info on this project?
7
6
 
@@ -11,13 +10,13 @@ http://www.ai4r.org
11
10
 
12
11
  1. Install the gem:
13
12
 
14
- gem install ai4r
13
+ gem install ai4r
15
14
 
16
15
  2. Include require statements in your code:
17
16
 
18
17
  require "rubygems"
19
18
  require "ai4r"
20
-
19
+
21
20
  = More Info
22
21
 
23
22
  * AI4R Project site: http://www.ai4r.org
@@ -31,14 +30,10 @@ If you have questions or constructive comments about this project send it to me:
31
30
  = Contributors
32
31
 
33
32
  This project was created and is maintained by {Sergio Fierens}[https://github.com/SergioFierens].
34
- There are other (great and absolutely cool) people who have donated time and code to make this project better, including:
35
-
36
- * {Thomas Kern}[https://github.com/thomaskern]
37
- * {Luis Parravicini}[https://github.com/luisparravicini]
38
- * {Kevin Menard}[https://github.com/nirvdrum]
33
+ There are other (great and absolutely cool) people who have donated time and code to make this project better. You can check out the complete AI4R contributors list: http://github.com/SergioFierens/ai4r/graphs/contributors.
39
34
 
40
35
  = Disclaimer
41
36
 
42
- This software is provided "as is" and without any express or implied warranties,
43
- including, without limitation, the implied warranties of merchantibility and
44
- fitness for a particular purpose.
37
+ This software is provided "as is" and without any express or implied warranties,
38
+ including, without limitation, the implied warranties of merchantibility and
39
+ fitness for a particular purpose.
@@ -0,0 +1,159 @@
1
+ symboling,normalized-losses,wheel-base,length,width,height,curb-weight,engine-size,bore,stroke,compression-ratio,horsepower,peak-rpm,city-mpg,highway-mpg,class
2
+ 2,164,99.8,176.6,66.2,54.3,2337,109,3.19,3.4,10,102,5500,24,30,13950
3
+ 2,164,99.4,176.6,66.4,54.3,2824,136,3.19,3.4,8,115,5500,18,22,17450
4
+ 1,158,105.8,192.7,71.4,55.7,2844,136,3.19,3.4,8.5,110,5500,19,25,17710
5
+ 1,158,105.8,192.7,71.4,55.9,3086,131,3.13,3.4,8.3,140,5500,17,20,23875
6
+ 2,192,101.2,176.8,64.8,54.3,2395,108,3.5,2.8,8.8,101,5800,23,29,16430
7
+ 0,192,101.2,176.8,64.8,54.3,2395,108,3.5,2.8,8.8,101,5800,23,29,16925
8
+ 0,188,101.2,176.8,64.8,54.3,2710,164,3.31,3.19,9,121,4250,21,28,20970
9
+ 0,188,101.2,176.8,64.8,54.3,2765,164,3.31,3.19,9,121,4250,21,28,21105
10
+ 2,121,88.4,141.1,60.3,53.2,1488,61,2.91,3.03,9.5,48,5100,47,53,5151
11
+ 1,98,94.5,155.9,63.6,52,1874,90,3.03,3.11,9.6,70,5400,38,43,6295
12
+ 0,81,94.5,158.8,63.6,52,1909,90,3.03,3.11,9.6,70,5400,38,43,6575
13
+ 1,118,93.7,157.3,63.8,50.8,1876,90,2.97,3.23,9.41,68,5500,37,41,5572
14
+ 1,118,93.7,157.3,63.8,50.8,1876,90,2.97,3.23,9.4,68,5500,31,38,6377
15
+ 1,118,93.7,157.3,63.8,50.8,2128,98,3.03,3.39,7.6,102,5500,24,30,7957
16
+ 1,148,93.7,157.3,63.8,50.6,1967,90,2.97,3.23,9.4,68,5500,31,38,6229
17
+ 1,148,93.7,157.3,63.8,50.6,1989,90,2.97,3.23,9.4,68,5500,31,38,6692
18
+ 1,148,93.7,157.3,63.8,50.6,1989,90,2.97,3.23,9.4,68,5500,31,38,7609
19
+ -1,110,103.3,174.6,64.6,59.8,2535,122,3.34,3.46,8.5,88,5000,24,30,8921
20
+ 3,145,95.9,173.2,66.3,50.2,2811,156,3.6,3.9,7,145,5000,19,24,12964
21
+ 2,137,86.6,144.6,63.9,50.8,1713,92,2.91,3.41,9.6,58,4800,49,54,6479
22
+ 2,137,86.6,144.6,63.9,50.8,1819,92,2.91,3.41,9.2,76,6000,31,38,6855
23
+ 1,101,93.7,150,64,52.6,1837,79,2.91,3.07,10.1,60,5500,38,42,5399
24
+ 1,101,93.7,150,64,52.6,1940,92,2.91,3.41,9.2,76,6000,30,34,6529
25
+ 1,101,93.7,150,64,52.6,1956,92,2.91,3.41,9.2,76,6000,30,34,7129
26
+ 0,110,96.5,163.4,64,54.5,2010,92,2.91,3.41,9.2,76,6000,30,34,7295
27
+ 0,78,96.5,157.1,63.9,58.3,2024,92,2.92,3.41,9.2,76,6000,30,34,7295
28
+ 0,106,96.5,167.5,65.2,53.3,2236,110,3.15,3.58,9,86,5800,27,33,7895
29
+ 0,106,96.5,167.5,65.2,53.3,2289,110,3.15,3.58,9,86,5800,27,33,9095
30
+ 0,85,96.5,175.4,65.2,54.1,2304,110,3.15,3.58,9,86,5800,27,33,8845
31
+ 0,85,96.5,175.4,62.5,54.1,2372,110,3.15,3.58,9,86,5800,27,33,10295
32
+ 0,85,96.5,175.4,65.2,54.1,2465,110,3.15,3.58,9,101,5800,24,28,12945
33
+ 1,107,96.5,169.1,66,51,2293,110,3.15,3.58,9.1,100,5500,25,31,10345
34
+ 0,145,113,199.6,69.6,52.8,4066,258,3.63,4.17,8.1,176,4750,15,19,32250
35
+ 1,104,93.1,159.1,64.2,54.1,1890,91,3.03,3.15,9,68,5000,30,31,5195
36
+ 1,104,93.1,159.1,64.2,54.1,1900,91,3.03,3.15,9,68,5000,31,38,6095
37
+ 1,104,93.1,159.1,64.2,54.1,1905,91,3.03,3.15,9,68,5000,31,38,6795
38
+ 1,113,93.1,166.8,64.2,54.1,1945,91,3.03,3.15,9,68,5000,31,38,6695
39
+ 1,113,93.1,166.8,64.2,54.1,1950,91,3.08,3.15,9,68,5000,31,38,7395
40
+ 1,129,98.8,177.8,66.5,53.7,2385,122,3.39,3.39,8.6,84,4800,26,32,8845
41
+ 0,115,98.8,177.8,66.5,55.5,2410,122,3.39,3.39,8.6,84,4800,26,32,8495
42
+ 1,129,98.8,177.8,66.5,53.7,2385,122,3.39,3.39,8.6,84,4800,26,32,10595
43
+ 0,115,98.8,177.8,66.5,55.5,2410,122,3.39,3.39,8.6,84,4800,26,32,10245
44
+ 0,115,98.8,177.8,66.5,55.5,2425,122,3.39,3.39,8.6,84,4800,26,32,11245
45
+ 0,118,104.9,175,66.1,54.4,2670,140,3.76,3.16,8,120,5000,19,27,18280
46
+ -1,93,110,190.9,70.3,56.5,3515,183,3.58,3.64,21.5,123,4350,22,25,25552
47
+ -1,93,110,190.9,70.3,58.7,3750,183,3.58,3.64,21.5,123,4350,22,25,28248
48
+ 0,93,106.7,187.5,70.3,54.9,3495,183,3.58,3.64,21.5,123,4350,22,25,28176
49
+ -1,93,115.6,202.6,71.7,56.3,3770,183,3.58,3.64,21.5,123,4350,22,25,31600
50
+ 3,142,96.6,180.3,70.5,50.8,3685,234,3.46,3.1,8.3,155,4750,16,18,35056
51
+ 2,161,93.7,157.3,64.4,50.8,1918,92,2.97,3.23,9.4,68,5500,37,41,5389
52
+ 2,161,93.7,157.3,64.4,50.8,1944,92,2.97,3.23,9.4,68,5500,31,38,6189
53
+ 2,161,93.7,157.3,64.4,50.8,2004,92,2.97,3.23,9.4,68,5500,31,38,6669
54
+ 1,161,93,157.3,63.8,50.8,2145,98,3.03,3.39,7.6,102,5500,24,30,7689
55
+ 3,153,96.3,173,65.4,49.4,2370,110,3.17,3.46,7.5,116,5500,23,30,9959
56
+ 3,153,96.3,173,65.4,49.4,2328,122,3.35,3.46,8.5,88,5000,25,32,8499
57
+ 1,125,96.3,172.4,65.4,51.6,2365,122,3.35,3.46,8.5,88,5000,25,32,6989
58
+ 1,125,96.3,172.4,65.4,51.6,2405,122,3.35,3.46,8.5,88,5000,25,32,8189
59
+ 1,125,96.3,172.4,65.4,51.6,2403,110,3.17,3.46,7.5,116,5500,23,30,9279
60
+ -1,137,96.3,172.4,65.4,51.6,2403,110,3.17,3.46,7.5,116,5500,23,30,9279
61
+ 1,128,94.5,165.3,63.8,54.5,1889,97,3.15,3.29,9.4,69,5200,31,37,5499
62
+ 1,128,94.5,165.3,63.8,54.5,2017,103,2.99,3.47,21.9,55,4800,45,50,7099
63
+ 1,128,94.5,165.3,63.8,54.5,1918,97,3.15,3.29,9.4,69,5200,31,37,6649
64
+ 1,122,94.5,165.3,63.8,54.5,1938,97,3.15,3.29,9.4,69,5200,31,37,6849
65
+ 1,103,94.5,170.2,63.8,53.5,2024,97,3.15,3.29,9.4,69,5200,31,37,7349
66
+ 1,128,94.5,165.3,63.8,54.5,1951,97,3.15,3.29,9.4,69,5200,31,37,7299
67
+ 1,128,94.5,165.6,63.8,53.3,2028,97,3.15,3.29,9.4,69,5200,31,37,7799
68
+ 1,122,94.5,165.3,63.8,54.5,1971,97,3.15,3.29,9.4,69,5200,31,37,7499
69
+ 1,103,94.5,170.2,63.8,53.5,2037,97,3.15,3.29,9.4,69,5200,31,37,7999
70
+ 2,168,95.1,162.4,63.8,53.3,2008,97,3.15,3.29,9.4,69,5200,31,37,8249
71
+ 0,106,97.2,173.4,65.2,54.7,2324,120,3.33,3.47,8.5,97,5200,27,34,8949
72
+ 0,106,97.2,173.4,65.2,54.7,2302,120,3.33,3.47,8.5,97,5200,27,34,9549
73
+ 0,128,100.4,181.7,66.5,55.1,3095,181,3.43,3.27,9,152,5200,17,22,13499
74
+ 0,108,100.4,184.6,66.5,56.1,3296,181,3.43,3.27,9,152,5200,17,22,14399
75
+ 0,108,100.4,184.6,66.5,55.1,3060,181,3.43,3.27,9,152,5200,19,25,13499
76
+ 3,194,91.3,170.7,67.9,49.7,3071,181,3.43,3.27,9,160,5200,19,25,17199
77
+ 3,194,91.3,170.7,67.9,49.7,3139,181,3.43,3.27,7.8,200,5200,17,23,19699
78
+ 1,231,99.2,178.5,67.9,49.7,3139,181,3.43,3.27,9,160,5200,19,25,18399
79
+ 0,161,107.9,186.7,68.4,56.7,3020,120,3.46,3.19,8.4,97,5000,19,24,11900
80
+ 0,161,107.9,186.7,68.4,56.7,3197,152,3.7,3.52,21,95,4150,28,33,13200
81
+ 0,161,107.9,186.7,68.4,56.7,3075,120,3.46,2.19,8.4,95,5000,19,24,15580
82
+ 0,161,107.9,186.7,68.4,56.7,3252,152,3.7,3.52,21,95,4150,28,33,16900
83
+ 0,161,107.9,186.7,68.4,56.7,3075,120,3.46,3.19,8.4,97,5000,19,24,16630
84
+ 0,161,107.9,186.7,68.4,56.7,3252,152,3.7,3.52,21,95,4150,28,33,17950
85
+ 0,161,108,186.7,68.3,56,3130,134,3.61,3.21,7,142,5600,18,24,18150
86
+ 1,119,93.7,157.3,63.8,50.8,1918,90,2.97,3.23,9.4,68,5500,37,41,5572
87
+ 1,119,93.7,157.3,63.8,50.8,2128,98,3.03,3.39,7.6,102,5500,24,30,7957
88
+ 1,154,93.7,157.3,63.8,50.6,1967,90,2.97,3.23,9.4,68,5500,31,38,6229
89
+ 1,154,93.7,167.3,63.8,50.8,1989,90,2.97,3.23,9.4,68,5500,31,38,6692
90
+ 1,154,93.7,167.3,63.8,50.8,2191,98,2.97,3.23,9.4,68,5500,31,38,7609
91
+ -1,74,103.3,174.6,64.6,59.8,2535,122,3.35,3.46,8.5,88,5000,24,30,8921
92
+ 3,186,94.5,168.9,68.3,50.2,2778,151,3.94,3.11,9.5,143,5500,19,27,22018
93
+ 3,150,99.1,186.6,66.5,56.1,2658,121,3.54,3.07,9.31,110,5250,21,28,11850
94
+ 2,104,99.1,186.6,66.5,56.1,2695,121,3.54,3.07,9.3,110,5250,21,28,12170
95
+ 3,150,99.1,186.6,66.5,56.1,2707,121,2.54,2.07,9.3,110,5250,21,28,15040
96
+ 2,104,99.1,186.6,66.5,56.1,2758,121,3.54,3.07,9.3,110,5250,21,28,15510
97
+ 3,150,99.1,186.6,66.5,56.1,2808,121,3.54,3.07,9,160,5500,19,26,18150
98
+ 2,104,99.1,186.6,66.5,56.1,2847,121,3.54,3.07,9,160,5500,19,26,18620
99
+ 2,83,93.7,156.9,63.4,53.7,2050,97,3.62,2.36,9,69,4900,31,36,5118
100
+ 2,83,93.7,157.9,63.6,53.7,2120,108,3.62,2.64,8.7,73,4400,26,31,7053
101
+ 2,83,93.3,157.3,63.8,55.7,2240,108,3.62,2.64,8.7,73,4400,26,31,7603
102
+ 0,102,97.2,172,65.4,52.5,2145,108,3.62,2.64,9.5,82,4800,32,37,7126
103
+ 0,102,97.2,172,65.4,52.5,2190,108,3.62,2.64,9.5,82,4400,28,33,7775
104
+ 0,102,97.2,172,65.4,52.5,2340,108,3.62,2.64,9,94,5200,26,32,9960
105
+ 0,102,97,172,65.4,54.3,2385,108,3.62,2.64,9,82,4800,24,25,9233
106
+ 0,102,97,172,65.4,54.3,2510,108,3.62,2.64,7.7,111,4800,24,29,11259
107
+ 0,89,97,173.5,65.4,53,2290,108,3.62,2.64,9,82,4800,28,32,7463
108
+ 0,89,97,173.5,65.4,53,2455,108,3.62,2.64,9,94,5200,25,31,10198
109
+ 0,85,96.9,173.6,65.4,54.9,2420,108,3.62,2.64,9,82,4800,23,29,8013
110
+ 0,85,96.9,173.6,65.4,54.9,2650,108,3.62,2.64,7.7,111,4800,23,23,11694
111
+ 1,87,95.7,158.7,63.6,54.5,1985,92,3.05,3.03,9,62,4800,35,39,5348
112
+ 1,87,95.7,158.7,63.6,54.5,2040,92,3.05,3.03,9,62,4800,31,38,6338
113
+ 1,74,95.7,158.7,63.6,54.5,2015,92,3.05,3.03,9,62,4800,31,38,6488
114
+ 0,77,95.7,169.7,63.6,59.1,2280,92,3.05,3.03,9,62,4800,31,37,6918
115
+ 0,81,95.7,169.7,63.6,59.1,2290,92,3.05,3.03,9,62,4800,27,32,7898
116
+ 0,91,95.7,169.7,63.6,59.1,3110,92,3.05,3.03,9,62,4800,27,32,8778
117
+ 0,91,95.7,166.3,64.4,53,2081,98,3.19,3.03,9,70,4800,30,37,6938
118
+ 0,91,95.7,166.3,64.4,52.8,2109,98,3.19,3.03,9,70,4800,30,37,7198
119
+ 0,91,95.7,166.3,64.4,53,2275,110,3.27,3.35,22.5,56,4500,34,36,7898
120
+ 0,91,95.7,166.3,64.4,52.8,2275,110,3.27,3.35,22.5,56,4500,38,47,7788
121
+ 0,91,95.7,166.3,64.4,53,2094,98,3.19,3.03,9,70,4800,38,47,7738
122
+ 0,91,95.7,166.3,64.4,52.8,2122,98,3.19,3.03,9,70,4800,28,34,8358
123
+ 0,91,95.7,166.3,64.4,52.8,2140,98,3.19,3.03,9,70,4800,28,34,9258
124
+ 1,168,94.5,168.7,64,52.6,2169,98,3.19,3.03,9,70,4800,29,34,8058
125
+ 1,168,94.5,168.7,64,52.6,2204,98,3.19,3.03,9,70,4800,29,34,8238
126
+ 1,168,94.5,168.7,64,52.6,2265,98,3.24,3.08,9.4,112,6600,26,29,9298
127
+ 1,168,94.5,168.7,64,52.6,2300,98,3.24,3.08,9.4,112,6600,26,29,9538
128
+ 2,134,98.4,176.2,65.6,52,2540,146,3.62,3.5,9.3,116,4800,24,30,8449
129
+ 2,134,98.4,176.2,65.6,52,2536,146,3.62,3.5,9.3,116,4800,24,30,9639
130
+ 2,134,98.4,176.2,65.6,52,2551,146,3.62,3.5,9.3,116,4800,24,30,9989
131
+ 2,134,98.4,176.2,65.6,52,2679,146,3.62,3.5,9.3,116,4800,24,30,11199
132
+ 2,134,98.4,176.2,65.6,52,2714,146,3.62,3.5,9.3,116,4800,24,30,11549
133
+ 2,134,98.4,176.2,65.6,53,2975,146,3.62,3.5,9.3,116,4800,24,30,17669
134
+ -1,65,102.4,175.6,66.5,54.9,2326,122,3.31,3.54,8.7,92,4200,29,34,8948
135
+ -1,65,102.4,175.6,66.5,54.9,2480,110,3.27,3.35,22.5,73,4500,30,33,10698
136
+ -1,65,102.4,175.6,66.5,53.9,2414,122,3.31,3.54,8.7,92,4200,27,32,9988
137
+ -1,65,102.4,175.6,66.5,54.9,2414,122,3.31,3.54,8.7,92,4200,27,32,10898
138
+ -1,65,102.4,175.6,66.5,53.9,2458,122,3.31,3.54,8.7,92,4200,27,32,11248
139
+ 3,197,102.9,183.5,67.7,52,2976,171,3.27,3.35,9.3,161,5200,20,24,16558
140
+ 3,197,102.9,183.5,67.7,52,3016,171,3.27,3.35,9.3,161,5200,19,24,15998
141
+ -1,90,104.5,187.8,66.5,54.1,3131,171,3.27,3.35,9.2,156,5200,20,24,15690
142
+ 2,122,97.3,171.7,65.5,55.7,2261,97,3.01,3.4,23,52,4800,37,46,7775
143
+ 2,122,97.3,171.7,65.5,55.7,2209,109,3.19,3.4,9,85,5250,27,34,7975
144
+ 2,94,97.3,171.7,65.5,55.7,2264,97,3.01,3.4,23,52,4800,37,46,7995
145
+ 2,94,97.3,171.7,65.5,55.7,2212,109,3.19,3.4,9,85,5250,27,34,8195
146
+ 2,94,97.3,171.7,65.5,55.7,2275,109,3.19,3.4,9,85,5250,27,34,8495
147
+ 2,94,97.3,171.7,65.5,55.7,2319,97,3.01,3.4,23,68,4500,37,42,9495
148
+ 2,94,97.3,171.7,65.5,55.7,2300,109,3.19,3.4,10,100,5500,26,32,9995
149
+ 3,256,94.5,165.7,64,51.4,2221,109,3.19,3.4,8.5,90,5500,24,29,9980
150
+ -2,103,104.3,188.8,67.2,56.2,2912,141,3.78,3.15,9.5,114,5400,23,28,12940
151
+ -1,74,104.3,188.8,67.2,57.5,3034,141,3.78,3.15,9.5,114,5400,23,28,13415
152
+ -2,103,104.3,188.8,67.2,56.2,2935,141,3.78,3.15,9.5,114,5400,24,28,15985
153
+ -1,74,104.3,188.8,67.2,57.5,3042,141,3.78,3.15,9.5,114,5400,24,28,16515
154
+ -2,103,104.3,188.8,67.2,56.2,3045,130,3.62,3.15,7.5,162,5100,17,22,18420
155
+ -1,74,104.3,188.8,67.2,57.5,3157,130,3.62,3.15,7.5,162,5100,17,22,18950
156
+ -1,95,109.1,188.8,68.9,55.5,2952,141,3.78,3.15,9.5,114,5400,23,28,16845
157
+ -1,95,109.1,188.8,68.8,55.5,3049,141,3.78,3.15,8.7,160,5300,19,25,19045
158
+ -1,95,109.1,188.8,68.9,55.5,3012,173,3.58,2.87,8.8,134,5500,18,23,21485
159
+ -1,95,109.1,188.8,68.9,55.5,3217,145,3.01,3.4,23,106,4800,26,27,22470
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/simple_linear_regression'
2
+ require File.dirname(__FILE__) + '/../../lib/ai4r/data/data_set'
3
+ require 'benchmark'
4
+
5
+ include Ai4r::Classifiers
6
+ include Ai4r::Data
7
+
8
+ data_set = DataSet.new
9
+ data_set.parse_csv_with_labels File.dirname(__FILE__) + "/simple_linear_regression_example.csv"
10
+
11
+ r = SimpleLinearRegression.new.build data_set
12
+ p r.eval([-1,95,109.1,188.8,68.9,55.5,3062,141,3.78,3.15,9.5,114,5400,19,25])
13
+
14
+ # => 11662.949367088606
15
+ #Actual price 22625
@@ -0,0 +1,56 @@
1
+ # Author:: Sergio Fierens (implementation)
2
+ # License:: MPL 1.1
3
+ # Project:: ai4r
4
+ # Url:: http://www.ai4r.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
+ # In this example we group results from a post-training survey into 4 groups.
11
+ # The Diana algorithm is used, but you can try other algorithms by changing
12
+ # the word "Diana" by "KMeans", "AverageLinkage", or any other cluster implementation.
13
+ # The cluster API is the same, so you can play around and observe different results.
14
+
15
+ require 'rubygems'
16
+ require 'ai4r'
17
+ include Ai4r::Data
18
+ include Ai4r::Clusterers
19
+
20
+ # 5 Questions on a post training survey
21
+ questions = [ "The material covered was appropriate for someone with my level of knowledge of the subject.",
22
+ "The material was presented in a clear and logical fashion",
23
+ "There was sufficient time in the session to cover the material that was presented",
24
+ "The instructor was respectful of students",
25
+ "The instructor provided good examples"]
26
+
27
+ # Answers to each question go from 1 (bad) to 5 (excellent)
28
+ # The answers array has an element per survey complemented.
29
+ # Each survey completed is in turn an array with the answer of each question.
30
+ answers = [ [ 1, 2, 3, 2, 2], # Answers of person 1
31
+ [ 5, 5, 3, 2, 2], # Answers of person 2
32
+ [ 1, 2, 3, 2, 2], # Answers of person 3
33
+ [ 1, 2, 2, 2, 2], # ...
34
+ [ 1, 2, 5, 5, 2],
35
+ [ 3, 3, 3, 3, 3],
36
+ [ 1, 2, 3, 2, 2],
37
+ [ 3, 2, 3, 5, 5],
38
+ [ 3, 3, 3, 5, 2],
39
+ [ 4, 4, 3, 1, 1],
40
+ [ 5, 5, 5, 5, 5],
41
+ [ 4, 2, 4, 2, 1],
42
+ [ 4, 4, 5, 5, 5],
43
+ [ 4, 4, 3, 2, 2],
44
+ [ 2, 2, 3, 2, 3],
45
+ [ 3, 3, 3, 1, 1]] # Answers of person 16
46
+
47
+ data_set = DataSet.new(:data_items => answers, :data_labels => questions)
48
+
49
+ # Let's group answers in 4 groups
50
+ clusterer = Diana.new.build(data_set, 4)
51
+
52
+ clusterer.clusters.each_with_index do |cluster, index|
53
+ puts "Group #{index+1}"
54
+ p cluster.data_items
55
+ end
56
+
@@ -16,7 +16,8 @@ require 'benchmark'
16
16
  times = Benchmark.measure do
17
17
 
18
18
  srand 1
19
-
19
+
20
+ # creating network with 256 input-neurons, 3-neurons and 0 hidden layers
20
21
  net = Ai4r::NeuralNetwork::Backpropagation.new([256, 3])
21
22
 
22
23
  tr_input = TRIANGLE.flatten.collect { |input| input.to_f / 5.0}
@@ -14,6 +14,7 @@ require File.dirname(__FILE__) + "/ai4r/clusterers/weighted_average_linkage"
14
14
  require File.dirname(__FILE__) + "/ai4r/clusterers/centroid_linkage"
15
15
  require File.dirname(__FILE__) + "/ai4r/clusterers/median_linkage"
16
16
  require File.dirname(__FILE__) + "/ai4r/clusterers/ward_linkage"
17
+ require File.dirname(__FILE__) + "/ai4r/clusterers/ward_linkage_hierarchical"
17
18
  require File.dirname(__FILE__) + "/ai4r/clusterers/diana"
18
19
  # Classifiers
19
20
  require File.dirname(__FILE__) + "/ai4r/classifiers/classifier"
@@ -30,4 +31,5 @@ require File.dirname(__FILE__) + "/ai4r/neural_network/hopfield"
30
31
  # Genetic Algorithms
31
32
  require File.dirname(__FILE__) + "/ai4r/genetic_algorithm/genetic_algorithm"
32
33
  # SOM
33
- require File.dirname(__FILE__) + "/ai4r/som/som"
34
+ require File.dirname(__FILE__) + "/ai4r/som/som"
35
+
@@ -280,7 +280,7 @@ module Ai4r
280
280
 
281
281
  def value(data)
282
282
  value = data[@index]
283
- return rule_not_found if !@values.include?(value)
283
+ return ErrorNode.new.value(data) if !@values.include?(value)
284
284
  return nodes[@values.index(value)].value(data)
285
285
  end
286
286
 
@@ -313,9 +313,13 @@ module Ai4r
313
313
  end
314
314
  end
315
315
 
316
+ class ModelFailureError < StandardError
317
+ default_message = "There was not enough information during training to do a proper induction for this data element."
318
+ end
319
+
316
320
  class ErrorNode #:nodoc: all
317
321
  def value(data)
318
- raise "There was not enough information during training to do a proper induction for this data element."
322
+ raise ModelFailureError, "There was not enough information during training to do a proper induction for the data element #{data}."
319
323
  end
320
324
  def get_rules
321
325
  return []
@@ -47,7 +47,7 @@ module Ai4r
47
47
  :hidden_layers => "Hidden layer structure. E.g. [8, 6] will generate " +
48
48
  "2 hidden layers with 8 and 6 neurons each. By default []",
49
49
  :training_iterations => "How many times the training should be " +
50
- "repeated. By default: 1000",
50
+ "repeated. By default: 500",
51
51
  :active_node_value => "Default: 1",
52
52
  :inactive_node_value => "Default: 0"
53
53
 
@@ -57,8 +57,8 @@ module Ai4r
57
57
 
58
58
  class NaiveBayes < Classifier
59
59
 
60
- parameters_info :m => "Default value is set to 0. It may be set to a value greater than " +
61
- "0 when the size of the dataset is relatively small"
60
+ parameters_info :m => 'Default value is set to 0. It may be set to a value greater than ' +
61
+ '0 when the size of the dataset is relatively small'
62
62
 
63
63
  def initialize
64
64
  @m = 0
@@ -75,7 +75,7 @@ module Ai4r
75
75
  # b.eval(["Red", "SUV", "Domestic"])
76
76
  # => 'No'
77
77
  def eval(data)
78
- prob = @class_prob.map {|cp| cp}
78
+ prob = @class_prob.dup
79
79
  prob = calculate_class_probabilities_for_entry(data, prob)
80
80
  index_to_klass(prob.index(prob.max))
81
81
  end
@@ -90,27 +90,28 @@ module Ai4r
90
90
  # b.get_probability_map(["Red", "SUV", "Domestic"])
91
91
  # => {"Yes"=>0.4166666666666667, "No"=>0.5833333333333334}
92
92
  def get_probability_map(data)
93
- prob = @class_prob.map {|cp| cp}
93
+ prob = @class_prob.dup
94
94
  prob = calculate_class_probabilities_for_entry(data, prob)
95
95
  prob = normalize_class_probability prob
96
96
  probability_map = {}
97
97
  prob.each_with_index { |p, i| probability_map[index_to_klass(i)] = p }
98
- return probability_map
98
+
99
+ probability_map
99
100
  end
100
101
 
101
102
  # counts values of the attribute instances and calculates the probability of the classes
102
103
  # and the conditional probabilities
103
104
  # Parameter data has to be an instance of CsvDataSet
104
105
  def build(data)
105
- raise "Error instance must be passed" unless data.is_a?(DataSet)
106
- raise "Data should not be empty" if data.data_items.length == 0
106
+ raise 'Error instance must be passed' unless data.is_a?(Ai4r::Data::DataSet)
107
+ raise 'Data should not be empty' if data.data_items.length == 0
107
108
 
108
109
  initialize_domain_data(data)
109
110
  initialize_klass_index
110
111
  initialize_pc
111
112
  calculate_probabilities
112
113
 
113
- return self
114
+ self
114
115
  end
115
116
 
116
117
  private
@@ -128,25 +129,27 @@ module Ai4r
128
129
  # probability of every attribute in condition to a specific class
129
130
  # this is repeated for every class
130
131
  def calculate_class_probabilities_for_entry(data, prob)
131
- prob.each_with_index do |prob_entry, prob_index|
132
+ 0.upto(prob.length - 1) do |prob_index|
132
133
  data.each_with_index do |att, index|
133
134
  next if value_index(att, index).nil?
134
135
  prob[prob_index] *= @pcp[index][value_index(att, index)][prob_index]
135
136
  end
136
137
  end
138
+
139
+ prob
137
140
  end
138
141
 
139
142
  # normalises the array of probabilities so the sum of the array equals 1
140
143
  def normalize_class_probability(prob)
141
144
  prob_sum = sum(prob)
142
145
  prob_sum > 0 ?
143
- prob.map {|prob_entry| prob_entry / prob_sum } :
146
+ prob.map { |prob_entry| prob_entry / prob_sum } :
144
147
  prob
145
148
  end
146
149
 
147
150
  # sums an array up; returns a number of type Float
148
151
  def sum(array)
149
- array.inject(0.0){|b, i| b+i}
152
+ array.inject(0.0) { |b, i| b + i }
150
153
  end
151
154
 
152
155
  # returns the name of the class when the index is found
@@ -160,7 +163,7 @@ module Ai4r
160
163
  @klass_index[dl] = index
161
164
  end
162
165
 
163
- @data_labels.each_with_index do |dl, index|
166
+ 0.upto(@data_labels.length - 1) do |index|
164
167
  @values[index] = {}
165
168
  @domains[index].each_with_index do |d, d_index|
166
169
  @values[index][d] = d_index
@@ -180,19 +183,19 @@ module Ai4r
180
183
 
181
184
  # builds an array of the form:
182
185
  # array[attributes][values][classes]
183
- def build_array(dl, index)
186
+ def build_array(index)
184
187
  domains = Array.new(@domains[index].length)
185
- domains.map do |p1|
186
- pl = Array.new @klasses.length, 0
188
+ domains.map do
189
+ Array.new @klasses.length, 0
187
190
  end
188
191
  end
189
192
 
190
193
  # initializes the two array for storing the count and conditional probabilities of
191
194
  # the attributes
192
195
  def initialize_pc
193
- @data_labels.each_with_index do |dl, index|
194
- @pcc << build_array(dl, index)
195
- @pcp << build_array(dl, index)
196
+ 0.upto(@data_labels.length - 1) do |index|
197
+ @pcc << build_array(index)
198
+ @pcp << build_array(index)
196
199
  end
197
200
  end
198
201
 
@@ -200,7 +203,7 @@ module Ai4r
200
203
  # certain attribute and the assigned class.
201
204
  # In addition to that, it also calculates the conditional probabilities and values
202
205
  def calculate_probabilities
203
- @klasses.each {|dl| @class_counts[klass_index(dl)] = 0}
206
+ @klasses.each { |dl| @class_counts[klass_index(dl)] = 0 }
204
207
 
205
208
  calculate_class_probabilities
206
209
  count_instances
@@ -220,7 +223,7 @@ module Ai4r
220
223
  # counts the instances of a certain value of a certain attribute and the assigned class
221
224
  def count_instances
222
225
  @data_items.each do |item|
223
- @data_labels.each_with_index do |dl, dl_index|
226
+ 0.upto(@data_labels.length - 1) do |dl_index|
224
227
  @pcc[dl_index][value_index(item[dl_index], dl_index)][klass_index(item.klass)] += 1
225
228
  end
226
229
  end
@@ -231,7 +234,7 @@ module Ai4r
231
234
  @pcc.each_with_index do |attributes, a_index|
232
235
  attributes.each_with_index do |values, v_index|
233
236
  values.each_with_index do |klass, k_index|
234
- @pcp[a_index][v_index][k_index] = (klass.to_f + @m * @class_prob[k_index]) / (@class_counts[k_index] + @m).to_f
237
+ @pcp[a_index][v_index][k_index] = (klass.to_f + @m * @class_prob[k_index]) / (@class_counts[k_index] + @m)
235
238
  end
236
239
  end
237
240
  end