-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfeature-extraction-and-preprocessing.lisp
45 lines (35 loc) · 1.4 KB
/
feature-extraction-and-preprocessing.lisp
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
;;;; Martin Kersner, [email protected]
;;;; 2016/08/23
;;; Create single one hot encoding vector.
(defun create-one-hot (len pos)
(let* ((tmp-one-hot (make-list len :initial-element 0)))
(setf (nth pos tmp-one-hot) 1)
tmp-one-hot))
(defun make-uniq (lst &optional (is-uniq nil))
(if is-uniq
lst
(remove-duplicates lst :test 'equal)))
;;; Create one hot encoding from given categorical values.
(defun one-hot-encoding (lst &optional (is-uniq nil))
(let* ((set-lst (make-uniq lst is-uniq))
(len-lst (length set-lst))
(ht (make-hash-table :test 'equal))
(tmp-one-hot nil))
(mapcar #'(lambda (idx item) (progn
(setf tmp-one-hot (create-one-hot len-lst idx))
(setf (gethash item ht) tmp-one-hot)))
(iota len-lst) set-lst)
ht))
;;; Apply one hot encoding on given categories.
(defun apply-hash-table-on-list (ht lst)
(mapcar #'(lambda (item) (gethash item ht)) lst))
;;; Create hash table where *keys* are unique items from given list
;;; and *values* are ascending numbers counted from 0.
(defun unique-numbers (lst &optional (is-uniq nil))
(let ((set-lst (make-uniq lst is-uniq))
(ht (make-hash-table :test 'equal)))
(mapcar #'(lambda (idx item)
(setf (gethash item ht) idx))
(iota (length set-lst))
set-lst)
ht))