Ask Your Question

Cv::ml::TrainData::loadFromCSV Parameters

asked 2016-10-20 23:54:11 -0500

damiya14 gravatar image

i want to load a csv file as train data.

in csv file first column contains the labels (which is 1 - 12) other columns contains the data.

cv::ml::TrainData::loadFromCSV  (   const String &  filename,
int     headerLineCount,
int     responseStartIdx = -1,
int     responseEndIdx = -1,
const String &  varTypeSpec = String(),
char    delimiter = ',',
char    missch = '?' 

i am confused with parameters. please explain the parameters.

Thank you!

edit retag flag offensive close merge delete


can you take a look at the docs and maybe refine your question ?

berak gravatar imageberak ( 2016-10-20 23:59:41 -0500 )edit

i read it like 10 times. i am confused with thees parameters

int     responseStartIdx = -1,
int     responseEndIdx = -1,
const String &  varTypeSpec = String()
damiya14 gravatar imagedamiya14 ( 2016-10-21 00:23:42 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-10-21 01:09:35 -0500

berak gravatar image

updated 2016-10-21 01:10:56 -0500

if your csv is like this (label is 1st column):

2,  1.4, 5.7, 6.6, 0.2
^            ^
label      data

then responseStartIdx is 0 and responseEndIdx is 1 .

(if your labels are in the last column, leave both at -1 (the default))

if your data is all numeric, you do not need to pass anything for varTypeSpec, this is only nessecary if you have "mixed" data like:

  1.2, apple, 17
edit flag offensive delete link more



thanks a lot!!!!!

damiya14 gravatar imagedamiya14 ( 2016-10-21 01:22:30 -0500 )edit

SVM.train(TrainData,labelsMat, Mat(), Mat(), params);

this is the normal way of training svm right? so when i load it from csv what is my labelsMat?

damiya14 gravatar imagedamiya14 ( 2016-10-21 02:28:01 -0500 )edit

wait, no, yours looks like opencv2.4 code.

remember, that traindata already contains the labels, so you would use this overload

Ptr<cv::ml::TrainData> tdata = cv::ml::TrainData::loadFromCSV("my.csv", 0, 0, 1);
Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
svm->setC(10); // maybe you want to set some params

if you don't use a csv file, you'd have a Mat features, and another Mat labels, and use it like :

svm->train(features, 0, labels);
berak gravatar imageberak ( 2016-10-21 02:34:32 -0500 )edit

Sorry for being so bad at this! i am kinda new to this. Is there a way that i can contact you?

damiya14 gravatar imagedamiya14 ( 2016-10-21 02:53:18 -0500 )edit

do not feel sorry in the 1st place, it's not your fault, that the api changed significantly, causing a lot of confusion !

then, i somewhat believe, it's better, to keep it here, (or on #opencv), so more folks can look at it, and help !

berak gravatar imageberak ( 2016-10-21 03:05:03 -0500 )edit

thanks mate!

  1. How can i save the svm.train and access in the classification program?
  2. Then how can i classify the image using the trained file?
damiya14 gravatar imagedamiya14 ( 2016-10-30 23:49:53 -0500 )edit


// careful, this makes a **new** instance !
Ptr<SVM> svm = Algorithm::load<SVM>("my.xml");
berak gravatar imageberak ( 2016-10-31 02:38:40 -0500 )edit

i am using opencv2.4 so the code do not work with me. :(


is it,"my.xmal");
damiya14 gravatar imagedamiya14 ( 2016-10-31 23:19:24 -0500 )edit

and then after saving is done i want to use that save file to classify the image. are these steps correct?

  1. Load the xml file
  2. Get features from the image
  3. Classify using svm

is there any example for this?

damiya14 gravatar imagedamiya14 ( 2016-11-01 00:46:23 -0500 )edit

oh, sorry, it seems, i got your opencv version wrong then, all the time ;(

but yes, your steps are correct (once you trained and saved the model, you do not need to reload the csv or retrain it, you can just load the saved model and do your prediction)

berak gravatar imageberak ( 2016-11-01 01:17:27 -0500 )edit

Question Tools

1 follower


Asked: 2016-10-20 23:54:11 -0500

Seen: 2,189 times

Last updated: Oct 21 '16