[−][src]Trait opencv::ml::SVM
Required methods
fn as_raw_SVM(&self) -> *mut c_void
Provided methods
fn get_type(&self) -> Result<i32>
@see setType
fn set_type(&mut self, val: i32) -> Result<()>
@copybrief getType @see getType
fn get_gamma(&self) -> Result<f64>
@see setGamma
fn set_gamma(&mut self, val: f64) -> Result<()>
@copybrief getGamma @see getGamma
fn get_coef0(&self) -> Result<f64>
@see setCoef0
fn set_coef0(&mut self, val: f64) -> Result<()>
@copybrief getCoef0 @see getCoef0
fn get_degree(&self) -> Result<f64>
@see setDegree
fn set_degree(&mut self, val: f64) -> Result<()>
@copybrief getDegree @see getDegree
fn get_c(&self) -> Result<f64>
@see setC
fn set_c(&mut self, val: f64) -> Result<()>
@copybrief getC @see getC
fn get_nu(&self) -> Result<f64>
@see setNu
fn set_nu(&mut self, val: f64) -> Result<()>
@copybrief getNu @see getNu
fn get_p(&self) -> Result<f64>
@see setP
fn set_p(&mut self, val: f64) -> Result<()>
@copybrief getP @see getP
fn get_class_weights(&self) -> Result<Mat>
@see setClassWeights
fn set_class_weights(&mut self, val: &Mat) -> Result<()>
@copybrief getClassWeights @see getClassWeights
fn get_term_criteria(&self) -> Result<TermCriteria>
@see setTermCriteria
fn set_term_criteria(&mut self, val: &TermCriteria) -> Result<()>
@copybrief getTermCriteria @see getTermCriteria
fn get_kernel_type(&self) -> Result<i32>
Type of a %SVM kernel. See SVM::KernelTypes. Default value is SVM::RBF.
fn set_kernel(&mut self, kernel_type: i32) -> Result<()>
Initialize with one of predefined kernels. See SVM::KernelTypes.
fn set_custom_kernel(&mut self, _kernel: &PtrOfKernel) -> Result<()>
Initialize with custom kernel. See SVM::Kernel class for implementation details
fn train_auto_with_data(
&mut self,
data: &PtrOfTrainData,
k_fold: i32,
cgrid: &ParamGrid,
gamma_grid: &ParamGrid,
p_grid: &ParamGrid,
nu_grid: &ParamGrid,
coeff_grid: &ParamGrid,
degree_grid: &ParamGrid,
balanced: bool
) -> Result<bool>
&mut self,
data: &PtrOfTrainData,
k_fold: i32,
cgrid: &ParamGrid,
gamma_grid: &ParamGrid,
p_grid: &ParamGrid,
nu_grid: &ParamGrid,
coeff_grid: &ParamGrid,
degree_grid: &ParamGrid,
balanced: bool
) -> Result<bool>
Trains an %SVM with optimal parameters.
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 C
- gammaGrid: grid for gamma
- pGrid: grid for p
- nuGrid: grid for nu
- coeffGrid: grid for coeff
- degreeGrid: grid for degree
- 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.
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.
C++ default parameters
- k_fold: 10
- cgrid: getDefaultGrid(C)
- gamma_grid: getDefaultGrid(GAMMA)
- p_grid: getDefaultGrid(P)
- nu_grid: getDefaultGrid(NU)
- coeff_grid: getDefaultGrid(COEF)
- degree_grid: getDefaultGrid(DEGREE)
- balanced: false
fn train_auto(
&mut self,
samples: &dyn ToInputArray,
layout: i32,
responses: &dyn ToInputArray,
k_fold: i32,
cgrid: &PtrOfParamGrid,
gamma_grid: &PtrOfParamGrid,
p_grid: &PtrOfParamGrid,
nu_grid: &PtrOfParamGrid,
coeff_grid: &PtrOfParamGrid,
degree_grid: &PtrOfParamGrid,
balanced: bool
) -> Result<bool>
&mut self,
samples: &dyn ToInputArray,
layout: i32,
responses: &dyn ToInputArray,
k_fold: i32,
cgrid: &PtrOfParamGrid,
gamma_grid: &PtrOfParamGrid,
p_grid: &PtrOfParamGrid,
nu_grid: &PtrOfParamGrid,
coeff_grid: &PtrOfParamGrid,
degree_grid: &PtrOfParamGrid,
balanced: bool
) -> Result<bool>
Trains an %SVM with optimal parameters
Parameters
- samples: training samples
- layout: 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 is
- Cgrid: grid for C
- gammaGrid: grid for gamma
- pGrid: grid for p
- nuGrid: grid for nu
- coeffGrid: grid for coeff
- degreeGrid: grid for degree
- 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.
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.
C++ default parameters
- k_fold: 10
- cgrid: SVM::getDefaultGridPtr(SVM::C)
- gamma_grid: SVM::getDefaultGridPtr(SVM::GAMMA)
- p_grid: SVM::getDefaultGridPtr(SVM::P)
- nu_grid: SVM::getDefaultGridPtr(SVM::NU)
- coeff_grid: SVM::getDefaultGridPtr(SVM::COEF)
- degree_grid: SVM::getDefaultGridPtr(SVM::DEGREE)
- balanced: false
fn get_support_vectors(&self) -> Result<Mat>
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.
fn get_uncompressed_support_vectors(&self) -> Result<Mat>
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.
fn get_decision_function(
&self,
i: i32,
alpha: &mut dyn ToOutputArray,
svidx: &mut dyn ToOutputArray
) -> Result<f64>
&self,
i: i32,
alpha: &mut dyn ToOutputArray,
svidx: &mut dyn ToOutputArray
) -> Result<f64>
Retrieves the decision function
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 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.
The method returns rho parameter of the decision function, a scalar subtracted from the weighted sum of kernel responses.
Methods
impl<'_> dyn SVM + '_
[src]
pub fn get_default_grid(param_id: i32) -> Result<ParamGrid>
[src]
Generates a grid for %SVM parameters.
Parameters
- param_id: %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 for the specified parameter of the %SVM algorithm. The grid may be passed to the function SVM::trainAuto.
pub fn get_default_grid_ptr(param_id: i32) -> Result<PtrOfParamGrid>
[src]
Generates a grid for %SVM parameters.
Parameters
- param_id: %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.
pub fn create() -> Result<PtrOfSVM>
[src]
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.
pub fn load(filepath: &str) -> Result<PtrOfSVM>
[src]
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