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<<" "<<result.cols<<"\n";
cout<<"result = "<< " " <<result<<"\n";
return 0;
}
since your csv seems to be rather small (38 items ?), could you add it, so folks could try your code ?
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.