You are looking at historical revision 20785 of this page. It may differ significantly from its current revision.

libsvm

In progress.

An interface to the libsvm library for creating and using Support Vector Machines.

Constants

svm_type
C-SVC NU-SVC ONE-CLASS EPSILON-SVR NU-SVR
kernel_type
LINEAR POLY RBF SIGMOID PRECOMPUTED

Procedures

[procedure] (make-svm-node index value)

Returns a pointer to an instance of svm_node.

[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))

Returns a pointer to an instance of svm_parameter.

[procedure] (problem-get-instance problem index)

Return a pointer to the data which defines the indexed instance in problem set.

[procedure] (problem-get-instance-values instance)

Return a list of (index value) pairs for the data defining the given instance.

[procedure] (problem-get-label problem index)

Returns the label for the indexed instance in problem set.

<procedure>(problem-num-instances problem)(/procedure> Returns the number of instances in the given problem.

[procedure] (read-problem filename)

Returns a pointer to an instance of svm_problem for problem read from filename.

[procedure] (svm-check-parameter problem parameter)

Returns #f if parameter set is ok, else a string giving description of error.

[procedure] (svm-check-probability-model model)
[procedure] (svm-destroy model-array)
[procedure] (svm-destroy-param parameter)
[procedure] (svm-free-model-content model)
[procedure] (svm-get-nr-class model)

Returns the number of classes for given model.

[procedure] (svm-get-svm-type model)

Returns the svm-type of the given model.

[procedure] (svm-get-svr-probability model)

Returns the SVR probability from given model.

[procedure] (svm-load-model filename)

Reads model definition from given filename and returns a pointer to the model.

[procedure] (svm-predict model svm-node-array)

Returns prediction of model for given svm-node-array.

[procedure] (svm-save-model filename model)

Saves given model to a file called filename. Returns 0 if OK, else 1 if error.

[procedure] (svm-train problem parameter)

Returns a pointer an SVM model trained using given problem and parameter set.

Examples

The following example uses a file from libsvm 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.

(require-extension format)
(require-extension libsvm)
(require-extension srfi-42)

;; read in a sample dataset in svm-light format
(define problem (read-problem "australian_scale.txt"))

(format #t "Problem has ~d instances~&"
       (problem-num-instances problem))

;; create a model from dataset - uses default parameters
(define svm-model (svm-train problem (make-svm-parameter)))

;; 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)))

Output:

Problem has 690 instances
.......*......*
optimization finished, #iter = 9453
nu = 0.290834
obj = -199.649812, rho = 1.025147
nSV = 210, nBSV = 193
Total nSV = 210
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) 
(10 -1.0)  (11 1.0) (13 -0.9) (14 -0.97576)), class -1, predicted -1.0 Y
. . . INFORMATION FOR OTHER INSTANCES . . . 
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) 
(10 -0.970149) (11 -1.0) (12 -1.0) (13 -0.44) (14 -1.0)), class  1, predicted -1.0
Proportion correct: 85.7%

Author

Peter Lane.

License

GPL version 3.0.

Requirements

You must have the libsvm library installed.

Version History