View Source Evision.ML.SVM (Evision v0.1.9)

Link to this section Summary

cv

Clears the algorithm state

Returns the algorithm string identifier. This string is used as top level xml/yml node tag when the object is saved to a file or string.

Reads algorithm parameters from a file storage

Positional Arguments

Saves the algorithm to a file. In order to make this method work, the derived class must implement Algorithm::write(FileStorage& fs).

simplified API for language bindings

simplified API for language bindings

cv.ml

Computes error on the training or test dataset

Computes error on the training or test dataset

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.

Python prototype (for reference):

@see setC

@see setClassWeights

@see setCoef0

Retrieves the decision function

Retrieves the decision function

Generates a grid for %SVM parameters.

@see setDegree

@see setGamma

Type of a %SVM kernel. See SVM::KernelTypes. Default value is SVM::RBF.

@see setNu

@see setP

Retrieves all the support vectors

@see setTermCriteria

@see setType

Retrieves all the uncompressed support vectors of a linear %SVM

Returns the number of variables in training samples

Returns true if the model is classifier

Returns true if the model is trained

Loads and creates a serialized svm from a file

Predicts response(s) for the provided sample(s)

Predicts response(s) for the provided sample(s)

Positional Arguments
  • val: double

@copybrief getC @see getC

Positional Arguments

@copybrief getClassWeights @see getClassWeights

Positional Arguments
  • val: double

@copybrief getCoef0 @see getCoef0

Positional Arguments
  • val: double

@copybrief getDegree @see getDegree

Positional Arguments
  • val: double

@copybrief getGamma @see getGamma

Positional Arguments
  • kernelType: int

Initialize with one of predefined kernels. See SVM::KernelTypes.

Positional Arguments
  • val: double

@copybrief getNu @see getNu

Positional Arguments
  • val: double

@copybrief getP @see getP

Positional Arguments
  • val: TermCriteria

@copybrief getTermCriteria @see getTermCriteria

Positional Arguments
  • val: int

@copybrief getType @see getType

Trains the statistical model

Trains the statistical model

Trains the statistical model

Trains an %SVM with optimal parameters

Trains an %SVM with optimal parameters

Functions

Raising version of clear/1.

Raising version of create/0.

Raising version of empty/1.

Raising version of getC/1.

Raising version of getCoef0/1.

Raising version of getDegree/1.

Raising version of getGamma/1.

Raising version of getNu/1.

Raising version of getP/1.

Raising version of getType/1.

Raising version of getVarCount/1.

Raising version of isClassifier/1.

Raising version of isTrained/1.

Raising version of load/1.

Raising version of predict/2.

Raising version of read/2.

Raising version of save/2.

Raising version of setC/2.

Raising version of setCoef0/2.

Raising version of setDegree/2.

Raising version of setGamma/2.

Raising version of setNu/2.

Raising version of setP/2.

Raising version of setType/2.

Raising version of train/2.

Raising version of write/2.

Raising version of write/3.

Link to this section cv

Clears the algorithm state

Python prototype (for reference):

clear() -> None

Returns the algorithm string identifier. This string is used as top level xml/yml node tag when the object is saved to a file or string.

Python prototype (for reference):

getDefaultName() -> retval

Reads algorithm parameters from a file storage

Positional Arguments
  • fn_: FileNode

Python prototype (for reference):

read(fn_) -> None
Positional Arguments

Saves the algorithm to a file. In order to make this method work, the derived class must implement Algorithm::write(FileStorage& fs).

Python prototype (for reference):

save(filename) -> None

simplified API for language bindings

Positional Arguments
  • fs: Ptr<FileStorage>
Keyword Arguments

Has overloading in C++

Python prototype (for reference):

write(fs[, name]) -> None

simplified API for language bindings

Positional Arguments
  • fs: Ptr<FileStorage>
Keyword Arguments

Has overloading in C++

Python prototype (for reference):

write(fs[, name]) -> None

Link to this section cv.ml

Link to this function

calcError(self, data, test)

View Source

Computes error on the training or test dataset

