-
Notifications
You must be signed in to change notification settings - Fork 65
/
read-data.cl
executable file
·1101 lines (1041 loc) · 49.8 KB
/
read-data.cl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(defpackage :hjs.learn.read-data
(:use :cl :hjs.util.meta :hjs.util.vector :hjs.learn.vars :hjs.util.matrix
:handling-missing-value)
(:nicknames :read-data)
(:import-from :handling-missing-value #:interpolate)
(:export
#:read-data-from-file
#:pick-and-specialize-data
#:divide-dataset
#:dataset-dimensions
#:dataset-points
#:unspecialized-dataset
#:specialized-dataset
#:numeric-dataset
#:numeric-matrix-dataset
#:dataset-numeric-points
#:numeric-and-category-dataset
#:numeric-matrix-and-category-dataset
#:dataset-category-points
#:dimension-name
#:dimension-type
#:dimension-index
#:dimension-metadata
#:make-dimension
#:copy-dimension
#:make-unspecialized-dataset
#:make-numeric-dataset
#:make-numeric-matrix-dataset
#:make-numeric-and-category-dataset
#:make-numeric-matrix-and-category-dataset
#:choice-a-dimension
#:choice-dimensions
#:dataset-cleaning
#:copy-dataset
#:make-bootstrap-sample-datasets))
(in-package :hjs.learn.read-data)
#+lispworks
(eval-when (:load-toplevel :execute)
(lw:set-default-character-element-type 'lw:simple-char))
;;;; helper function
;;@ function-type: t -> symbol
(defun guess-data-type (value)
(typecase value
(double-float :numeric)
(string :category)
(otherwise :unknown)))
;;;; data type definition
(defconstant +known-data-type+ '(:numeric :category :unknown))
(defclass dimension ()
((name
:initarg :name
:accessor dimension-name
:type string
:initform "")
(type
:initarg :type
:accessor dimension-type
:type symbol
:initform (error "Must specify the data type of the dimension.")
:documentation "Can be numeric or category.")
(index
:initarg :index
:accessor dimension-index
:type fixnum
:initform (error "Must specify the index of dimension either in the numerically typed data vector or the categorially typed data vector.")
:documentation "The nth column of points array of corresponding type. (type * index) should locate the place where dimension is stored.")
(metadata
:initarg :metadata
:accessor dimension-metadata
:type list
:initform '()
:documentation "An alist that stores some useful information, e.g. the hashtable (for equality) for a category dimension."
)))
(define-condition dimension-unknown-type-error (simple-error)
((dimension :initarg :dimension)))
(defmethod initialize-instance :after ((d dimension) &key &allow-other-keys)
(when (not (find (dimension-type d) +known-data-type+))
(error 'dimension-unknown-type-error
:format-control "Type of dimension(~s) is unknown, it must be one of ~s"
:format-arguments (list (dimension-type d) +known-data-type+)
:dimension d)))
(defun make-dimension (name type index &key metadata)
(check-type name string)
(check-type type symbol)
(check-type index fixnum)
(check-type metadata list)
(make-instance 'dimension
:name name
:type type
:index index
:metadata metadata))
(defmethod copy-dimension ((dimension dimension))
(make-dimension (dimension-name dimension)
(dimension-type dimension)
(dimension-index dimension)
:metadata (dimension-metadata dimension)))
(defmethod print-object ((d dimension) stream)
(with-accessors ((name dimension-name)
(type dimension-type)
(index dimension-index)) d
(print-unreadable-object (d stream :type t :identity nil)
(format stream "NAME: ~A, TYPE: ~A, INDEX: ~A."
name type index))))
(defclass dataset ()
((dimensions
:initarg :dimensions
:accessor dataset-dimensions
:type (simple-array dimension (*))
:initform (error "Must specify the dimension information for the dataset.")))
(:documentation
"The base class."))
(defmethod print-object ((d dataset) stream)
(with-accessors ((dim dataset-dimensions)) d
(let ((*print-length* (or *print-length* 10))
(names (map 'list #'dimension-name dim))
(types (map 'list #'dimension-type dim)))
(print-unreadable-object (d stream :type t :identity nil))
(if (> (length names) *print-length*)
(format stream "~&DIMENSIONS: ~{~A~^~T| ~} ...~%" (subseq names 0 *print-length*))
(format stream "~&DIMENSIONS: ~{~A~^~T| ~}~%" names))
(if (> (length names) *print-length*)
(format stream "~&TYPES: ~{~A~^~T| ~} ...~%" (subseq types 0 *print-length*))
(format stream "~&TYPES: ~{~A~^~T| ~}~%" types))
(format stream "~&NUMBER OF DIMENSIONS: ~a~%" (length names)))))
(defclass unspecialized-dataset (dataset)
((points
:initarg :points
:accessor dataset-points
:type (simple-array dvec (*))
:initform (error "Must specify points of the dataset.")))
(:documentation
"Unspecialized data, numeric value and category value are stored in one array."))
(defmethod print-object ((d unspecialized-dataset) stream)
(call-next-method)
(format stream "~&DATA POINTS: ~a POINTS~%" (length (dataset-points d))))
;;@ function-type: (string) -> #(dvec) -> unspecialized-dataset
;;@ precondition:
;;@ - length of data > 0
;;@ - length of all-column-names > 0, zero dimension is meaningless
;;@ - dimensions of all-column-names = dimensions of a point
(defun make-unspecialized-dataset (all-column-names data
&key (missing-value-check t)
missing-values-list
(missing-value-test #'equalp))
(assert (> (length data) 0))
(assert (> (length all-column-names) 0))
(assert (every (let ((n (length all-column-names)))
(declare (type fixnum n))
(lambda (p) (= n (the fixnum (length p))))) data))
;; create dimensions
(let ((dimensions
(loop
for n in all-column-names
for i from 0
collect (make-dimension n :unknown i) into result
finally (return (coerce result 'vector)))))
;; make dataset
(make-instance 'unspecialized-dataset
:dimensions dimensions
:points (if missing-value-check
(let ((test-fcn
(if missing-values-list
#'(lambda (val) (missing-value-p
val
:missing-values-list missing-values-list
:test missing-value-test))
#'(lambda (val)
(missing-value-p val :test missing-value-test)))))
(do-vec (vec data :setf-var sf :return data)
(setf sf (fill-na vec test-fcn))))
data))))
(defgeneric choice-dimensions (names data)
(:documentation "Pick up several dimensions as (vector vector)"))
(defgeneric choice-a-dimension (name data)
(:documentation "Pick up a dimension as vector"))
(defun choice-poses (data poses l)
(let ((ans (make-array l :element-type t))
(i 0))
(dolist (p poses ans)
(setf (svref ans i) (svref data p))
(incf i))))
(defmethod choice-dimensions (names (unsp-data unspecialized-dataset))
(let* ((dims (dataset-dimensions unsp-data))
(poses (mapcar
#'(lambda (x)
(dimension-index
(find x dims
:test #'string=
:key #'dimension-name)))
names))
(l (length names)))
(loop for vec across (dataset-points unsp-data)
collect (choice-poses vec poses l) into result
finally (return (coerce result 'vector)))))
(defmethod choice-a-dimension (name (unsp-data unspecialized-dataset))
(let ((pos (dimension-index
(find name
(dataset-dimensions unsp-data)
:test #'string=
:key #'dimension-name))))
(loop for vec across (dataset-points unsp-data)
collect (svref vec pos) into result
finally (return (coerce result 'vector)))))
;;;; specialized dataset
(defclass specialized-dataset (dataset)
()
(:documentation
"Abstract datatype for specialized datasets."))
(defclass numeric-dataset (specialized-dataset)
((numeric-points
:initarg :numeric-points
:accessor dataset-numeric-points
:type (simple-array dvec (*))
:initform (error "Must specify points of the dataset.")))
(:documentation
"Dataset specialized in numeric values."))
(defmethod print-object ((d numeric-dataset) stream)
(call-next-method)
(format stream "~&NUMERIC DATA POINTS: ~A POINTS~%" (length (dataset-numeric-points d))))
;;@ function-type: (string) -> #(dvec) -> numeric-dataset
;;@ precondition:
;;@ - length of data > 0
;;@ - length of all-column-names > 0, zero dimension is meaningless
;;@ - dimensions of all-column-names = dimensions of a point
;;@ - data is of type (simple-array dvec (*)) (as what 'specialized' means)
(defun make-numeric-dataset (all-column-names specialized-data)
(assert (> (length specialized-data) 0))
(assert (> (length all-column-names) 0))
(assert (= (length all-column-names)
(length (aref specialized-data 0))))
(check-type specialized-data simple-vector)
(check-type (aref specialized-data 0) dvec)
(let ((dimensions (make-array (length all-column-names))))
(loop
for n in all-column-names
for i from 0
for d = (make-dimension n :numeric i)
do (setf (aref dimensions i) d))
(make-instance 'numeric-dataset
:dimensions dimensions
:numeric-points specialized-data)))
(defclass category-dataset (specialized-dataset)
((category-points
:initarg :category-points
:accessor dataset-category-points
:type (simple-array cvec (*))
:initform (error "Must specify points of the dataset.")))
(:documentation
"Dataset specialized in category values."))
(defmethod print-object ((d category-dataset) stream)
(call-next-method)
(format stream "~&CATEGORY DATA POINTS: ~A POINTS~%" (length (dataset-category-points d))))
;;@ function-type: (string) -> #(dvec) -> numeric-dataset
;;@ precondition:
;;@ - length of data > 0
;;@ - length of all-column-names > 0, zero dimension is meaningless
;;@ - dimensions of all-column-names = dimensions of a point
;;@ - data is of tyep (simple-array dvec (*)) (as what 'specialized' means)
(defun make-category-dataset (all-column-names specialized-data)
(assert (> (length specialized-data) 0))
(assert (> (length all-column-names) 0))
(assert (= (length all-column-names)
(length (aref specialized-data 0))))
(check-type specialized-data simple-vector)
(check-type (aref specialized-data 0) simple-array)
(let ((dimensions (make-array (length all-column-names))))
(loop
for n in all-column-names
for i from 0
for table = (make-hash-table :test 'equal #+allegro :values #+allegro nil)
for d = (make-dimension n :category i
:metadata `((:table . ,table)))
do (setf (aref dimensions i) d))
;; compact category values
(loop
for d across dimensions
do (loop
with i = (dimension-index d)
with table = (cdr (assoc :table (dimension-metadata d)))
for p across specialized-data
for c = (aref p i)
do (multiple-value-bind (val exist-p)
(gethash c table)
(if exist-p
(setf (aref p i) val)
(setf (gethash c table) #+allegro t #-allegro c)))))
(make-instance 'category-dataset
:dimensions dimensions
:category-points specialized-data)))
(defclass numeric-and-category-dataset (numeric-dataset category-dataset)
()
(:documentation
"Dataset specialized in both numeric and category values."))
;;@ function-type: (string) -> #(dvec) -> #(simple-vector) -> hash-table -> numeric-and-category-dataset
;;@ precondition:
;;@ - length of data > 0
;;@ - length of all-column-names > 0
;;@ - length of element of numeric-data = length of numeric-indices
;;@ - length of element of category-data = length of category-indices
;;@ - dimensions of all-column-names = dimensions of numeric-data + dimensions of category-data
;;@ - numeric-data is of type (simple-array dvec (*)), and category-data is of type (simple-array cvec (*))
(defun make-numeric-and-category-dataset (all-column-names numeric-data numeric-indices
category-data category-indices)
(assert (and (> (length all-column-names) 0)
(> (length numeric-data) 0)
(> (length category-data) 0)))
(assert (= (length all-column-names)
(+ (length numeric-indices)
(length category-indices))))
(check-type numeric-data simple-vector)
(check-type (aref numeric-data 0) dvec)
(check-type category-data simple-vector)
(check-type (aref category-data 0) simple-vector)
(let ((dimensions
(make-array (+ (length numeric-indices) (length category-indices)))))
(loop
for i from 0
for index in numeric-indices
for d = (make-dimension (nth index all-column-names) :numeric i)
do (setf (aref dimensions index) d))
(loop
for i from 0
for index in category-indices
for table = (make-hash-table :test 'equal #+allegro :values #+allegro nil)
for d = (make-dimension (nth index all-column-names) :category i
:metadata `((:table . ,table)))
do (setf (aref dimensions index) d))
;; compact category values
(loop
for d across dimensions
when (eq (dimension-type d) :category)
do (loop
with i = (dimension-index d)
with table = (cdr (assoc :table (dimension-metadata d)))
for p across category-data
for c = (aref p i)
do (multiple-value-bind (val exist-p)
(gethash c table)
(if exist-p
(setf (aref p i) val)
(setf (gethash c table) #+allegro t #-allegro c)))))
;; finally
(make-instance 'numeric-and-category-dataset
:dimensions dimensions
:numeric-points numeric-data
:category-points category-data)))
;;; dataset represented as multi-dimensional array
(defclass numeric-matrix-dataset (specialized-dataset)
((numeric-points
:initarg :numeric-points
:accessor dataset-numeric-points
:type dmat
:initform (error "Must specify points of the dataset.")))
(:documentation
"Dataset represented as matrix (2-dim CL array)"))
(defmethod print-object ((d numeric-matrix-dataset) stream)
(call-next-method)
(format stream "~&NUMERIC-MATRIX DATA POINTS: ~A POINTS~%" (array-dimension (dataset-numeric-points d) 0)))
;;@ function-type: string -> dmat -> numeric-matrix-dataset
;;@ precondition:
;;@ - dim_1 of data > 0
;;@ - length of all-column-names > 0, zero dimension is meaningless
;;@ - dimensions of all-column-names = dimensions of a point
;;@ - data is of type dmat (as what 'specialized' means)
(defun make-numeric-matrix-dataset (all-column-names specialized-matrix-data)
(assert (> (array-dimension specialized-matrix-data 0) 0))
(assert (> (length all-column-names) 0))
(assert (= (length all-column-names)
(array-dimension specialized-matrix-data 1)))
(check-type specialized-matrix-data dmat)
(let ((dimensions (make-array (length all-column-names))))
(loop
for n in all-column-names
for i from 0
for d = (make-dimension n :numeric i)
do (setf (aref dimensions i) d))
(make-instance 'numeric-matrix-dataset
:dimensions dimensions
:numeric-points specialized-matrix-data)))
(defclass numeric-matrix-and-category-dataset (numeric-matrix-dataset category-dataset)
()
(:documentation
"Dataset specialized in both numeric (as matrix) and category values."))
;;@ function-type: (string) -> #(dvec) -> #(simple-vector) -> hash-table -> numeric-matrix-and-category-dataset
;;@ precondition:
;;@ - nrow of data > 0
;;@ - length of all-column-names > 0
;;@ - length of element of numeric-data = length of numeric-indices
;;@ - length of element of category-data = length of category-indices
;;@ - dimensions of all-column-names = dimensions of numeric-data + dimensions of category-data
;;@ - numeric-data is of type dmat, and category-data is of type (simple-array cvec (*))
(defun make-numeric-matrix-and-category-dataset (all-column-names numeric-data numeric-indices
category-data category-indices)
(assert (and (> (length all-column-names) 0)
(> (array-dimension numeric-data 0) 0)
(> (length category-data) 0)))
(assert (= (length all-column-names)
(+ (length numeric-indices)
(length category-indices))))
(check-type numeric-data dmat)
(check-type category-data simple-vector)
(check-type (aref category-data 0) simple-vector)
(let ((dimensions
(make-array (+ (length numeric-indices) (length category-indices)))))
(loop
for i from 0
for index in numeric-indices
for d = (make-dimension (nth index all-column-names) :numeric i)
do (setf (aref dimensions index) d))
(loop
for i from 0
for index in category-indices
for table = (make-hash-table :test 'equal #+allegro :values #+allegro nil)
for d = (make-dimension (nth index all-column-names) :category i
:metadata `((:table . ,table)))
do (setf (aref dimensions index) d))
;; compact category values
(loop
for d across dimensions
when (eq (dimension-type d) :category)
do (loop
with i = (dimension-index d)
with table = (cdr (assoc :table (dimension-metadata d)))
for p across category-data
for c = (aref p i)
do (multiple-value-bind (val exist-p)
(gethash c table)
(if exist-p
(setf (aref p i) val)
(setf (gethash c table) #+allegro t #-allegro c)))))
;; finally
(make-instance 'numeric-matrix-and-category-dataset
:dimensions dimensions
:numeric-points numeric-data
:category-points category-data)))
(defmethod dataset-points ((dataset specialized-dataset))
(ecase (type-of dataset)
(numeric-and-category-dataset
(with-accessors ((c-ps dataset-category-points)
(n-ps dataset-numeric-points)
(dims dataset-dimensions)) dataset
(coerce
(loop for row below (length c-ps)
as vec = (make-array (length dims) :element-type t)
collect
(loop for dim across dims
for col from 0
as type = (dimension-type dim)
as index = (dimension-index dim)
do (setf (svref vec col)
(aref (svref (ecase type (:numeric n-ps) (:category c-ps)) row) index))
finally (return vec)))
'vector)))
(numeric-dataset (dataset-numeric-points dataset))
(category-dataset (dataset-category-points dataset))
((numeric-matrix-dataset numeric-matrix-and-category-dataset)
(error "It's fairly inefficient to do that, better keep the original unspecialized dataset."))))
;;;; read and process data
;;@ function-type: string -> unspecialized-dataset
(defun read-data-from-file (filename &key
(type :sexp)
(external-format :default external-format-p)
csv-type-spec
(csv-header-p t)
(missing-value-check t)
missing-values-list)
"Convention: first line is column name."
(assert (member type '(:sexp :csv)))
(ecase type
((:sexp nil)
(let (tmp)
(with-open-file (f filename :external-format external-format)
(with-standard-io-syntax
(let ((*read-eval* nil)
(*read-default-float-format* 'double-float))
(setf tmp (read f)))))
(make-unspecialized-dataset
(first tmp)
(map 'vector
(lambda (p)
(coerce p 'vector))
(rest tmp))
:missing-value-check missing-value-check
:missing-values-list missing-values-list)))
(:csv
(multiple-value-bind (data header)
(csv:read-csv-file filename :header csv-header-p :type-spec csv-type-spec
:external-format (if external-format-p external-format
#+allegro :932
#-allegro :sjis))
(make-unspecialized-dataset (coerce header 'list) data
:missing-value-check missing-value-check
:missing-values-list missing-values-list)))))
;;; function-type: unspecialized-dataset -> specialized-dataset
(defmethod pick-and-specialize-data ((d unspecialized-dataset) &key
(range :all)
except
data-types ; :numeric | :category
store-numeric-data-as-matrix
)
(assert (not (null data-types)))
(assert (every (lambda (type) (member type '(:numeric :category))) data-types))
(let* ((dimensions (dataset-dimensions d))
(total-size (length dimensions))
(range1 (if (eq range :all)
(loop for i below total-size collect i)
range))
(range (sort (set-difference range1 except) #'<)) ; destructive on range
(numeric-indices) ;indices in original dataset
(category-indices)
(numeric-indices-new) ;indices in new specialized dataset
(category-indices-new))
(assert (= (length range) (length data-types)))
(loop
for index in range
for i from 0
for dt in data-types
do (ecase dt
(:numeric
(push i numeric-indices-new)
(push index numeric-indices))
(:category
(push i category-indices-new)
(push index category-indices)))
finally (progn
(setf numeric-indices (nreverse numeric-indices))
(setf category-indices (nreverse category-indices))
(setf numeric-indices-new (nreverse numeric-indices-new))
(setf category-indices-new (nreverse category-indices-new))))
;;
(let ((all-column-names
(loop
with dimensions = (dataset-dimensions d)
for index in range
for name = (dimension-name (aref dimensions index))
collect name)))
(cond
;; numeric-dataset
((null category-indices-new)
(let* ((size (length numeric-indices-new))
(data
(map 'vector
(lambda (p) (declare (simple-vector p))
(let* ((sp (make-dvec size)))
(declare (type dvec sp))
(loop
for index of-type array-index in numeric-indices
for i of-type array-index from 0
as val = (aref p index)
do (setf (aref sp i)
(if (na-p val) *nan* (coerce val 'double-float)))
finally (return sp))))
(dataset-points d)))
(make-func (if store-numeric-data-as-matrix
#'make-numeric-matrix-dataset
#'make-numeric-dataset)))
(when store-numeric-data-as-matrix
(setf data (vecs2mat data)))
(funcall make-func all-column-names data)))
;; category-dataset
((null numeric-indices-new)
(let* ((size (length category-indices-new))
(data
(map 'vector
(lambda (p)
(declare (simple-vector p))
(let* ((sp (make-array size)))
(declare (type simple-vector sp))
(loop
for index of-type array-index in category-indices
for i of-type array-index from 0
as val = (aref p index)
do (setf (aref sp i) (if (na-p val) *c-nan* (aref p index)))
finally (return sp))))
(dataset-points d))))
(make-category-dataset all-column-names data)))
;; numeric and category dataset
(t
(let* ((numeric-data-size (length numeric-indices-new))
(category-data-size (length category-indices-new))
(numeric-data
(map 'vector
(lambda (p)
(declare (simple-vector p))
(let* ((sp (make-dvec numeric-data-size)))
(declare (type dvec sp))
(loop
for index of-type array-index in numeric-indices
for i of-type array-index from 0
as val = (aref p index)
do (setf (aref sp i)
(if (na-p val) *nan* (coerce (aref p index) 'double-float)))
finally (return sp))))
(dataset-points d)))
(category-data
(map 'vector
(lambda (p)
(declare (simple-vector p))
(let* ((sp (make-array category-data-size)))
(declare (type simple-vector sp))
(loop
for index of-type array-index in category-indices
for i of-type array-index from 0
as val = (aref p index)
do (setf (aref sp i) (if (na-p val) *c-nan* (aref p index)))
finally (return sp))))
(dataset-points d)))
(make-func (if store-numeric-data-as-matrix
#'make-numeric-matrix-and-category-dataset
#'make-numeric-and-category-dataset)))
(when store-numeric-data-as-matrix
(setf numeric-data (vecs2mat numeric-data)))
(funcall make-func
all-column-names
numeric-data
numeric-indices-new
category-data
category-indices-new)))))))
(defgeneric divide-dataset (dataset &key divide-ratio random range except)
(:documentation "Divide dataset and restrict column"))
(defmethod divide-dataset ((unsp-d unspecialized-dataset)
&key divide-ratio
random
(range :all)
except)
(unless divide-ratio (setq divide-ratio '(1 0)))
(assert (every (lambda (r) (and (numberp r) (integerp r) (not (minusp r)))) divide-ratio))
(let* ((dimensions (dataset-dimensions unsp-d))
(dim (length dimensions))
(row-indexes
(loop with total-size = (length (dataset-points unsp-d))
with row-list = (loop for i below total-size collect i)
with total-r = (apply #'+ divide-ratio)
for r in divide-ratio
as c = (floor (* total-size (/ r total-r)))
collect (loop repeat c
as pos = (if random (random (length row-list)) 0)
as val = (nth pos row-list)
collect (progn (setf row-list
(remove val row-list :test #'eql
:start pos :end (1+ pos)))
val)) into row-indexes
finally (return
(progn (when row-list
(mapcar (lambda (i) (push i (car (last row-indexes))))
row-list))
row-indexes))))
(range1 (if (eq range :all)
(loop for i below dim collect i)
(copy-seq range)))
(range (sort (set-difference range1 except) #'<)))
(assert (<= 1 (length range) (length dimensions)))
(let ((all-column-names
(loop for index in range
for name = (dimension-name (aref dimensions index))
collect name)))
(apply #'values
(loop for rows in row-indexes
as points = (map 'vector
(let ((pts (dataset-points unsp-d)))
(lambda (row)
(map 'vector
(lambda (i)
(let ((pt (svref pts row)))
(declare (type (simple-array t (*)) pt))
(svref pt i))) range)))
(sort rows #'<))
when (plusp (length points))
collect (make-unspecialized-dataset
all-column-names points :missing-value-check nil))))))
(defmethod divide-dataset ((specialized-d specialized-dataset)
&key divide-ratio
random
(range :all)
except)
(unless divide-ratio (setq divide-ratio '(1 0)))
(assert (every (lambda (r)
(and (numberp r) (integerp r) (not (minusp r)))) divide-ratio))
(let* ((dimensions (dataset-dimensions specialized-d))
(dim (length dimensions))
(row-indexes
(loop with total-size =
(length (etypecase specialized-d
(category-dataset (dataset-category-points specialized-d))
(numeric-dataset (dataset-numeric-points specialized-d))
(numeric-and-category-dataset (dataset-category-points specialized-d))))
with row-list = (loop for i below total-size collect i)
with total-r = (apply #'+ divide-ratio)
for r in divide-ratio
as c = (floor (* total-size (/ r total-r)))
collect (loop repeat c
as pos = (if random (random (length row-list)) 0)
as val = (nth pos row-list)
collect (progn (setf row-list
(remove val row-list :test #'eql
:start pos :end (1+ pos)))
val)) into row-indexes
finally (return
(progn (when row-list
(mapcar (lambda (i) (push i (car (last row-indexes))))
row-list))
row-indexes))))
(range1 (if (eq range :all)
(loop for i below dim collect i)
(copy-seq range)))
(range (sort (set-difference range1 except) #'<))
(numeric-indices) ;indices in original dataset
(category-indices)
(numeric-indices-new) ;indices in new specialized dataset
(category-indices-new))
(assert (<= 1 (length range) (length dimensions)))
(loop for index in range
for i from 0
for dt in (mapcar (lambda (n) (svref dimensions n)) range)
do (ecase (dimension-type dt)
(:numeric
(push i numeric-indices-new)
(push index numeric-indices))
(:category
(push i category-indices-new)
(push index category-indices)))
finally (progn
(setf numeric-indices (nreverse numeric-indices))
(setf category-indices (nreverse category-indices))
(setf numeric-indices-new (nreverse numeric-indices-new))
(setf category-indices-new (nreverse category-indices-new))))
(let ((all-column-names
(loop for index in range
for name = (dimension-name (aref dimensions index))
collect name)))
(apply #'values
(loop for rows in row-indexes
as points = (mapcar (let ((ps (dataset-points specialized-d)))
(lambda (r) (svref ps r)))
(sort rows #'<))
when points
collect
(cond
;; numeric-dataset
((null category-indices-new)
(let* ((size (length numeric-indices-new))
(data
(map 'vector
(lambda (p)
(let* ((sp (make-dvec size)))
(declare (type dvec sp))
(loop
for index of-type array-index in numeric-indices
for i of-type array-index from 0
as val = (aref p index)
do (setf (aref sp i)
(if (na-p val) *nan* (coerce val 'double-float)))
finally (return sp))))
points)))
(make-numeric-dataset all-column-names data)))
;; category-dataset
((null numeric-indices-new)
(let* ((size (length category-indices-new))
(data
(map 'vector
(lambda (p)
(let* ((sp (make-array size)))
(declare (type simple-vector sp))
(loop
for index of-type array-index in category-indices
for i of-type array-index from 0
as val = (aref p index)
do (setf (aref sp i) (if (na-p val) *c-nan* (aref p index)))
finally (return sp))))
points)))
(make-category-dataset all-column-names data)))
;; numeric and category dataset
(t
(let* ((numeric-data-size (length numeric-indices-new))
(category-data-size (length category-indices-new))
(numeric-data
(map 'vector
(lambda (p)
(let* ((sp (make-dvec numeric-data-size)))
(declare (type dvec sp))
(loop
for index of-type array-index in numeric-indices
for i of-type array-index from 0
as val = (aref p index)
do (setf (aref sp i)
(if (na-p val) *nan* (coerce (aref p index) 'double-float)))
finally (return sp))))
points))
(category-data
(map 'vector
(lambda (p)
(declare (simple-vector p))
(let* ((sp (make-array category-data-size)))
(declare (type simple-vector sp))
(loop
for index of-type array-index in category-indices
for i of-type array-index from 0
as val = (aref p index)
do (setf (aref sp i) (if (na-p val) *c-nan* (aref p index)))
finally (return sp))))
points)))
(make-numeric-and-category-dataset
all-column-names
numeric-data numeric-indices-new
category-data category-indices-new)))))))))
(defmethod copy-dataset ((dataset dataset))
(flet ((copy-dims (d) (map 'vector #'copy-dimension (dataset-dimensions d)))
(copy-pts (pts) (map 'vector #'copy-seq pts)))
(etypecase dataset
(numeric-and-category-dataset
(make-instance 'numeric-and-category-dataset
:dimensions (copy-dims dataset)
:numeric-points (copy-pts (dataset-numeric-points dataset))
:category-points (copy-pts (dataset-category-points dataset))))
(numeric-matrix-and-category-dataset
(make-instance 'numeric-matrix-and-category-dataset
:dimensions (copy-dims dataset)
:numeric-points (copy-mat (dataset-numeric-points dataset))
:category-points (copy-pts (dataset-category-points dataset))))
(numeric-dataset
(make-instance 'numeric-dataset
:dimensions (copy-dims dataset)
:numeric-points (copy-pts (dataset-numeric-points dataset))))
(numeric-matrix-dataset
(make-instance 'numeric-matrix-dataset
:dimensions (copy-dims dataset)
:numeric-points (copy-mat (dataset-numeric-points dataset))))
(category-dataset
(make-instance 'category-dataset
:dimensions (copy-dims dataset)
:category-points (copy-pts (dataset-category-points dataset))))
(unspecialized-dataset
(make-instance 'unspecialized-dataset
:dimensions (copy-dims dataset)
:points (copy-pts (dataset-points dataset)))))))
(defmethod choice-dimensions (names (data specialized-dataset))
(with-accessors ((dims dataset-dimensions)
(pts dataset-points)) data
(let* ((poses (loop for name in names
collect (position name dims :key #'dimension-name :test #'string=)))
(types (mapcar (lambda (pos) (dimension-type (aref dims pos))) poses))
(type (cond ((every (lambda (ty) (eq ty :numeric)) types) :numeric)
((every (lambda (ty) (eq ty :category)) types) :category)
(t t))))
(when poses
(loop for pt across pts
as new-pt = (mapcar (lambda (pos) (aref pt pos)) poses)
collect (case type
(:numeric (coerce new-pt 'dvec))
(:category (coerce new-pt 'vector)) ;;
(t (coerce new-pt 'vector))) into result
finally (return (coerce result 'vector)))))))
(defmethod choice-a-dimension (name (data specialized-dataset))
(multiple-value-bind (pos type)
(loop for pos from 0
for dim across (dataset-dimensions data)
when (string= (dimension-name dim) name)
return (values pos (dimension-type dim)))
(when pos
(loop for vec across (dataset-points data)
collect (aref vec pos) into result
finally (return (ecase type
(:numeric (coerce result 'dvec))
(:category (coerce result 'vector)) ;;
))))))
(defmethod make-bootstrap-sample (dataset)
(let* ((data-pts (dataset-points dataset))
(n (length data-pts))
(new-data-pts (make-array n :element-type t)))
(declare (type vector data-pts new-data-pts) (type fixnum n))
(loop for i of-type fixnum below n
do (setf (svref new-data-pts i)
(copy-seq (svref data-pts (the fixnum (random n))))))
new-data-pts))
(defmethod make-bootstrap-sample-datasets ((dataset dataset) &key (number-of-datasets 10))
(assert (and (integerp number-of-datasets) (> number-of-datasets 0)))
(flet ((copy-dims (d) (map 'vector #'copy-dimension (dataset-dimensions d)))
(pick-up-poses (arry poses) (mapcar (lambda (pos) (aref arry pos)) poses)))
(loop repeat number-of-datasets
as pts = (make-bootstrap-sample dataset)
collect
(etypecase dataset
(numeric-and-category-dataset
(make-instance 'numeric-and-category-dataset
:dimensions (copy-dims dataset)
:numeric-points
(map 'vector
(let ((poses (loop for pos from 0
for dim across (dataset-dimensions dataset)
when (eq :numeric (dimension-type dim)) collect pos)))
(lambda (pt) (coerce (pick-up-poses pt poses) 'dvec))) pts)
:category-points
(map 'vector
(let ((poses (loop for pos from 0
for dim across (dataset-dimensions dataset)
when (eq :category (dimension-type dim)) collect pos)))
(lambda (pt) (coerce (pick-up-poses pt poses) 'vector))) ;;
pts)))
(numeric-dataset (make-instance 'numeric-dataset
:dimensions (copy-dims dataset)
:numeric-points pts))
(category-dataset (make-instance 'category-dataset
:dimensions (copy-dims dataset)
:category-points pts))
(unspecialized-dataset (make-instance 'unspecialized-dataset
:dimensions (copy-dims dataset)
:points pts))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; dataset cleaning (outlier + interpolate) ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defconstant +known-outlier-types+
'(:numeric (:std-dev :mean-dev :user :smirnov-grubbs)
:category (:user :freq)))
(defconstant +known-interp-types+
'(:numeric (:zero :min :max :mean :median :spline)
:category (:mode)))
(defmacro outlier-points (points outlier-types outlier-values &key (type :numeric))
(let (tr-fcn vec-type)
(case type
(:numeric (setq tr-fcn 'transposeV vec-type 'dvec))
(:category (setq tr-fcn 'trans vec-type 'vector))
(t (error "invalid type | ~A" type)))
`(let ((tr-data (funcall ',tr-fcn ,points)))
(unless ,outlier-types (setq ,outlier-types
(make-list (length tr-data) :initial-element nil)))
(unless ,outlier-values (setq ,outlier-values
(make-list (length tr-data) :initial-element nil)))
(assert (every (lambda (val)
(or (null val) (member val (getf +known-outlier-types+ ,type))))
,outlier-types))
(assert (= (length tr-data) (length ,outlier-types) (length ,outlier-values)))
(funcall ',tr-fcn
(do-vec (vec tr-data :type ,vec-type :index-var i :setf-var sf :return tr-data)
(let ((outlier-type (elt ,outlier-types i))
(value (elt ,outlier-values i)))
(when outlier-type
(setf sf (outlier-verification
vec :type outlier-type :outlier-value value :seq-type ,type)))))))))
(defmacro interp-points (points interp-types &key (type :numeric))
(let (tr-fcn vec-type)
(case type
(:numeric (setq tr-fcn 'transposeV vec-type 'dvec))
(:category (setq tr-fcn 'trans vec-type 'vector))
(t (error "invalid type | ~A" type)))
`(let ((tr-data (funcall ',tr-fcn ,points)))
(unless ,interp-types (setq ,interp-types (make-list (length tr-data) :initial-element nil)))