source: orange/source/orange/libsvm_interface.hpp @ 11703:9b8d8ab7820c

Revision 11703:9b8d8ab7820c, 4.5 KB checked in by janezd <janez.demsar@…>, 7 months ago (diff)

Removed the GPL copyright notice from all files except orangeqt.

Line 
1#ifndef __SVM_HPP
2#define __SVM_HPP
3
4#include "table.hpp"
5
6#include "classify.hpp"
7#include "learn.hpp"
8#include "orange.hpp"
9#include "domain.hpp"
10#include "examplegen.hpp"
11#include "table.hpp"
12#include "examples.hpp"
13#include "distance.hpp"
14
15#include "libsvm/svm.h"
16
17svm_model *svm_load_model_alt(string& buffer);
18int svm_save_model_alt(string& buffer, const svm_model *model);
19
20WRAPPER(ExampleGenerator)
21WRAPPER(KernelFunc)
22WRAPPER(SVMLearner)
23WRAPPER(SVMClassifier)
24WRAPPER(ExampleTable)
25
26class ORANGE_API TKernelFunc: public TOrange{
27public:
28    __REGISTER_ABSTRACT_CLASS
29    virtual float operator()(const TExample &, const TExample &)=0;
30};
31
32WRAPPER(KernelFunc)
33
34
35class ORANGE_API TSVMLearner : public TLearner{
36public:
37    __REGISTER_CLASS
38
39  CLASSCONSTANTS(SVMType: C_SVC=C_SVC; Nu_SVC=NU_SVC; OneClass=ONE_CLASS; Epsilon_SVR=EPSILON_SVR; Nu_SVR=NU_SVR)
40  CLASSCONSTANTS(Kernel: Linear=LINEAR; Polynomial=POLY; RBF=RBF; Sigmoid=SIGMOID; Custom=PRECOMPUTED)
41  CLASSCONSTANTS(LIBSVM_VERSION: VERSION=LIBSVM_VERSION)
42
43    //parameters
44    int svm_type; //P(&SVMLearner_SVMType)  SVM type (C_SVC=0, NU_SVC, ONE_CLASS, EPSILON_SVR=3, NU_SVR=4)
45    int kernel_type; //P(&SVMLearner_Kernel)  kernel type (LINEAR=0, POLY, RBF, SIGMOID, CUSTOM=4)
46    float degree;   //P polynomial kernel degree
47    float gamma;    //P poly/rbf/sigm parameter
48    float coef0;    //P poly/sigm parameter
49    float cache_size; //P cache size in MB
50    float eps;  //P stopping criteria
51    float C;    //P for C_SVC and C_SVR
52    float nu;   //P for NU_SVC and ONE_CLASS
53    float p;    //P for C_SVR
54    int shrinking;  //P shrinking
55    int probability;    //P probability
56    bool verbose;       //P verbose
57
58    int nr_weight;      /* for C_SVC */
59    int *weight_label;  /* for C_SVC */
60    double* weight;     /* for C_SVC */
61
62    PKernelFunc kernelFunc; //P custom kernel function
63
64    TSVMLearner();
65    ~TSVMLearner();
66
67    PClassifier operator()(PExampleGenerator, const int & = 0);
68
69protected:
70    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, double last=0.0);
71    virtual svm_node* init_problem(svm_problem &problem, PExampleTable examples, int n_elements);
72    virtual int getNumOfElements(PExampleGenerator examples);
73    virtual TSVMClassifier* createClassifier(
74                PDomain domain, svm_model* model, PExampleTable supportVectors, PExampleTable examples);
75};
76
77class ORANGE_API TSVMLearnerSparse : public TSVMLearner{
78public:
79    __REGISTER_CLASS
80    bool useNonMeta; //P include non meta attributes in the learning process
81protected:
82    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, double last=0.0);
83    virtual int getNumOfElements(PExampleGenerator examples);
84    virtual TSVMClassifier* createClassifier(
85            PDomain domain, svm_model* model, PExampleTable supportVectors, PExampleTable examples);
86};
87
88
89class ORANGE_API TSVMClassifier : public TClassifierFD {
90public:
91    __REGISTER_CLASS
92    TSVMClassifier() {
93        this->model = NULL;
94    };
95
96    TSVMClassifier(PDomain, svm_model * model, PExampleTable supportVectors,
97            PKernelFunc kernelFunc=NULL, PExampleTable examples=NULL);
98
99    ~TSVMClassifier();
100
101    TValue operator()(const TExample&);
102    PDistribution classDistribution(const TExample &);
103
104    PFloatList getDecisionValues(const TExample &);
105
106    PIntList nSV; //P nSV
107    PFloatList rho; //P rho
108    PFloatListList coef; //P coef
109    PFloatList probA; //P probA - pairwise probability information
110    PFloatList probB; //P probB - pairwise probability information
111    PExampleTable supportVectors; //P support vectors
112
113    PExampleTable examples; //P training instances when svm_type == Custom
114    PKernelFunc kernelFunc; //P custom kernel function used when svm_type == Custom
115
116    int svm_type; //P(&SVMLearner_SVMType)  SVM type (C_SVC=0, NU_SVC, ONE_CLASS, EPSILON_SVR=3, NU_SVR=4)
117    int kernel_type; //P(&SVMLearner_Kernel)  kernel type (LINEAR=0, POLY, RBF, SIGMOID, CUSTOM=4)
118
119    svm_model* getModel() {return model;}
120
121protected:
122    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, double last=0.0);
123    virtual int getNumOfElements(const TExample& example);
124
125private:
126    svm_model *model;
127};
128
129class ORANGE_API TSVMClassifierSparse : public TSVMClassifier {
130public:
131    __REGISTER_CLASS
132    TSVMClassifierSparse() {};
133
134    TSVMClassifierSparse(
135            PDomain domain, svm_model * model, bool useNonMeta,
136            PExampleTable supportVectors,
137            PKernelFunc kernelFunc=NULL,
138            PExampleTable examples=NULL
139            ) : TSVMClassifier(domain, model, supportVectors, kernelFunc, examples) {
140        this->useNonMeta = useNonMeta;
141    }
142
143    bool useNonMeta; //PR include non meta attributes
144
145protected:
146    virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, double last=0.0);
147    virtual int getNumOfElements(const TExample& example);
148};
149
150#endif
151
Note: See TracBrowser for help on using the repository browser.