Positional Arguments
  • data: Ptr<TrainData>.

    the training data

  • test: bool.

    if true, the error is computed over the test subset of the data, otherwise it's computed over the training subset of the data. Please note that if you loaded a completely different dataset to evaluate already trained classifier, you will probably want not to set the test subset at all with TrainData::setTrainTestSplitRatio and specify test=false, so that the error is computed for the whole new set. Yes, this sounds a bit confusing.

Return

The method uses StatModel::predict to compute the error. For regression models the error is computed as RMS, for classifiers - as a percent of missclassified samples (0%-100%).

Python prototype (for reference):

calcError(data, test[, resp]) -> retval, resp
Link to this function

calcError(self, data, test, opts)

View Source

Computes error on the training or test dataset

Positional Arguments
  • data: Ptr<TrainData>.

    the training data

  • test: bool.

    if true, the error is computed over the test subset of the data, otherwise it's computed over the training subset of the data. Please note that if you loaded a completely different dataset to evaluate already trained classifier, you will probably want not to set the test subset at all with TrainData::setTrainTestSplitRatio and specify test=false, so that the error is computed for the whole new set. Yes, this sounds a bit confusing.

Return

The method uses StatModel::predict to compute the error. For regression models the error is computed as RMS, for classifiers - as a percent of missclassified samples (0%-100%).

Python prototype (for reference):

calcError(data, test[, resp]) -> retval, resp

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.

Python prototype (for reference):

create() -> retval

Python prototype (for reference):

empty() -> retval

@see setC

Python prototype (for reference):

getC() -> retval

@see setClassWeights

Python prototype (for reference):

getClassWeights() -> retval

@see setCoef0

Python prototype (for reference):

getCoef0() -> retval
Link to this function

getDecisionFunction(self, i)

View Source

Retrieves the decision function

Positional Arguments
  • i: int.

    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 \f$N(N-1)/2\f$ decision functions.

Return
  • alpha: Evision.Mat.

    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: Evision.Mat.

    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.

The method returns rho parameter of the decision function, a scalar subtracted from the weighted sum of kernel responses.

Python prototype (for reference):

getDecisionFunction(i[, alpha[, svidx]]) -> retval, alpha, svidx
Link to this function

getDecisionFunction(self, i, opts)

View Source

Retrieves the decision function

Positional Arguments
  • i: int.

    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 \f$N(N-1)/2\f$ decision functions.

Return
  • alpha: Evision.Mat.

    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: Evision.Mat.

    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.

The method returns rho parameter of the decision function, a scalar subtracted from the weighted sum of kernel responses.

Python prototype (for reference):

getDecisionFunction(i[, alpha[, svidx]]) -> retval, alpha, svidx
Link to this function

getDefaultGridPtr(param_id)

View Source

Generates a grid for %SVM parameters.

Positional Arguments
  • param_id: int.

    %SVM parameters IDs that must be one of the SVM::ParamTypes. The grid is generated for the parameter with this ID.

The function generates a grid pointer for the specified parameter of the %SVM algorithm. The grid may be passed to the function SVM::trainAuto.

Python prototype (for reference):

getDefaultGridPtr(param_id) -> retval

@see setDegree

Python prototype (for reference):

getDegree() -> retval

@see setGamma

Python prototype (for reference):

getGamma() -> retval

Type of a %SVM kernel. See SVM::KernelTypes. Default value is SVM::RBF.

Python prototype (for reference):

getKernelType() -> retval

@see setNu

Python prototype (for reference):

getNu() -> retval

@see setP

Python prototype (for reference):

getP() -> 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.

Python prototype (for reference):

getSupportVectors() -> retval

@see setTermCriteria

Python prototype (for reference):

getTermCriteria() -> retval

@see setType

Python prototype (for reference):

getType() -> retval
Link to this function

getUncompressedSupportVectors(self)

View Source

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.

Python prototype (for reference):

getUncompressedSupportVectors() -> retval

Returns the number of variables in training samples

Python prototype (for reference):

getVarCount() -> retval

Returns true if the model is classifier

Python prototype (for reference):

isClassifier() -> retval

Returns true if the model is trained

Python prototype (for reference):

isTrained() -> retval

Loads and creates a serialized svm from a file

