dataset.scm 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ;; ================
  2. ;; DATA ABSTRACTION
  3. ;; ================
  4. ;; These procedures serve as an abstraction layer over the underlying data
  5. ;; structures used to represent data sets. Make sure to only ever use these
  6. ;; procedures, when dealing with data sets.
  7. (define-module (dataset))
  8. (use-modules
  9. ;; SRFI-1 for list procedures
  10. (srfi srfi-1)
  11. ;; SRFI-8 for `receive`
  12. (srfi srfi-8)
  13. (rnrs exceptions)
  14. (rnrs conditions)
  15. (utils)
  16. (utils list)
  17. (data-point))
  18. (define-public empty-dataset '())
  19. (define-public dataset-empty? null?)
  20. (define-public dataset-first car)
  21. (define-public dataset-rest cdr)
  22. (define-public dataset-range
  23. (lambda (data start end)
  24. "Get a range of values from the given data."
  25. (list-range data start end)))
  26. (define-public dataset-length length)
  27. (define-public dataset-num-cols
  28. (lambda (dataset)
  29. (cond
  30. [(dataset-empty? dataset) 0]
  31. [else
  32. (data-point-length (dataset-first dataset))])))
  33. (define-public dataset-filter
  34. (lambda (predicate data)
  35. (filter predicate data)))
  36. (define-public dataset-partition
  37. (lambda (predicate data)
  38. (partition predicate data)
  39. #;(receive (part-pred-true part-pred-false)
  40. (partition predicate data)
  41. (values part-pred-true part-pred-false))))
  42. (define-public dataset-map
  43. (lambda (procedure data)
  44. (map procedure data)))
  45. (define-public dataset-take
  46. (lambda (data n)
  47. (take data n)))
  48. (define-public dataset-drop
  49. (lambda (data n)
  50. (drop data n)))
  51. (define-public dataset-reduce list-reduce)
  52. (define-public dataset-get-col
  53. (lambda (data col-index)
  54. (dataset-map (lambda (data-point)
  55. (data-point-get-col data-point col-index))
  56. data)))
  57. (define-public column-uniform?
  58. (lambda (column equal-test)
  59. "Check, whether all elements in the column are equal."
  60. (apply equal-test column)))
  61. (define-public shuffle-dataset fisher-yates-shuffle)
  62. (define-public dataset-get-columns
  63. (lambda (dataset)
  64. (cond
  65. ;; if the dataset is empty, then there are no columns
  66. [(dataset-empty? dataset) '()]
  67. [else
  68. (let ([column-indices
  69. (range 0 (data-point-length (dataset-first dataset)))])
  70. (map (lambda (ind) (dataset-get-col dataset ind))
  71. column-indices))])))
  72. ;; ==================
  73. ;; COLUMN ABSTRACTION
  74. ;; ==================
  75. (define-public dataset-column-first car)
  76. (define-public dataset-column-rest cdr)
  77. (define-public dataset-column-empty? null?)