data-representation-abstraction.rkt 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #lang racket
  2. (require "utils.rkt")
  3. (provide (all-defined-out))
  4. (define (data-empty? data)
  5. (empty? data))
  6. (define (data-first data)
  7. (car data))
  8. (define (data-rest data)
  9. (cdr data))
  10. (define (data-range data start end)
  11. (list-range data start end))
  12. (define (data-length data)
  13. (length data))
  14. (define (data-point-length data-point)
  15. (vector-length data-point))
  16. (define (data-filter predicate data)
  17. (filter predicate data))
  18. (define (data-partition predicate data)
  19. (partition predicate data))
  20. (define (data-map procedure data)
  21. (map procedure data))
  22. (define (data-take data n)
  23. (take data n))
  24. (define (data-drop data n)
  25. (drop data n))
  26. (define (data-get-col data col-index)
  27. (data-map (lambda (data-point)
  28. (data-point-get-col data-point col-index))
  29. data))
  30. (define (data-point-get-col data-point col-index)
  31. (vector-ref data-point col-index))
  32. (define (data-point-take-features data-point n-features)
  33. (vector-take data-point n-features))
  34. (define (labels-elements-equal? subset)
  35. (with-handlers ([exn:fail:contract:arity?
  36. (lambda (exception)
  37. (< (data-length subset) 2))])
  38. (apply = subset)))
  39. (define (data-majority-prediction data label-column-index)
  40. (let-values ([(part1 part2)
  41. (data-partition (lambda (data-point)
  42. (= (data-point-get-col data-point label-column-index) 0))
  43. data)])
  44. (cond [(> (data-length part2) (data-length part1)) 1]
  45. [else 0])))