标签:opencv svm svm参数优化 svr回归 opencv svr cvsvm
SVM(支持向量机)是机器学习算法里用得最多的一种算法。SVM最常用的是用于分类,不过SVM也可以用于回归,我的实验中就是用SVM来实现SVR(支持向量回归)。对于功能这么强的算法,opencv中自然也是有集成好了,我们可以直接调用。
网上讲opencv中SVM使用的文章有很多,但讲SVM参数优化的文章却很少。所以在这里不重点讲怎么使用SVM,而是谈谈怎样通过opencv中自带的库优化SVM中的各参数。
相信用SVM做过实验的人都知道,SVM的各参数对实验结果有很大的影响,比如C,gama,P,coef等等。下面就是CvSVMParams类的原型。
C++: CvSVMParams::CvSVMParams()
C++: CvSVMParams::CvSVMParams(int svm_type,
int kernel_type,
double degree,
double gamma,
double coef0,
double Cvalue,
double nu,
double p,
CvMat* class_weights,
CvTermCriteria term_crit
)
<2>kernel_type:SVM的内核类型(4种):
CvSVMParams param; param.svm_type = CvSVM::EPS_SVR; //我的实验是用SVR作回归分析,可能大部分人的实验是用SVM来分类,方法都一样 param.kernel_type = CvSVM::RBF; param.C = 1; param.p = 5e-3; param.gamma = 0.01; param.term_crit = cvTermCriteria(CV_TERMCRIT_EPS, 100, 5e-3);
C++: bool CvSVM::train(const Mat& trainData,
const Mat& responses,
const Mat& varIdx=Mat(),
const Mat& sampleIdx=Mat(),
CvSVMParams params=CvSVMParams()
)
C++: bool CvSVM::train_auto(const Mat& trainData,
const Mat& responses,
const Mat& varIdx,
const Mat& sampleIdx,
CvSVMParams params,
int k_fold=10,
CvParamGrid Cgrid=CvSVM::get_default_grid(CvSVM::C),
CvParamGrid gammaGrid=CvSVM::get_default_grid(CvSVM::GAMMA),
CvParamGrid pGrid=CvSVM::get_default_grid(CvSVM::P),
CvParamGrid nuGrid=CvSVM::get_default_grid(CvSVM::NU),
CvParamGrid coeffGrid=CvSVM::get_default_grid(CvSVM::COEF),
CvParamGrid degreeGrid=CvSVM::get_default_grid(CvSVM::DEGREE),
bool balanced=false
)
CvSVMParams param; param.svm_type = CvSVM::EPS_SVR; param.kernel_type = CvSVM::RBF; param.C = 1; //给参数赋初始值 param.p = 5e-3; //给参数赋初始值 param.gamma = 0.01; //给参数赋初始值 param.term_crit = cvTermCriteria(CV_TERMCRIT_EPS, 100, 5e-3); //对不用的参数step设为0 CvParamGrid nuGrid = CvParamGrid(1,1,0.0); CvParamGrid coeffGrid = CvParamGrid(1,1,0.0); CvParamGrid degreeGrid = CvParamGrid(1,1,0.0); CvSVM regressor; regressor.train_auto(PCA_training,tr_label,NULL,NULL,param, 10, regressor.get_default_grid(CvSVM::C), regressor.get_default_grid(CvSVM::GAMMA), regressor.get_default_grid(CvSVM::P), nuGrid, coeffGrid, degreeGrid);
CvSVMParams params_re = regressor.get_params(); regressor.save("training_srv.xml"); float C = params_re.C; float P = params_re.p; float gamma = params_re.gamma; printf("\nParms: C = %f, P = %f,gamma = %f \n",C,P,gamma);
OpenCV中的SVM参数优化,布布扣,bubuko.com
标签:opencv svm svm参数优化 svr回归 opencv svr cvsvm
原文地址:http://blog.csdn.net/computerme/article/details/38677599