Positional Arguments
  • filepath: String.

    path to serialized svm

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.

Python prototype (for reference):

load(filepath) -> retval

Predicts response(s) for the provided sample(s)

Positional Arguments
  • samples: Evision.Mat.

    The input samples, floating-point matrix

Keyword Arguments
  • flags: int.

    The optional flags, model-dependent. See cv::ml::StatModel::Flags.

Return
  • results: Evision.Mat.

    The optional output matrix of results.

Python prototype (for reference):

predict(samples[, results[, flags]]) -> retval, results
Link to this function

predict(self, samples, opts)

View Source

Predicts response(s) for the provided sample(s)

Positional Arguments
  • samples: Evision.Mat.

    The input samples, floating-point matrix

Keyword Arguments
  • flags: int.

    The optional flags, model-dependent. See cv::ml::StatModel::Flags.

Return
  • results: Evision.Mat.

    The optional output matrix of results.

Python prototype (for reference):

predict(samples[, results[, flags]]) -> retval, results
Positional Arguments
  • val: double

@copybrief getC @see getC

Python prototype (for reference):

setC(val) -> None
Link to this function

setClassWeights(self, val)

View Source
Positional Arguments

@copybrief getClassWeights @see getClassWeights

Python prototype (for reference):

setClassWeights(val) -> None
Positional Arguments
  • val: double

@copybrief getCoef0 @see getCoef0

Python prototype (for reference):

setCoef0(val) -> None
Positional Arguments
  • val: double

@copybrief getDegree @see getDegree

Python prototype (for reference):

setDegree(val) -> None
Positional Arguments
  • val: double

@copybrief getGamma @see getGamma

Python prototype (for reference):

setGamma(val) -> None
Link to this function

setKernel(self, kernelType)

View Source
Positional Arguments
  • kernelType: int

Initialize with one of predefined kernels. See SVM::KernelTypes.

Python prototype (for reference):

setKernel(kernelType) -> None
Positional Arguments
  • val: double

@copybrief getNu @see getNu

Python prototype (for reference):

setNu(val) -> None
Positional Arguments
  • val: double

@copybrief getP @see getP

Python prototype (for reference):

setP(val) -> None
Link to this function

setTermCriteria(self, val)

View Source
Positional Arguments
  • val: TermCriteria

@copybrief getTermCriteria @see getTermCriteria

Python prototype (for reference):

setTermCriteria(val) -> None
Positional Arguments
  • val: int

@copybrief getType @see getType

Python prototype (for reference):

setType(val) -> None

Trains the statistical model

Positional Arguments
  • trainData: Ptr<TrainData>.

    training data that can be loaded from file using TrainData::loadFromCSV or created with TrainData::create.

Keyword Arguments
  • flags: int.

    optional flags, depending on the model. Some of the models can be updated with the new training samples, not completely overwritten (such as NormalBayesClassifier or ANN_MLP).

Python prototype (for reference):

train(trainData[, flags]) -> retval
Link to this function

train(self, trainData, opts)

View Source

Trains the statistical model

Positional Arguments
  • trainData: Ptr<TrainData>.

    training data that can be loaded from file using TrainData::loadFromCSV or created with TrainData::create.

Keyword Arguments
  • flags: int.

    optional flags, depending on the model. Some of the models can be updated with the new training samples, not completely overwritten (such as NormalBayesClassifier or ANN_MLP).

Python prototype (for reference):

train(trainData[, flags]) -> retval
Link to this function

train(self, samples, layout, responses)

View Source

Trains the statistical model

Positional Arguments
  • samples: Evision.Mat.

    training samples

  • layout: int.

    See ml::SampleTypes.

  • responses: Evision.Mat.

    vector of responses associated with the training samples.

Python prototype (for reference):

train(samples, layout, responses) -> retval
Link to this function

trainAuto(self, samples, layout, responses)

View Source

Trains an %SVM with optimal parameters

Positional Arguments
  • samples: Evision.Mat.

    training samples

  • layout: int.

    See ml::SampleTypes.

  • responses: Evision.Mat.

    vector of responses associated with the training samples.

