Class cv::ml::SVM#
Support Vector Machines. View details
#include <opencv2/ml.hpp>Collaboration diagram for cv::ml::SVM:
Public Types#
SVM kernel type
SVM params type
SVM type
Public Types inherited from cv::ml::StatModel
Return |
Name |
Description |
|---|---|---|
Public Member Functions#
Public Member Functions inherited from cv::ml::StatModel
Return |
Name |
Description |
|---|---|---|
|
|
Computes error on the training or test dataset. |
|
Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read. |
|
|
Returns the number of variables in training samples. |
|
|
Returns true if the model is classifier. |
|
|
Returns true if the model is trained. |
|
|
|
Predicts response(s) for the provided sample(s) |
|
Trains the statistical model. |
|
|
|
Trains the statistical model. |
Public Member Functions inherited from cv::Algorithm
Return |
Name |
Description |
|---|---|---|
|
Clears the algorithm state. |
|
|
Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read. |
|
|
Reads algorithm parameters from a file storage. |
|
|
||
|
|
|
|
Stores algorithm parameters in a file storage. |
|
|
Static Public Member Functions#
Static Public Member Functions inherited from cv::ml::StatModel
Return |
Name |
Description |
|---|---|---|
|
Create and train model with default parameters. |
Static Public Member Functions inherited from cv::Algorithm
Return |
Name |
Description |
|---|---|---|
|
|
Loads algorithm from the file. |
|
|
Loads algorithm from a String. |
|
Reads algorithm from the file node. |
Additional Inherited Members#
Protected Member Functions inherited from cv::Algorithm
Return |
Name |
Description |
|---|---|---|
|
Detailed Description#
Support Vector Machines.
See also
ml_intro_svm
Member Enumeration Documentation#
enum KernelTypes
SVM kernel type
|
Returned by SVM::getKernelType in case when custom kernel has been set |
|
Linear kernel. No mapping is done, linear discrimination (or regression) is done in the original feature space. It is the fastest option. \(K(x_i, x_j) = x_i^T x_j\). |
|
Polynomial kernel: \(K(x_i, x_j) = (\gamma x_i^T x_j + coef0)^{degree}, \gamma > 0\). |
|
Radial basis function (RBF), a good choice in most cases. \(K(x_i, x_j) = e^{-\gamma ||x_i - x_j||^2}, \gamma > 0\). |
|
Sigmoid kernel: \(K(x_i, x_j) = \tanh(\gamma x_i^T x_j + coef0)\). |
|
Exponential Chi2 kernel, similar to the RBF kernel: \(K(x_i, x_j) = e^{-\gamma \chi^2(x_i,x_j)}, \chi^2(x_i,x_j) = (x_i-x_j)^2/(x_i+x_j), \gamma > 0\). |
|
Histogram intersection kernel. A fast kernel. \(K(x_i, x_j) = min(x_i,x_j)\). |
enum ParamTypes
SVM params type
|
|
|
|
|
|
enum Types
SVM type
|
C-Support Vector Classification. n-class classification (n \(\geq\) 2), allows imperfect separation of classes with penalty multiplier C for outliers. |
|
\(\nu\)-Support Vector Classification. n-class classification with possible imperfect separation. Parameter \(\nu\) (in the range 0..1, the larger the value, the smoother the decision boundary) is used instead of C. |
|
Distribution Estimation (One-class SVM). All the training data are from the same class, SVM builds a boundary that separates the class from the rest of the feature space. |
|
\(\epsilon\)-Support Vector Regression. The distance between feature vectors from the training set and the fitting hyper-plane must be less than p. For outliers the penalty multiplier C is used. |
|
\(\nu\)-Support Vector Regression. \(\nu\) is used instead of p. See [56] for details. |
Member Function Documentation#
getC()#
double cv::ml::SVM::getC()
Python:
cv.ml.SVM.getC() -> retval
Parameter C of a SVM optimization problem. For SVM::C_SVC, SVM::EPS_SVR or SVM::NU_SVR. Default value is 0.
See also
getClassWeights()#
cv::Mat cv::ml::SVM::getClassWeights()
Python:
cv.ml.SVM.getClassWeights() -> retval
Optional weights in the SVM::C_SVC problem, assigned to particular classes. They are multiplied by C so the parameter C of class i becomes classWeights(i) * C. Thus these weights affect the misclassification penalty for different classes. The larger weight, the larger penalty on misclassification of data from the corresponding class. Default value is empty Mat.
See also
getCoef0()#
double cv::ml::SVM::getCoef0()
Python:
cv.ml.SVM.getCoef0() -> retval
Parameter coef0 of a kernel function. For SVM::POLY or SVM::SIGMOID. Default value is 0.
See also
getDecisionFunction()#
double cv::ml::SVM::getDecisionFunction(
int i,
OutputArray alpha,
OutputArray svidx )
Python:
cv.ml.SVM.getDecisionFunction(i[, alpha[, svidx]]) -> retval, alpha, svidx
Retrieves the decision function.
The method returns rho parameter of the decision function, a scalar subtracted from the weighted sum of kernel responses.
Parameters
i— the index of the decision function. If the problem solved is regression, 1-class or 2-class classification, then there will be just one decision function and the index should always be 0. Otherwise, in the case of N-class classification, there will be \(N(N-1)/2\) decision functions.alpha— the optional output vector for weights, corresponding to different support vectors. In the case of linear SVM all the alpha’s will be 1’s.svidx— the optional output vector of indices of support vectors within the matrix of support vectors (which can be retrieved by SVM::getSupportVectors). In the case of linear SVM each decision function consists of a single “compressed” support vector.
getDegree()#
double cv::ml::SVM::getDegree()
Python:
cv.ml.SVM.getDegree() -> retval
Parameter degree of a kernel function. For SVM::POLY. Default value is 0.
See also
getGamma()#
double cv::ml::SVM::getGamma()
Python:
cv.ml.SVM.getGamma() -> retval
Parameter \(\gamma\) of a kernel function. For SVM::POLY, SVM::RBF, SVM::SIGMOID or SVM::CHI2. Default value is 1.
See also
getKernelType()#
int cv::ml::SVM::getKernelType()
Python:
cv.ml.SVM.getKernelType() -> retval
Type of a SVM kernel. See SVM::KernelTypes. Default value is SVM::RBF.
getNu()#
double cv::ml::SVM::getNu()
Python:
cv.ml.SVM.getNu() -> retval
Parameter \(\nu\) of a SVM optimization problem. For SVM::NU_SVC, SVM::ONE_CLASS or SVM::NU_SVR. Default value is 0.
See also
getP()#
double cv::ml::SVM::getP()
Python:
cv.ml.SVM.getP() -> retval
Parameter \(\epsilon\) of a SVM optimization problem. For SVM::EPS_SVR. Default value is 0.
See also
getSupportVectors()#
Mat cv::ml::SVM::getSupportVectors()
Python:
cv.ml.SVM.getSupportVectors() -> retval
Retrieves all the support vectors.
The method returns all the support vectors as a floating-point matrix, where support vectors are stored as matrix rows.
getTermCriteria()#
cv::TermCriteria cv::ml::SVM::getTermCriteria()
Python:
cv.ml.SVM.getTermCriteria() -> retval
Termination criteria of the iterative SVM training procedure which solves a partial case of constrained quadratic optimization problem. You can specify tolerance and/or the maximum number of iterations. Default value is TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 1000, FLT_EPSILON );
See also
getType()#
int cv::ml::SVM::getType()
Python:
cv.ml.SVM.getType() -> retval
Type of a SVM formulation. See SVM::Types. Default value is SVM::C_SVC.
See also
getUncompressedSupportVectors()#
Mat cv::ml::SVM::getUncompressedSupportVectors()
Python:
cv.ml.SVM.getUncompressedSupportVectors() -> retval
Retrieves all the uncompressed support vectors of a linear SVM.
The method returns all the uncompressed support vectors of a linear SVM that the compressed support vector, used for prediction, was derived from. They are returned in a floating-point matrix, where the support vectors are stored as matrix rows.
setC()#
void cv::ml::SVM::setC(double val)
Python:
cv.ml.SVM.setC(val)
See also
setClassWeights()#
void cv::ml::SVM::setClassWeights(const cv::Mat & val)
Python:
cv.ml.SVM.setClassWeights(val)
See also
setCoef0()#
void cv::ml::SVM::setCoef0(double val)
Python:
cv.ml.SVM.setCoef0(val)
See also
setCustomKernel()#
void cv::ml::SVM::setCustomKernel(const Ptr< Kernel > & _kernel)
Initialize with custom kernel. See SVM::Kernel class for implementation details
setDegree()#
void cv::ml::SVM::setDegree(double val)
Python:
cv.ml.SVM.setDegree(val)
See also
setGamma()#
void cv::ml::SVM::setGamma(double val)
Python:
cv.ml.SVM.setGamma(val)
See also
setKernel()#
void cv::ml::SVM::setKernel(int kernelType)
Python:
cv.ml.SVM.setKernel(kernelType)
Initialize with one of predefined kernels. See SVM::KernelTypes.
setNu()#
void cv::ml::SVM::setNu(double val)
Python:
cv.ml.SVM.setNu(val)
See also
setP()#
void cv::ml::SVM::setP(double val)
Python:
cv.ml.SVM.setP(val)
See also
setTermCriteria()#
void cv::ml::SVM::setTermCriteria(const cv::TermCriteria & val)
Python:
cv.ml.SVM.setTermCriteria(val)
See also
setType()#
void cv::ml::SVM::setType(int val)
Python:
cv.ml.SVM.setType(val)
See also
trainAuto()#
bool cv::ml::SVM::trainAuto(
const Ptr< TrainData > & data,
int kFold = 10,
ParamGrid Cgrid = getDefaultGrid(C),
ParamGrid gammaGrid = getDefaultGrid(GAMMA),
ParamGrid pGrid = getDefaultGrid(P),
ParamGrid nuGrid = getDefaultGrid(NU),
ParamGrid coeffGrid = getDefaultGrid(COEF),
ParamGrid degreeGrid = getDefaultGrid(DEGREE),
bool balanced = false )
Python:
cv.ml.SVM.trainAuto(samples, layout, responses[, kFold[, Cgrid[, gammaGrid[, pGrid[, nuGrid[, coeffGrid[, degreeGrid[, balanced]]]]]]]]) -> retval
Trains an SVM with optimal parameters.
The method trains the SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. 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 gammaGrid.step = 0, gammaGrid.minVal, gamma_grid.maxVal as arbitrary numbers. In this case, the value 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 SVM::getDefaultGrid. To generate a grid, for example, for gamma, call SVM::getDefaultGrid(SVM::GAMMA).
This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual SVM with parameters specified in params is executed.
Parameters
data— the training data that can be constructed using TrainData::create or TrainData::loadFromCSV.kFold— Cross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the SVM algorithm is executed kFold times.Cgrid— grid for CgammaGrid— grid for gammapGrid— grid for pnuGrid— grid for nucoeffGrid— grid for coeffdegreeGrid— grid for degreebalanced— 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.
trainAuto()#
bool cv::ml::SVM::trainAuto(
InputArray samples,
int layout,
InputArray responses,
int kFold = 10,
Ptr< ParamGrid > Cgrid = SVM::getDefaultGridPtr(SVM::C),
Ptr< ParamGrid > gammaGrid = SVM::getDefaultGridPtr(SVM::GAMMA),
Ptr< ParamGrid > pGrid = SVM::getDefaultGridPtr(SVM::P),
Ptr< ParamGrid > nuGrid = SVM::getDefaultGridPtr(SVM::NU),
Ptr< ParamGrid > coeffGrid = SVM::getDefaultGridPtr(SVM::COEF),
Ptr< ParamGrid > degreeGrid = SVM::getDefaultGridPtr(SVM::DEGREE),
bool balanced = false )
Python:
cv.ml.SVM.trainAuto(samples, layout, responses[, kFold[, Cgrid[, gammaGrid[, pGrid[, nuGrid[, coeffGrid[, degreeGrid[, balanced]]]]]]]]) -> retval
Trains an SVM with optimal parameters.
The method trains the SVM model automatically by choosing the optimal parameters C, gamma, p, nu, coef0, degree. Parameters are considered optimal when the cross-validation estimate of the test set error is minimal.
This function only makes use of SVM::getDefaultGrid for parameter optimization and thus only offers rudimentary parameter options.
This function works for the classification (SVM::C_SVC or SVM::NU_SVC) as well as for the regression (SVM::EPS_SVR or SVM::NU_SVR). If it is SVM::ONE_CLASS, no optimization is made and the usual SVM with parameters specified in params is executed.
Parameters
samples— training sampleslayout— See ml::SampleTypes.responses— vector of responses associated with the training samples.kFold— Cross-validation parameter. The training set is divided into kFold subsets. One subset is used to test the model, the others form the train set. So, the SVM algorithm isCgrid— grid for CgammaGrid— grid for gammapGrid— grid for pnuGrid— grid for nucoeffGrid— grid for coeffdegreeGrid— grid for degreebalanced— 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.
create()#
static Ptr< SVM > cv::ml::SVM::create()
Python:
cv.ml.SVM.create() -> retval
cv.ml.SVM_create() -> retval
Creates empty model. Use StatModel::train to train the model. Since SVM has several parameters, you may want to find the best parameters for your problem, it can be done with SVM::trainAuto.
getDefaultGrid()#
static ParamGrid cv::ml::SVM::getDefaultGrid(int param_id)
Generates a grid for SVM parameters.
The function generates a grid for the specified parameter of the SVM algorithm. The grid may be passed to the function SVM::trainAuto.
Parameters
param_id— SVM parameters IDs that must be one of the SVM::ParamTypes. The grid is generated for the parameter with this ID.
getDefaultGridPtr()#
static Ptr< ParamGrid > cv::ml::SVM::getDefaultGridPtr(int param_id)
Python:
cv.ml.SVM.getDefaultGridPtr(param_id) -> retval
cv.ml.SVM_getDefaultGridPtr(param_id) -> retval
Generates a grid for SVM parameters.
The function generates a grid pointer for the specified parameter of the SVM algorithm. The grid may be passed to the function SVM::trainAuto.
Parameters
param_id— SVM parameters IDs that must be one of the SVM::ParamTypes. The grid is generated for the parameter with this ID.
load()#
static Ptr< SVM > cv::ml::SVM::load(const String & filepath)
Python:
cv.ml.SVM.load(filepath) -> retval
cv.ml.SVM_load(filepath) -> retval
Loads and creates a serialized svm from a file.
Use SVM::save to serialize and store an SVM to disk. Load the SVM from this file again, by calling this function with the path to the file.
Parameters
filepath— path to serialized svm
Source file#
The documentation for this class was generated from the following file:
opencv2/ml.hpp