Changeset 20785 in project


Ignore:
Timestamp:
10/11/10 01:57:06 (11 years ago)
Author:
petercrlane
Message:

documentation for first example

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/libsvm

    r20781 r20785  
    1717<procedure>(make-svm-parameter  #!key (svm-type C-SVC) (kernel-type LINEAR)  (degree 0) (gamma 0.0) (coef0 0.0)   (cache-size 0.0) (eps 0.001) (C 0.0) (nr-weight 0) (nu 0.0) (p 0.0) (shrinking 0) (probability 0))</procedure>
    1818Returns a pointer to an instance of {{svm_parameter}}.
     19
     20<procedure>(problem-get-instance problem index)</procedure>
     21Return a pointer to the data which defines the indexed instance in problem set.
     22
     23<procedure>(problem-get-instance-values instance)</procedure>
     24Return a list of (index value) pairs for the data defining the given instance.
    1925
    2026<procedure>(problem-get-label problem index)</procedure>
     
    6167=== Examples
    6268
     69The following example uses a file from libsvm [[http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/|examples]]; the example, along with others, is also available in the svn repository for this egg.  This script reads in the datafile, constructs a model, and then reports all details of the dataset and performance.
     70
     71<enscript>
     72(require-extension format)
     73(require-extension libsvm)
     74(require-extension srfi-42)
     75
     76;; read in a sample dataset in svm-light format
     77(define problem (read-problem "australian_scale.txt"))
     78
     79(format #t "Problem has ~d instances~&"
     80       (problem-num-instances problem))
     81
     82;; create a model from dataset - uses default parameters
     83(define svm-model (svm-train problem (make-svm-parameter)))
     84
     85;; display instances with actual and predicted class
     86(define *correct*
     87  (sum-ec (: i (problem-num-instances problem))
     88          (begin
     89            (let ((actual (problem-get-label problem i))
     90                  (predicted (svm-predict svm-model (problem-get-instance problem i))))
     91              (format #t "Instance ~d: values ~a, class ~2d, predicted ~f ~a~&"
     92                      (+ 1 i)
     93                      (problem-get-instance-values (problem-get-instance problem i))
     94                      actual
     95                      predicted
     96                      (if (= actual predicted)
     97                        "Y"
     98                        ""))
     99              ;; return a '1' for each correct prediction
     100              (if (= actual predicted) 1 0)))))
     101
     102;; display summary of performance
     103(format #t "Proportion correct: ~4,1f%~&"
     104        (/ (* 100 *correct*) (problem-num-instances problem)))
     105</enscript>
     106
     107Output:
     108
     109 Problem has 690 instances
     110 .......*......*
     111 optimization finished, #iter = 9453
     112 nu = 0.290834
     113 obj = -199.649812, rho = 1.025147
     114 nSV = 210, nBSV = 193
     115 Total nSV = 210
     116 Instance 1: values ((1 1.0) (2 -0.749474) (3 -0.181429) (5 -0.538462) (6 -0.25) (7 -0.888772) (8 -1.0) (9 -1.0)
     117 (10 -1.0)  (11 1.0) (13 -0.9) (14 -0.97576)), class -1, predicted -1.0 Y
     118 . . . INFORMATION FOR OTHER INSTANCES . . .
     119 Instance 690: values ((1 1.0) (2 -0.180451) (3 -0.997143) (5 0.384615) (6 -0.25) (7 -0.997193) (8 -1.0) (9 1.0)
     120 (10 -0.970149) (11 -1.0) (12 -1.0) (13 -0.44) (14 -1.0)), class  1, predicted -1.0
     121 Proportion correct: 85.7%
    63122
    64123=== Author
     
    72131=== Requirements
    73132
    74 
     133You must have the libsvm library installed.
    75134
    76135=== Version History
Note: See TracChangeset for help on using the changeset viewer.