Ask Your Question
0

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

asked 2017-04-22 05:42:01 -0600

vidushig2 gravatar image

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

berak gravatar image

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<<" "<<result.cols<<"\n";
cout<<"result = "<< " " <<result<<"\n";
return 0;
}
edit retag flag offensive close merge delete

Comments

since your csv seems to be rather small (38 items ?), could you add it, so folks could try your code ?

berak gravatar imageberak ( 2017-04-22 06:06:55 -0600 )edit

Is the way i am using trainAuto function correct? I have dataset of 100 images.I need to detect tables from these images.I have divided dataset into two parts one for training and one for testing. For each document image , a component is extracted and features are determined for that component.26 Features are there for each component and i have saved these features in csv file. I just want to know how to optimize parameters gamma and cost using grid search with 5-fold cross-validation process?Or what value should i take for c and gamma to get correct results.

vidushig2 gravatar imagevidushig2 ( 2017-04-22 06:17:56 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-04-22 07:04:17 -0600

berak gravatar image
  • default kfolds value for trainAuto is 10, if you want 5 folds, you have to specify:

    svm->trainAuto(td, 5);

  • if you leave all param grids at default, it might take very long, since each combination is tested against each other. if you're specifically interested in gamma and C (you do not need nu, coef0 or p for the RBF kernel), make a "nogrid" param for the others:

    Ptr<ml::SVM::ParamGrid> nogrid = ml::SVM::ParamGrid::create(0,0,0);

    svm->trainAuto(td, 5, getDefaultGrid(C), getDefaultGrid(GAMMA), nogrid, nogrid, nogrid, nogrid);

  • you will need much more data, 38 samples is like nothing.
  • try a LINEAR kernel, too.
edit flag offensive delete link more

Comments

The code is working.But it is not giving right answer

vidushig2 gravatar imagevidushig2 ( 2017-04-22 12:01:41 -0600 )edit
1

yea, probably. try with more data, different kernel, etc.

berak gravatar imageberak ( 2017-04-23 05:29:20 -0600 )edit

hi @break, in training could i find a function or method to know if my training could give me good result or do i have to change the kernel or modify params ?!

manef gravatar imagemanef ( 2019-01-17 05:25:05 -0600 )edit

@manef, we can only help you, if you're concise about what you're doing.

berak gravatar imageberak ( 2019-01-17 05:49:10 -0600 )edit

I'm trying tio traing SVM , SVM always give my positive result, i tried to change somme param like using RGB with different value of gamma or LINEAR with different C . I'm asking if there is a way how to choose the right params ?! at least to know that i'm doing progress with training beacuase always i got positive result ( 1 ) ! i couldn't understand i have to play with the params or adding more training data

manef gravatar imagemanef ( 2019-01-17 05:55:56 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2017-04-22 05:42:01 -0600

Seen: 1,825 times

Last updated: Apr 22 '17