Changeset 20834 in project


Ignore:
Timestamp:
10/14/10 00:31:20 (11 years ago)
Author:
petercrlane
Message:

added make-problem

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/libsvm

    r20791 r20834  
    99
    1010=== Procedures
     11
     12<procedure>(make-problem definition)</procedure>
     13Returns a pointer to an instance of {{svm_problem}}, created using the given {{definition}}.  The definition can be in one of two forms.  In both forms, the definition is a list of instances.  The instance description is one of:
     14* a label followed by numeric values for each of the features; or
     15* a label followed by a list of {{(feature-index value)}} pairs.  (NOT YET)
    1116
    1217<procedure>(make-svm-node index value)</procedure>
     
    119124 Proportion correct: 85.7%
    120125
     126The following example shows how to construct a problem set from individual data values:
     127<enscript>
     128;;; This program illustrates how to construct a problem set for use in libsvm
     129;;; Written by Peter Lane, 2010
     130
     131(require-extension srfi-1)
     132(require-extension srfi-42)
     133(require-extension format)
     134(require-extension libsvm)
     135
     136(format #t "Classification with LIBSVM~&")
     137(format #t "--------------------------~&")
     138 
     139;; Sample dataset: the 'Play Tennis' dataset
     140;; from T. Mitchell, Machine Learning (1997)
     141;; --------------------------------------------
     142;; Labels for each instance in the training set
     143;;    1 = Play, 0 = Not
     144
     145(define labels '(0 0 1 1 1 0 1 0 1 1 1 1 1 0))
     146
     147;; Recoding the attribute values into range [0, 1]
     148(define instances '((0.0 1.0 1.0 0.0)
     149                    (0.0 1.0 1.0 1.0)
     150                    (0.5 1.0 1.0 0.0)
     151                    (1.0 0.5 1.0 0.0)
     152                    (1.0 0.0 0.0 0.0)
     153                    (1.0 0.0 0.0 1.0)
     154                    (0.5 0.0 0.0 1.0)
     155                    (0.0 0.5 1.0 0.0)
     156                    (0.0 0.0 0.0 0.0)
     157                    (1.0 0.5 0.0 0.0)
     158                    (0.0 0.5 0.0 1.0)
     159                    (0.5 0.5 1.0 1.0)
     160                    (0.5 1.0 0.0 0.0)
     161                    (.0 0.5 1.0 1.0)))
     162
     163;; create some arbitrary train/test split
     164(define training-labels (take labels 10))
     165(define training-instances (take instances 10))
     166(define test-labels (drop labels 10))
     167(define test-instances (drop instances 10))
     168
     169;; pair the instances with their labels in required form for making problem set
     170(define training-problem
     171  (make-problem (map cons training-labels training-instances)))
     172(define test-problem
     173  (make-problem (map cons test-labels test-instances)))
     174
     175(define (report-results svm-model problem)
     176  ;; display instances with actual and predicted class
     177  (define *correct*
     178    (sum-ec (: i (problem-num-instances problem))
     179            (begin
     180              (let ((actual (problem-get-label problem i))
     181                    (predicted (svm-predict svm-model (problem-get-instance problem i))))
     182                (format #t "Instance ~d: values ~a, class ~2d, predicted ~f ~a~&"
     183                        (+ 1 i)
     184                        (problem-get-instance-values (problem-get-instance problem i))
     185                        actual
     186                        predicted
     187                        (if (= actual predicted)
     188                          "Y"
     189                          ""))
     190                ;; return a '1' for each correct prediction
     191                (if (= actual predicted) 1 0)))))
     192
     193  ;; display summary of performance
     194  (format #t "Proportion correct: ~4,1f%~&"
     195          (/ (* 100 *correct*) (problem-num-instances problem))))
     196
     197(for-each ;; train and test a model for each type of kernel
     198  (lambda (kernel name)
     199    (let* ((parameters (make-svm-parameter kernel-type: kernel
     200                                           svm-type: NU-SVC
     201                                           degree: 1
     202                                           gamma: 100.0
     203                                           C: 10))
     204           (model (svm-train training-problem parameters)))
     205      (format #t "Kernel: ~a~&" name)
     206      (format #t "Results on training set~&")
     207      (report-results model training-problem)
     208      (format #t "Results on test set~&")
     209      (report-results model test-problem)))
     210  (list LINEAR POLY RBF SIGMOID)
     211  (list "Linear" "Polynomial" "Radial-Basis Function" "Sigmoid"))
     212</enscript>
     213
    121214=== Author
    122215
Note: See TracChangeset for help on using the changeset viewer.