Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to find optimal SVM parameters gamma and cost using grid search with 5-fold cross validation process?

I'm using libsvm to classify my dataset but I'm not reaching good results with SVM. I think that it is because the parameters: Gamma and Cost were defined wrongly. I'm training the SVM with C-SVC and RBF Kernel.Please tell how to obtain optimal parameters using a grid-search with 5-fold cross-validation process. My complete code is given below

int main(int,char **) { Ptr<ml::traindata> tdata = ml::TrainData::loadFromCSV("/home/vidushi/Desktop/new/vidtrain.csv",1,-2,0); Mat data = tdata->getTrainSamples(); int labels[38] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1}; Mat labelsMat(38, 1, CV_32SC1, labels); Ptr<cv::ml::svm> svm = cv::ml::SVM::create(); svm->setType(SVM::C_SVC); svm->setKernel(SVM::RBF); svm->setC(1000); svm->setGamma(100); Ptr<ml::traindata> td = ml::TrainData::create(data, ROW_SAMPLE, labelsMat); svm->trainAuto(td); Ptr<ml::traindata> tdata1 = ml::TrainData::loadFromCSV("/home/vidushi/Desktop/new/vidtest1.csv",1,-2,0); Mat data1 = tdata1->getTrainSamples(); Mat result; svm->predict(data1,result); cout<<result.rows&lt;&lt;" "&lt;<result.cols&lt;&lt;"\n";="" cout&lt;&lt;"result="&lt;&lt; " "="" &lt;<result&lt;&lt;"\n";="" return="" 0;="" }<="" p="">

click to hide/show revision 2
No.2 Revision

updated 2017-04-22 05:57:56 -0600

berak gravatar image

how to find optimal SVM parameters gamma and cost using grid search with 5-fold cross validation process?

I'm using libsvm to classify my dataset but I'm not reaching good results with SVM. I think that it is because the parameters: Gamma and Cost were defined wrongly. I'm training the SVM with C-SVC and RBF Kernel.Please tell how to obtain optimal parameters using a grid-search with 5-fold cross-validation process. My complete code is given below

int main(int,char **)
{
Ptr<ml::traindata> Ptr<ml::TrainData> tdata = ml::TrainData::loadFromCSV("/home/vidushi/Desktop/new/vidtrain.csv",1,-2,0);
Mat data   = tdata->getTrainSamples();
int labels[38] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1};
Mat labelsMat(38, 1, CV_32SC1, labels);
Ptr<cv::ml::svm> Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::RBF);
svm->setC(1000);
svm->setGamma(100);
Ptr<ml::traindata> Ptr<ml::TrainData> td = ml::TrainData::create(data, ROW_SAMPLE, labelsMat);
svm->trainAuto(td);
 Ptr<ml::traindata> Ptr<ml::TrainData> tdata1 = ml::TrainData::loadFromCSV("/home/vidushi/Desktop/new/vidtest1.csv",1,-2,0);
Mat data1   = tdata1->getTrainSamples();
Mat result;
svm->predict(data1,result);
cout<<result.rows&lt;&lt;" "&lt;<result.cols&lt;&lt;"\n";="" cout&lt;&lt;"result="&lt;&lt; cout<<result.rows<<" "<<result.cols<<"\n";
cout<<"result = "<< " "="" &lt;<result&lt;&lt;"\n";="" return="" 0;="" }<="" p="">

" <<result<<"\n"; return 0; }