(setf *data* ;;; id major-op family old sent.home? '((d1 yes no no no) (d2 yes no yes no) (d3 no no no yes) (d4 no no yes no) (d5 no yes yes yes))) (setf *training* nil) (defun setup () (loop for d in *data* do (setf *training* (cons (first d) *training*)) (loop for attribute in '(major-op family old sent.home?) as value in (cdr d) do (setf (get (first d) attribute) value)))) (defun partition (attribute instances) (let (result vlist v) ;this puts nil into result, vlist, v (loop for e in instances do (setq v (get e attribute)) (if (setq vlist (assoc v result)) (rplacd vlist (cons e (cdr vlist))) (setq result (cons (list v e) result)))) (cons attribute result))) (defun choose.best.partition (target.attribute partitions) (let ((lowest.exp.entropy 9999) exp.entropy best.partition) (loop for att.partition in partitions do (when (< (setq exp.entropy (expected.entropy target.attribute (cdr att.partition))) lowest.exp.entropy) (print 'exp.entropy)(princ exp.entropy) (setq lowest.exp.entropy exp.entropy) (setq best.partition att.partition))) best.partition)) (defun expected.entropy (att partition) (print 'partition) (princ partition) (loop for p in partition sum (* (length (cdr p)) (loop for v in (get att 'legal.values) sum (let ((vcount (loop for e in (cdr p) count (eq v (get e att)))) proportion) (setq proportion (/ vcount (length (cdr p)))) (* -1.0 proportion (if (zerop proportion) 0 (log proportion 2))))))))