(defstruct (abcd (:print-function abcd-print)) for (a 0) (b 0) (c 0) (d 0)) (labels ((a (x) (abcd-a x)) (b (x) (abcd-b x)) (c (x) (abcd-c x)) (d (x) (abcd-d x))) (defun pd (x) (if (zerop (d x)) 0 (/ (d x) (+ (b x) (d x))))) (defun pf (x) (if (zerop (c x)) 0 (/ (c x) (+ (a x) (c x))))) (defun all (x) (+ (a x) (b x) (c x) (d x))) (defun recall (x) (pd x)) (defun precision (x) (if(zerop (d x)) 0 (/ (d x) (+ (c x)(d x))))) (defun accuracy (x) (if (and (zerop (a x)) (zerop (d x))) 0 (/ (+ (a x) (d x)) (all x)))) (defun f-measure (x) (if (or (zerop (a x)) (zerop (d x))) 0 (harmonic-mean (pd x) (pf x)))) (defun balance (x &optional (goalpd 1) (goalpf 0)) (- 1 (/ (sqrt (+ (expt (- goalpf (pf x)) 2) (expt (- goalpd (pd x)) 2))) (sqrt 2)))) ) (defun abcd-stats (pairs &key (verbose t)) "stats from cons of (want . got)" (let* ((h (make-hash-table :test #'equal)) classes out) (dolist (pair pairs) (unless (member (first pair) classes) (push (first pair) classes)) (unless (member (rest pair) classes) (push (rest pair) classes)) (incf (gethash pair h 0))); while here, collect abcd-stats (if verbose (abcd-matrix pairs classes h)) (dolist (class classes out) (unless (null class) (let ((abcd (make-abcd :for class))) (maphash ; for each item in the hash counts, do #'(lambda (pair count) (abcd-stat (first pair) (rest pair) count class h abcd)) h) (push abcd out)))))) (defun abcd-stat (want got count goal h abcd) (if (eql got goal) (if (eql want goal) (incf (abcd-d abcd) count) (incf (abcd-c abcd) count)) (if (eql want goal) (incf (abcd-b abcd) count) (incf (abcd-a abcd) count))))