OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Fri, 20 Jun 2014 05:54:48 -0500How to build a regression tree over binary variables?http://answers.opencv.org/question/228/how-to-build-a-regression-tree-over-binary-variables/Let's say I have a training set {y<sub>i</sub>,x<sub>i</sub>}<sub>N</sub> with y<sub>i</sub>>0 and x<sub>i</sub> a vector of binary variables x<sub>ik</sub>∈{0,1}.
Which is the best way to build a regression tree with selected variables? Should I use CvDTree or implement my own regression tree?Tue, 10 Jul 2012 07:18:04 -0500http://answers.opencv.org/question/228/how-to-build-a-regression-tree-over-binary-variables/Answer by Maria Dimashova for <p>Let's say I have a training set {y<sub>i</sub>,x<sub>i</sub>}<sub>N</sub> with y<sub>i</sub>>0 and x<sub>i</sub> a vector of binary variables x<sub>ik</sub>∈{0,1}.</p>
<p>Which is the best way to build a regression tree with selected variables? Should I use CvDTree or implement my own regression tree?</p>
http://answers.opencv.org/question/228/how-to-build-a-regression-tree-over-binary-variables/?answer=232#post-id-232CvDTree is suitable for your task. It supports category (so binary too) and ordered variables, classification and regression problems. To build a tree you should use one of the CvDTree::train() methods, see [doc](http://docs.opencv.org/modules/ml/doc/decision_trees.html?highlight=cvdtree#cvdtree-train). For example, for the first version of train method from the doc you can:
1. Load your variables to the trainData matrix of CV\_32FC1 type, each row of which is a one sample variables;
2. Set tflag to CV\_ROW\_SAMPLE. It means that the each sample variables are located on the row in trainData.
3. Load your vector of responses yi to the 'responses' matrix of CV\_32FC1 type.
4. Pass empty matrices for varIdx and sampleIdx parameters. That means that all variables and all samples will used in the training.
5. Create varType matrix of CV\_8UC1 type, rows count = 1, cols count = variable\_count + 1 (the last "+1" is for response type). Set CV\_VAR\_ORDERED (it's 0) and CV\_VAR\_CATEGORICAL (it's 1) for ordered and categorical variables respectively. For you task it will something like this [1,..1, 0].
6. Set empty mask for missingDataMask parameter if there are not missing values in your sample variables.
7. Fill 'params'. See [doc](http://docs.opencv.org/modules/ml/doc/decision_trees.html?highlight=cvdtree#cvdtreeparams). It's more complete than the doc for CvDTree::train().
Now you can run training a regression tree for your data.Tue, 10 Jul 2012 10:34:33 -0500http://answers.opencv.org/question/228/how-to-build-a-regression-tree-over-binary-variables/?answer=232#post-id-232Comment by masad801 for <p>CvDTree is suitable for your task. It supports category (so binary too) and ordered variables, classification and regression problems. To build a tree you should use one of the CvDTree::train() methods, see <a href="http://docs.opencv.org/modules/ml/doc/decision_trees.html?highlight=cvdtree#cvdtree-train">doc</a>. For example, for the first version of train method from the doc you can:</p>
<ol>
<li>Load your variables to the trainData matrix of CV_32FC1 type, each row of which is a one sample variables;</li>
<li>Set tflag to CV_ROW_SAMPLE. It means that the each sample variables are located on the row in trainData.</li>
<li>Load your vector of responses yi to the 'responses' matrix of CV_32FC1 type. </li>
<li>Pass empty matrices for varIdx and sampleIdx parameters. That means that all variables and all samples will used in the training. </li>
<li>Create varType matrix of CV_8UC1 type, rows count = 1, cols count = variable_count + 1 (the last "+1" is for response type). Set CV_VAR_ORDERED (it's 0) and CV_VAR_CATEGORICAL (it's 1) for ordered and categorical variables respectively. For you task it will something like this [1,..1, 0]. </li>
<li>Set empty mask for missingDataMask parameter if there are not missing values in your sample variables.</li>
<li>Fill 'params'. See <a href="http://docs.opencv.org/modules/ml/doc/decision_trees.html?highlight=cvdtree#cvdtreeparams">doc</a>. It's more complete than the doc for CvDTree::train().</li>
</ol>
<p>Now you can run training a regression tree for your data.</p>
http://answers.opencv.org/question/228/how-to-build-a-regression-tree-over-binary-variables/?comment=35399#post-id-35399I have found an example of how to use OpenCV RF for classification, which gives quite detailed explanation on how to set the params. [link to code](http://breckon.eu/toby/teaching/ml/examples/c++/opticaldigits_ex/randomforest.cpp)
However what I am not sure about is how these params would reflect if my problem is of regression.
Can you please let me know how this can be done?Fri, 20 Jun 2014 05:54:48 -0500http://answers.opencv.org/question/228/how-to-build-a-regression-tree-over-binary-variables/?comment=35399#post-id-35399Comment by Niu ZhiHeng for <p>CvDTree is suitable for your task. It supports category (so binary too) and ordered variables, classification and regression problems. To build a tree you should use one of the CvDTree::train() methods, see <a href="http://docs.opencv.org/modules/ml/doc/decision_trees.html?highlight=cvdtree#cvdtree-train">doc</a>. For example, for the first version of train method from the doc you can:</p>
<ol>
<li>Load your variables to the trainData matrix of CV_32FC1 type, each row of which is a one sample variables;</li>
<li>Set tflag to CV_ROW_SAMPLE. It means that the each sample variables are located on the row in trainData.</li>
<li>Load your vector of responses yi to the 'responses' matrix of CV_32FC1 type. </li>
<li>Pass empty matrices for varIdx and sampleIdx parameters. That means that all variables and all samples will used in the training. </li>
<li>Create varType matrix of CV_8UC1 type, rows count = 1, cols count = variable_count + 1 (the last "+1" is for response type). Set CV_VAR_ORDERED (it's 0) and CV_VAR_CATEGORICAL (it's 1) for ordered and categorical variables respectively. For you task it will something like this [1,..1, 0]. </li>
<li>Set empty mask for missingDataMask parameter if there are not missing values in your sample variables.</li>
<li>Fill 'params'. See <a href="http://docs.opencv.org/modules/ml/doc/decision_trees.html?highlight=cvdtree#cvdtreeparams">doc</a>. It's more complete than the doc for CvDTree::train().</li>
</ol>
<p>Now you can run training a regression tree for your data.</p>
http://answers.opencv.org/question/228/how-to-build-a-regression-tree-over-binary-variables/?comment=236#post-id-236Thanks a lot for your valuable suggestions and detailed descriptions. Appreciate!Wed, 11 Jul 2012 01:35:04 -0500http://answers.opencv.org/question/228/how-to-build-a-regression-tree-over-binary-variables/?comment=236#post-id-236