org.opencv.ml
public class CvSVM extends CvStatModel
Support Vector Machines.
Note:
Modifier and Type | Field and Description |
---|---|
static int |
C |
static int |
C_SVC |
static int |
COEF |
static int |
DEGREE |
static int |
EPS_SVR |
static int |
GAMMA |
static int |
LINEAR |
static int |
NU |
static int |
NU_SVC |
static int |
NU_SVR |
static int |
ONE_CLASS |
static int |
P |
static int |
POLY |
static int |
RBF |
static int |
SIGMOID |
Constructor and Description |
---|
CvSVM()
Default and training constructors.
|
CvSVM(Mat trainData,
Mat responses)
Default and training constructors.
|
CvSVM(Mat trainData,
Mat responses,
Mat varIdx,
Mat sampleIdx,
CvSVMParams params)
Default and training constructors.
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
int |
get_support_vector_count() |
int |
get_var_count()
Returns the number of used features (variables count).
|
void |
predict_all(Mat samples,
Mat results)
Predicts the response for input sample(s).
|
float |
predict(Mat sample)
Predicts the response for input sample(s).
|
float |
predict(Mat sample,
boolean returnDFVal)
Predicts the response for input sample(s).
|
boolean |
train_auto(Mat trainData,
Mat responses,
Mat varIdx,
Mat sampleIdx,
CvSVMParams params)
Trains an SVM with optimal parameters.
|
boolean |
train_auto(Mat trainData,
Mat responses,
Mat varIdx,
Mat sampleIdx,
CvSVMParams params,
int k_fold,
CvParamGrid Cgrid,
CvParamGrid gammaGrid,
CvParamGrid pGrid,
CvParamGrid nuGrid,
CvParamGrid coeffGrid,
CvParamGrid degreeGrid,
boolean balanced)
Trains an SVM with optimal parameters.
|
boolean |
train(Mat trainData,
Mat responses)
Trains an SVM.
|
boolean |
train(Mat trainData,
Mat responses,
Mat varIdx,
Mat sampleIdx,
CvSVMParams params)
Trains an SVM.
|
load, load, save, save
public static final int C
public static final int C_SVC
public static final int COEF
public static final int DEGREE
public static final int EPS_SVR
public static final int GAMMA
public static final int LINEAR
public static final int NU
public static final int NU_SVC
public static final int NU_SVR
public static final int ONE_CLASS
public static final int P
public static final int POLY
public static final int RBF
public static final int SIGMOID
public CvSVM()
Default and training constructors.
The constructors follow conventions of "CvStatModel.CvStatModel". See "CvStatModel.train" for parameters descriptions.
public CvSVM(Mat trainData, Mat responses)
Default and training constructors.
The constructors follow conventions of "CvStatModel.CvStatModel". See "CvStatModel.train" for parameters descriptions.
trainData
- a trainDataresponses
- a responsespublic CvSVM(Mat trainData, Mat responses, Mat varIdx, Mat sampleIdx, CvSVMParams params)
Default and training constructors.
The constructors follow conventions of "CvStatModel.CvStatModel". See "CvStatModel.train" for parameters descriptions.
trainData
- a trainDataresponses
- a responsesvarIdx
- a varIdxsampleIdx
- a sampleIdxparams
- a paramspublic void clear()
public int get_support_vector_count()
public int get_var_count()
Returns the number of used features (variables count).
public void predict_all(Mat samples, Mat results)
Predicts the response for input sample(s).
If you pass one sample then prediction result is returned. If you want to get
responses for several samples then you should pass the results
matrix where prediction results will be stored.
The function is parallelized with the TBB library.
samples
- Input samples for prediction.results
- Output prediction responses for corresponding samples.public float predict(Mat sample)
Predicts the response for input sample(s).
If you pass one sample then prediction result is returned. If you want to get
responses for several samples then you should pass the results
matrix where prediction results will be stored.
The function is parallelized with the TBB library.
sample
- Input sample for prediction.public float predict(Mat sample, boolean returnDFVal)
Predicts the response for input sample(s).
If you pass one sample then prediction result is returned. If you want to get
responses for several samples then you should pass the results
matrix where prediction results will be stored.
The function is parallelized with the TBB library.
sample
- Input sample for prediction.returnDFVal
- Specifies a type of the return value. If true
and the problem is 2-class classification then the method returns the
decision function value that is signed distance to the margin, else the
function returns a class label (classification) or estimated function value
(regression).public boolean train_auto(Mat trainData, Mat responses, Mat varIdx, Mat sampleIdx, CvSVMParams params)
Trains an SVM with optimal parameters.
The method trains the SVM model automatically by choosing the optimal
parameters C
, gamma
, p
,
nu
, coef0
, degree
from "CvSVMParams".
Parameters are considered optimal when the cross-validation estimate of the
test set error is minimal.
If there is no need to optimize a parameter, the corresponding grid step
should be set to any value less than or equal to 1. For example, to avoid
optimization in gamma
, set gamma_grid.step = 0
,
gamma_grid.min_val
, gamma_grid.max_val
as arbitrary
numbers. In this case, the value params.gamma
is taken for
gamma
.
And, finally, if the optimization in a parameter is required but the
corresponding grid is unknown, you may call the function "CvSVM.get_default_grid".
To generate a grid, for example, for gamma
, call
CvSVM.get_default_grid(CvSVM.GAMMA)
.
This function works for the classification (params.svm_type=CvSVM.C_SVC
or params.svm_type=CvSVM.NU_SVC
) as well as for the regression
(params.svm_type=CvSVM.EPS_SVR
or params.svm_type=CvSVM.NU_SVR
).
If params.svm_type=CvSVM.ONE_CLASS
, no optimization is made and
the usual SVM with parameters specified in params
is executed.
trainData
- a trainDataresponses
- a responsesvarIdx
- a varIdxsampleIdx
- a sampleIdxparams
- a paramspublic boolean train_auto(Mat trainData, Mat responses, Mat varIdx, Mat sampleIdx, CvSVMParams params, int k_fold, CvParamGrid Cgrid, CvParamGrid gammaGrid, CvParamGrid pGrid, CvParamGrid nuGrid, CvParamGrid coeffGrid, CvParamGrid degreeGrid, boolean balanced)
Trains an SVM with optimal parameters.
The method trains the SVM model automatically by choosing the optimal
parameters C
, gamma
, p
,
nu
, coef0
, degree
from "CvSVMParams".
Parameters are considered optimal when the cross-validation estimate of the
test set error is minimal.
If there is no need to optimize a parameter, the corresponding grid step
should be set to any value less than or equal to 1. For example, to avoid
optimization in gamma
, set gamma_grid.step = 0
,
gamma_grid.min_val
, gamma_grid.max_val
as arbitrary
numbers. In this case, the value params.gamma
is taken for
gamma
.
And, finally, if the optimization in a parameter is required but the
corresponding grid is unknown, you may call the function "CvSVM.get_default_grid".
To generate a grid, for example, for gamma
, call
CvSVM.get_default_grid(CvSVM.GAMMA)
.
This function works for the classification (params.svm_type=CvSVM.C_SVC
or params.svm_type=CvSVM.NU_SVC
) as well as for the regression
(params.svm_type=CvSVM.EPS_SVR
or params.svm_type=CvSVM.NU_SVR
).
If params.svm_type=CvSVM.ONE_CLASS
, no optimization is made and
the usual SVM with parameters specified in params
is executed.
trainData
- a trainDataresponses
- a responsesvarIdx
- a varIdxsampleIdx
- a sampleIdxparams
- a paramsk_fold
- Cross-validation parameter. The training set is divided into
k_fold
subsets. One subset is used to test the model, the others
form the train set. So, the SVM algorithm is executed k_fold
times.Cgrid
- a CgridgammaGrid
- Iteration grid for the corresponding SVM parameter.pGrid
- Iteration grid for the corresponding SVM parameter.nuGrid
- Iteration grid for the corresponding SVM parameter.coeffGrid
- Iteration grid for the corresponding SVM parameter.degreeGrid
- Iteration grid for the corresponding SVM parameter.balanced
- If true
and the problem is 2-class
classification then the method creates more balanced cross-validation subsets
that is proportions between classes in subsets are close to such proportion
in the whole train dataset.public boolean train(Mat trainData, Mat responses)
Trains an SVM.
The method trains the SVM model. It follows the conventions of the generic "CvStatModel.train" approach with the following limitations:
CV_ROW_SAMPLE
data layout is supported.
params.svm_type=CvSVM.C_SVC
or params.svm_type=CvSVM.NU_SVC
), or ordered (params.svm_type=CvSVM.EPS_SVR
or params.svm_type=CvSVM.NU_SVR
), or not required at all
(params.svm_type=CvSVM.ONE_CLASS
).
All the other parameters are gathered in the "CvSVMParams" structure.
trainData
- a trainDataresponses
- a responsespublic boolean train(Mat trainData, Mat responses, Mat varIdx, Mat sampleIdx, CvSVMParams params)
Trains an SVM.
The method trains the SVM model. It follows the conventions of the generic "CvStatModel.train" approach with the following limitations:
CV_ROW_SAMPLE
data layout is supported.
params.svm_type=CvSVM.C_SVC
or params.svm_type=CvSVM.NU_SVC
), or ordered (params.svm_type=CvSVM.EPS_SVR
or params.svm_type=CvSVM.NU_SVR
), or not required at all
(params.svm_type=CvSVM.ONE_CLASS
).
All the other parameters are gathered in the "CvSVMParams" structure.
trainData
- a trainDataresponses
- a responsesvarIdx
- a varIdxsampleIdx
- a sampleIdxparams
- a params