; procedures to generate synthetic (linearly separable) data for ; perceptron training. ; ; assume all our data will be in the range [-1,1) ; ; produce "n-points" points of "dim" dimensional linearly separable data ; ; choose the hyperplane to divide the data by picking a random ; direction and then choose a random number between -1 and 1 to offset ; the hyperplane from the origin. ; (define (pick-hyperplane dim) (list (random-direction dim) (- (random 2.0) 1))) (define (lsdata hyperplane points) (let ((normal (car hyperplane)) (offset (cadr hyperplane)) (dim (length (car hyperplane)))) (define (get-pts pts n pos) (cond ((= n points) (print "Of " n " examples, " pos " are positive examples.\n") pts) (else (let* ((p (random-point dim)) (c (if (> (dot-product p normal) offset) 1 -1))) (get-pts (cons (list c p) pts) (+ n 1) (if (= c 1) (+ pos 1) pos)))))) (get-pts '() 0 0))) ; produce a random N dimensional point in the N dimensional unit hypercube ; (define (random-point N) (if (zero? N) '() (cons (- (random 2.0) 1) (random-point (- N 1))))) ; produce an N dimensional unit vector with a random direction ; ; pick a random vector in the unit cube, discard if not in the unit ; circle, and then normalize it. ; (define (random-direction N) (let* ((v (random-point N)) (m (magnitude v))) (if (> m 1.0) (random-direction N) (map (lambda (x) (/ x m)) v)))) (define (magnitude v) (sqrt (dot-product v v))) (define (dot-product a b) (apply + (map * a b))) ; print (using display) all the arguments to this procedure ; (define (print . stuff) (if (not (null? stuff)) (begin (display (car stuff)) (apply print (cdr stuff)))))