Keyword Arguments
  • kFold: int.

    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

  • cgrid: Ptr<ParamGrid>.

  • gammaGrid: Ptr<ParamGrid>.

    grid for gamma

  • pGrid: Ptr<ParamGrid>.

  • nuGrid: Ptr<ParamGrid>.

  • coeffGrid: Ptr<ParamGrid>.

    grid for coeff

  • degreeGrid: Ptr<ParamGrid>.

    grid for degree

  • balanced: bool.

    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.

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.

Python prototype (for reference):

trainAuto(samples, layout, responses[, kFold[, Cgrid[, gammaGrid[, pGrid[, nuGrid[, coeffGrid[, degreeGrid[, balanced]]]]]]]]) -> retval
Link to this function

trainAuto(self, samples, layout, responses, opts)

View Source

Trains an %SVM with optimal parameters

Positional Arguments
  • samples: Evision.Mat.

    training samples

  • layout: int.

    See ml::SampleTypes.

  • responses: Evision.Mat.

    vector of responses associated with the training samples.

Keyword Arguments
  • kFold: int.

    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

  • cgrid: Ptr<ParamGrid>.

  • gammaGrid: Ptr<ParamGrid>.

    grid for gamma

  • pGrid: Ptr<ParamGrid>.

  • nuGrid: Ptr<ParamGrid>.

  • coeffGrid: Ptr<ParamGrid>.

    grid for coeff

  • degreeGrid: Ptr<ParamGrid>.

    grid for degree

  • balanced: bool.

    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.

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.

Python prototype (for reference):

trainAuto(samples, layout, responses[, kFold[, Cgrid[, gammaGrid[, pGrid[, nuGrid[, coeffGrid[, degreeGrid[, balanced]]]]]]]]) -> retval

Link to this section Functions

Link to this function

calcError!(self, data, test)

View Source

Raising version of calcError/3.

Link to this function

calcError!(self, data, test, opts)

View Source

Raising version of calcError/4.

Raising version of clear/1.

Raising version of create/0.

Raising version of empty/1.

Raising version of getC/1.

Raising version of getClassWeights/1.

Raising version of getCoef0/1.

Link to this function

getDecisionFunction!(self, i)

View Source

Raising version of getDecisionFunction/2.

Link to this function

getDecisionFunction!(self, i, opts)

View Source

Raising version of getDecisionFunction/3.

Link to this function

getDefaultGridPtr!(param_id)

View Source

Raising version of getDefaultGridPtr/1.

Raising version of getDefaultName/1.

Raising version of getDegree/1.

Raising version of getGamma/1.

Raising version of getKernelType/1.

Raising version of getNu/1.

Raising version of getP/1.

Link to this function

getSupportVectors!(self)

View Source

Raising version of getSupportVectors/1.

Raising version of getTermCriteria/1.

Raising version of getType/1.

Link to this function

getUncompressedSupportVectors!(self)

View Source

Raising version of getUncompressedSupportVectors/1.

Raising version of getVarCount/1.

Raising version of isClassifier/1.

Raising version of isTrained/1.

Raising version of load/1.

Raising version of predict/2.

Link to this function

predict!(self, samples, opts)

View Source

Raising version of predict/3.

Raising version of read/2.

Raising version of save/2.

Raising version of setC/2.

Link to this function

setClassWeights!(self, val)

View Source

Raising version of setClassWeights/2.

Raising version of setCoef0/2.

Raising version of setDegree/2.

Raising version of setGamma/2.

Link to this function

setKernel!(self, kernelType)

View Source

Raising version of setKernel/2.

Raising version of setNu/2.

Raising version of setP/2.

Link to this function

setTermCriteria!(self, val)

View Source

Raising version of setTermCriteria/2.

Raising version of setType/2.

Raising version of train/2.

Link to this function

train!(self, trainData, opts)

View Source

Raising version of train/3.

Link to this function

train!(self, samples, layout, responses)

View Source

Raising version of train/4.

Link to this function

trainAuto!(self, samples, layout, responses)

View Source

Raising version of trainAuto/4.

Link to this function

trainAuto!(self, samples, layout, responses, opts)

View Source

Raising version of trainAuto/5.

Raising version of write/2.

Raising version of write/3.