Does anyone have some example using Random Forests with the 2.3.1 API Mat and not the cvMat?
Basically I have a Matrix Mat data that consists of 1000 rows with 16x16x3 elements and a Matrix Mat responses a 1000x1 matrix that holds which class each row belongs to. I would like to run the random forest algorithm on this.
You've already got the data in the right format; all that's left is to instantiate a CvRTrees object and perform your prediction.
The documentation for Random Trees v2.3 can be found here. You'll also want to look at the CvStatModel::train()
documentation, which actually has the description of most of the parameters for CvRTree::train
. Tom referenced a good complete example in comments that you should use.
Along with your data, you'll need a Mat to specify the type of each of your attributes. This Mat has one row for each input attribute, and one additional row for the output type (so 16x16x3 + 1 rows, in your case).
Optionally, you can use a CvRTParams object to specify parameters like number of trees, max depth, etc. I use the defaults in the example below.
If you like, you can pass in valIdx and sampleIdx Mats that specify which attributes and which data rows, respectively, to use for training. This could be useful for selection training/validation data without doing a bunch of gymnastics to get them in separate Mats.
Here's a quick example:
#define ATTRIBUTES_PER_SAMPLE (16*16*3)
// Assumes training data (1000, 16x16x3) are in training_data
// Assumes training classifications (1000, 1) are in training_classifications
// All inputs are numerical. You can change this to reflect your data
Mat var_type = Mat(ATTRIBUTES_PER_SAMPLE + 1, 1, CV_8U );
var_type.setTo(Scalar(CV_VAR_NUMERICAL) ); // all inputs are numerical
// Output is a category; this is classification, not regression
var_type.at<uchar>(ATTRIBUTES_PER_SAMPLE, 0) = CV_VAR_CATEGORICAL;
// Train the classifier
CvRTrees* rtree = new CvRTrees;
rtree->train(training_data, CV_ROW_SAMPLE, training_classifications,
Mat(), Mat(), var_type);