Changeset 20835 in project


Ignore:
Timestamp:
10/14/10 00:32:35 (11 years ago)
Author:
petercrlane
Message:

added 'make-problem' + example

Location:
release/4/libsvm
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • release/4/libsvm/trunk/libsvm.scm

    r20789 r20835  
    2222    C-SVC NU-SVC ONE-CLASS EPSILON-SVR NU-SVR
    2323    LINEAR POLY RBF SIGMOID PRECOMPUTED
     24    make-problem
    2425    make-svm-node
    2526    make-svm-parameter
     
    6263  (define PRECOMPUTED 4)
    6364
     65  ;; problem definition should be a list of lists,
     66  ;; each sublist defining an instance, in one of following formats:
     67  ;; - label feature-value-0 feature-value-1 ...
     68  ;; - label (index value) (index value) ...      (TODO)
     69  (define (make-problem definition)
     70    (let ((problem (create-svm-problem (length definition) (- (length (car definition)) 1)))
     71          (i 0))
     72      (for-each (lambda (instance)
     73                  (problem-set-label! problem i (exact->inexact (car instance)))
     74                  (let ((j 0))
     75                    (for-each (lambda (value)
     76                                (problem-set-value! problem i j (exact->inexact value))
     77                                (set! j (+ 1 j)))
     78                              (cdr instance))
     79                    (problem-set-end-value! problem i j))
     80                  (set! i (+ 1 i)))
     81                definition)
     82      problem))
     83
     84  (define create-svm-problem
     85    (foreign-lambda* (c-pointer (struct "svm_problem")) ((int num_instances) (int num_features))
     86                     "int i;
     87                     struct svm_problem * problem = malloc(sizeof(struct svm_problem));
     88                     problem->l = num_instances;
     89                     problem->x = malloc(sizeof(struct svm_node *) * num_instances);
     90                     struct svm_node * x_space;
     91                     x_space = malloc(sizeof(struct svm_node) * (1 + num_features) * num_instances);
     92                     for (i = 0; i < problem->l; ++i)
     93                     {
     94                       problem->x[i] = &x_space[i * (1 + num_features)];
     95                     }
     96                     problem->y = malloc(sizeof(double) * num_instances);
     97                     C_return(problem);"))
     98
     99  (define problem-set-label!
     100    (foreign-lambda* void (((c-pointer (struct "svm_problem")) problem) (int index) (double value))
     101                     "problem->y[index] = value;"))
     102
     103(define problem-set-value!
     104    (foreign-lambda* void (((c-pointer (struct "svm_problem")) problem) (int index) (int feature) (double value))
     105                     "problem->x[index][feature].index = feature;
     106                     problem->x[index][feature].value = value;"))
     107
     108  (define problem-set-end-value!
     109    (foreign-lambda* void (((c-pointer (struct "svm_problem")) problem) (int index) (int feature))
     110                     "problem->x[index][feature].index = -1;
     111                     problem->x[index][feature].value = 0.0;"))
     112 
    64113  (define make-svm-node
    65114    (foreign-lambda* (c-pointer (struct "svm_node")) ((int index) (double value))
Note: See TracChangeset for help on using the changeset viewer.