dataset-test.scm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. (use-modules
  2. ;; SRFI 64 for unit testing facilities
  3. (srfi srfi-64)
  4. ;; utils - the code to be tested
  5. (decision-tree)
  6. ;; Utilities for testing
  7. (utils test)
  8. ;; Dependencies for testing the code to be tested
  9. (dataset)
  10. (data-point)
  11. (tree))
  12. (define TEST-DATA
  13. (list #(2.771244718 1.784783929 0)
  14. #(1.728571309 1.169761413 0)
  15. #(3.678319846 2.81281357 0)
  16. #(3.961043357 2.61995032 0)
  17. #(2.999208922 2.209014212 0)
  18. #(7.497545867 3.162953546 1)
  19. #(9.00220326 3.339047188 1)
  20. #(7.444542326 0.476683375 1)
  21. #(10.12493903 3.234550982 1)
  22. #(6.642287351 3.319983761 1)))
  23. (define PRECISION (expt 10 -9))
  24. (test-begin "dataset-test")
  25. (test-group
  26. "dataset-empty?"
  27. (test-assert "dataset-empty?-1"
  28. (dataset-empty? empty-dataset))
  29. (test-assert "dataset-empty?-2"
  30. (not (dataset-empty? (list 1 2 3)))))
  31. (test-group
  32. "dataset-first"
  33. (test-equal "dataset-first-1"
  34. #(0 0 0)
  35. (dataset-first (list #(0 0 0) #(1 1 1)))))
  36. (test-group
  37. "dataset-rest"
  38. (test-equal "dataset-rest-1"
  39. (list #(1 1 1))
  40. (dataset-rest (list #(0 0 0) #(1 1 1))))
  41. (test-equal "dataset-rest-2"
  42. (list #(1 1 1)
  43. #(2 2 2)
  44. #(3 3 3))
  45. (dataset-rest (list #(0 0 0)
  46. #(1 1 1)
  47. #(2 2 2)
  48. #(3 3 3)))))
  49. (test-group
  50. "dataset-length"
  51. (test-equal "dataset-length-1"
  52. 3
  53. (dataset-length (list #(0) #(1) #(2))))
  54. (test-equal "dataset-length-2"
  55. 4
  56. (dataset-length (list #(0) #(1) #(2 3) #(3 4 5)))))
  57. (test-group
  58. "dataset-filter"
  59. (test-equal "dataset-filter-1"
  60. (dataset-filter (lambda (data-point)
  61. (> (data-point-get-col data-point 1) 3))
  62. TEST-DATA)
  63. (list #(7.497545867 3.162953546 1)
  64. #(9.00220326 3.339047188 1)
  65. #(10.12493903 3.234550982 1)
  66. #(6.642287351 3.319983761 1)))
  67. (test-equal "dataset-filter-2"
  68. (dataset-filter (lambda (data-point)
  69. (< (data-point-get-col data-point 0) 2))
  70. TEST-DATA)
  71. (list #(1.728571309 1.169761413 0))))
  72. (test-group
  73. "dataset-map"
  74. (test-assert "dataset-map-1"
  75. (lists-of-vectors-approximately-equal?
  76. (dataset-map (lambda (data-point)
  77. (vector-set data-point 0
  78. (+ (vector-ref data-point 0) 1.0)))
  79. TEST-DATA)
  80. (list #(3.771244718 1.784783929 0)
  81. #(2.728571309 1.169761413 0)
  82. #(4.678319846 2.81281357 0)
  83. #(4.961043357 2.61995032 0)
  84. #(3.999208922 2.209014212 0)
  85. #(8.497545867 3.162953546 1)
  86. #(10.00220326 3.339047188 1)
  87. #(8.444542326 0.476683375 1)
  88. #(11.12493903 3.234550982 1)
  89. #(7.642287351 3.319983761 1))
  90. PRECISION
  91. (lambda (list-1-elem list-2-elem epsilon)
  92. (vectors-approximately-equal? list-1-elem
  93. list-2-elem
  94. epsilon
  95. approximately-equal?)))))
  96. (test-group
  97. "dataset-get-col"
  98. (test-equal "dataset-get-col-1"
  99. ;; index starting at 0, it should be the following list
  100. (list 1 2 3)
  101. (dataset-get-col (list #(123 43 1)
  102. #(2132435 23 2)
  103. #(1244 769 3))
  104. 2)))
  105. (test-group
  106. "data-point-get-col"
  107. (test-equal "data-point-get-col-1"
  108. 32
  109. (data-point-get-col #(0 32 478 282 1) 1))
  110. (test-equal "data-point-get-col-2"
  111. 0
  112. (data-point-get-col #(0 32 478 282 1) 0)))
  113. (test-group
  114. "dataset-reduce"
  115. (test-approximate "dataset-reduce-1"
  116. 3.0
  117. (dataset-reduce
  118. (list
  119. #(1 2 0.5)
  120. #(3 -4 1.0)
  121. #(5 6 1.5))
  122. (lambda (acc data-point)
  123. (+ acc (data-point-get-col data-point 2)))
  124. 0)
  125. PRECISION))
  126. (test-group
  127. "select-min-cost-split"
  128. (test-equal "select-min-cost-split should never select infinite cost"
  129. (make-split 0
  130. 6.642287351
  131. (list
  132. ;; left branch data
  133. (list #(2.771244718 1.784783929 0)
  134. #(1.728571309 1.169761413 0)
  135. #(3.678319846 2.81281357 0)
  136. #(3.961043357 2.61995032 0)
  137. #(2.999208922 2.209014212 0))
  138. ;; right branch data
  139. (list #(7.497545867 3.162953546 1)
  140. #(9.00220326 3.339047188 1)
  141. #(7.444542326 0.476683375 1)
  142. #(10.12493903 3.234550982 1)
  143. #(6.642287351 3.319983761 1)))
  144. 0.0)
  145. (select-min-cost-split
  146. (make-split 0
  147. 6.642287351
  148. (list
  149. ;; left branch data
  150. (list #(2.771244718 1.784783929 0)
  151. #(1.728571309 1.169761413 0)
  152. #(3.678319846 2.81281357 0)
  153. #(3.961043357 2.61995032 0)
  154. #(2.999208922 2.209014212 0))
  155. ;; right branch data
  156. (list #(7.497545867 3.162953546 1)
  157. #(9.00220326 3.339047188 1)
  158. #(7.444542326 0.476683375 1)
  159. #(10.12493903 3.234550982 1)
  160. #(6.642287351 3.319983761 1)))
  161. 0.0)
  162. (make-split 0 +inf.0 (list '() '()) +inf.0))))
  163. (test-group
  164. "dataset-get-columns"
  165. (test-equal "dataset-get-columns-1"
  166. (list
  167. (list 2.771244718
  168. 1.728571309
  169. 3.678319846
  170. 3.961043357
  171. 2.999208922
  172. 7.497545867
  173. 9.00220326
  174. 7.444542326
  175. 10.12493903
  176. 6.642287351)
  177. (list 1.784783929
  178. 1.169761413
  179. 2.81281357
  180. 2.61995032
  181. 2.209014212
  182. 3.162953546
  183. 3.339047188
  184. 0.476683375
  185. 3.234550982
  186. 3.319983761)
  187. (list 0
  188. 0
  189. 0
  190. 0
  191. 0
  192. 1
  193. 1
  194. 1
  195. 1
  196. 1))
  197. (dataset-get-columns TEST-DATA)))
  198. (test-end "dataset-test")