;;; This program illustrates how to construct a problem set for use in libsvm ;;; Written by Peter Lane, 2010 (require-extension srfi-1) (require-extension srfi-42) (require-extension format) (require-extension libsvm) (format #t "Classification with LIBSVM~&") (format #t "--------------------------~&") ;; Sample dataset: the 'Play Tennis' dataset ;; from T. Mitchell, Machine Learning (1997) ;; -------------------------------------------- ;; Labels for each instance in the training set ;; 1 = Play, 0 = Not (define labels '(0 0 1 1 1 0 1 0 1 1 1 1 1 0)) ;; Recoding the attribute values into range [0, 1] (define instances '((0.0 1.0 1.0 0.0) (0.0 1.0 1.0 1.0) (0.5 1.0 1.0 0.0) (1.0 0.5 1.0 0.0) (1.0 0.0 0.0 0.0) (1.0 0.0 0.0 1.0) (0.5 0.0 0.0 1.0) (0.0 0.5 1.0 0.0) (0.0 0.0 0.0 0.0) (1.0 0.5 0.0 0.0) (0.0 0.5 0.0 1.0) (0.5 0.5 1.0 1.0) (0.5 1.0 0.0 0.0) (.0 0.5 1.0 1.0))) ;; create some arbitrary train/test split (define training-labels (take labels 10)) (define training-instances (take instances 10)) (define test-labels (drop labels 10)) (define test-instances (drop instances 10)) (define training-problem (make-problem (map cons training-labels training-instances))) (define test-problem (make-problem (map cons test-labels test-instances))) (define (report-results svm-model problem) ;; display instances with actual and predicted class (define *correct* (sum-ec (: i (problem-num-instances problem)) (begin (let ((actual (problem-get-label problem i)) (predicted (svm-predict svm-model (problem-get-instance problem i)))) (format #t "Instance ~d: values ~a, class ~2d, predicted ~f ~a~&" (+ 1 i) (problem-get-instance-values (problem-get-instance problem i)) actual predicted (if (= actual predicted) "Y" "")) ;; return a '1' for each correct prediction (if (= actual predicted) 1 0))))) ;; display summary of performance (format #t "Proportion correct: ~4,1f%~&" (/ (* 100 *correct*) (problem-num-instances problem)))) (for-each (lambda (kernel name) (let* ((parameters (make-svm-parameter kernel-type: kernel svm-type: NU-SVC degree: 1 gamma: 100.0 C: 10)) (model (svm-train training-problem parameters))) (format #t "Kernel: ~a~&" name) (format #t "Results on training set~&") (report-results model training-problem) (format #t "Results on test set~&") (report-results model test-problem))) (list LINEAR POLY RBF SIGMOID) (list "Linear" "Polynomial" "Radial-Basis Function" "Sigmoid"))