;;;; svm-draw.scm ;;;; requires: draw.scm ;;;; requires: svmmodel.scm (declare (usual-integrations)) ;; set the sample points to be plotted to the training samples for 'svmmodel' (define (set-samples svmmodel) (set! *sample-points* (svmmodel-sample-map svmmodel))) ;; not used, generally - divide every value in the matrix by the max value. (define (normalize-matrix m) (let ((gmax (matrix-max m))) (let ((tweak-row (lambda (r) (map (lambda (s) (/ s gmax)) r)))) (map tweak-row m)))) ;; find the maximum value in a matrix (define (matrix-max m) (define (row-max r) (max (reduce max 0. r) (- (reduce min 0. r)))) (reduce max 0. (map row-max m))) ;; return a color matrix for 'draw' based on the output matrix 'sm'. ;; this can be generated by (svm-matrix svmmodel). (define (color-svm-matrix sm) (define (adjust-matrix m mmax) (define (color-point output) (if (or (< (abs (- output 1)) 0.2) (< (abs (+ output 1)) 0.2)) '(114 114 114) (let ((offset (round->exact (/ (* output 255) mmax)))) (if (> offset 0) (list (- 255 offset) (- 255 offset) 255) (list 255 (+ 255 offset) (+ 255 offset)))))) (define (color-row sr) (map color-point sr)) (map color-row m)) (adjust-matrix sm (matrix-max sm))) ;; returns a matrix of output values for svmmodel. ;; The range and resolution are dictated by *draw-max-x*, *draw-min-x*, ;; *draw-max-y*, *draw-min-y*, *draw-window-width*, *draw-window-height*. (define (svm-matrix svmmodel) (let ((fm (function-matrix (lambda (x y) (svmmodel-output svmmodel (new-unknown x y)))))) (set-samples svmmodel) fm)) (define (draw-svm svmmodel) (svmmodel-learn svmmodel) (draw (color-svm-matrix (svm-matrix svmmodel))))